1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 27999096fSHerbert Xu #include <crypto/hash.h> 3d879cb83SAl Viro #include <linux/export.h> 42f8b5444SChristoph Hellwig #include <linux/bvec.h> 54d0e9df5SAlbert van der Linde #include <linux/fault-inject-usercopy.h> 6d879cb83SAl Viro #include <linux/uio.h> 7d879cb83SAl Viro #include <linux/pagemap.h> 828961998SIra Weiny #include <linux/highmem.h> 9d879cb83SAl Viro #include <linux/slab.h> 10d879cb83SAl Viro #include <linux/vmalloc.h> 11241699cdSAl Viro #include <linux/splice.h> 12bfdc5970SChristoph Hellwig #include <linux/compat.h> 13d879cb83SAl Viro #include <net/checksum.h> 14d05f4435SSagi Grimberg #include <linux/scatterlist.h> 15d0ef4c36SMarco Elver #include <linux/instrumented.h> 16d879cb83SAl Viro 17241699cdSAl Viro #define PIPE_PARANOIA /* for now */ 18241699cdSAl Viro 19fcb14cb1SAl Viro /* covers ubuf and kbuf alike */ 20fcb14cb1SAl Viro #define iterate_buf(i, n, base, len, off, __p, STEP) { \ 21fcb14cb1SAl Viro size_t __maybe_unused off = 0; \ 22fcb14cb1SAl Viro len = n; \ 23fcb14cb1SAl Viro base = __p + i->iov_offset; \ 24fcb14cb1SAl Viro len -= (STEP); \ 25fcb14cb1SAl Viro i->iov_offset += len; \ 26fcb14cb1SAl Viro n = len; \ 27fcb14cb1SAl Viro } 28fcb14cb1SAl Viro 295c67aa90SAl Viro /* covers iovec and kvec alike */ 30a6e4ec7bSAl Viro #define iterate_iovec(i, n, base, len, off, __p, STEP) { \ 317baa5099SAl Viro size_t off = 0; \ 32a6e4ec7bSAl Viro size_t skip = i->iov_offset; \ 337a1bcb5dSAl Viro do { \ 347baa5099SAl Viro len = min(n, __p->iov_len - skip); \ 357baa5099SAl Viro if (likely(len)) { \ 367baa5099SAl Viro base = __p->iov_base + skip; \ 377baa5099SAl Viro len -= (STEP); \ 387baa5099SAl Viro off += len; \ 397baa5099SAl Viro skip += len; \ 407baa5099SAl Viro n -= len; \ 417a1bcb5dSAl Viro if (skip < __p->iov_len) \ 427a1bcb5dSAl Viro break; \ 43d879cb83SAl Viro } \ 44d879cb83SAl Viro __p++; \ 457a1bcb5dSAl Viro skip = 0; \ 467a1bcb5dSAl Viro } while (n); \ 47a6e4ec7bSAl Viro i->iov_offset = skip; \ 487baa5099SAl Viro n = off; \ 49d879cb83SAl Viro } 50d879cb83SAl Viro 51a6e4ec7bSAl Viro #define iterate_bvec(i, n, base, len, off, p, STEP) { \ 527baa5099SAl Viro size_t off = 0; \ 53a6e4ec7bSAl Viro unsigned skip = i->iov_offset; \ 547491a2bfSAl Viro while (n) { \ 557491a2bfSAl Viro unsigned offset = p->bv_offset + skip; \ 561b4fb5ffSAl Viro unsigned left; \ 5721b56c84SAl Viro void *kaddr = kmap_local_page(p->bv_page + \ 5821b56c84SAl Viro offset / PAGE_SIZE); \ 597baa5099SAl Viro base = kaddr + offset % PAGE_SIZE; \ 60a6e4ec7bSAl Viro len = min(min(n, (size_t)(p->bv_len - skip)), \ 617491a2bfSAl Viro (size_t)(PAGE_SIZE - offset % PAGE_SIZE)); \ 621b4fb5ffSAl Viro left = (STEP); \ 6321b56c84SAl Viro kunmap_local(kaddr); \ 647baa5099SAl Viro len -= left; \ 657baa5099SAl Viro off += len; \ 667baa5099SAl Viro skip += len; \ 677491a2bfSAl Viro if (skip == p->bv_len) { \ 687491a2bfSAl Viro skip = 0; \ 697491a2bfSAl Viro p++; \ 70d879cb83SAl Viro } \ 717baa5099SAl Viro n -= len; \ 721b4fb5ffSAl Viro if (left) \ 731b4fb5ffSAl Viro break; \ 747491a2bfSAl Viro } \ 75a6e4ec7bSAl Viro i->iov_offset = skip; \ 767baa5099SAl Viro n = off; \ 77d879cb83SAl Viro } 78d879cb83SAl Viro 79a6e4ec7bSAl Viro #define iterate_xarray(i, n, base, len, __off, STEP) { \ 801b4fb5ffSAl Viro __label__ __out; \ 81622838f3SAl Viro size_t __off = 0; \ 82821979f5SMatthew Wilcox (Oracle) struct folio *folio; \ 83a6e4ec7bSAl Viro loff_t start = i->xarray_start + i->iov_offset; \ 844b179e9aSAl Viro pgoff_t index = start / PAGE_SIZE; \ 857ff50620SDavid Howells XA_STATE(xas, i->xarray, index); \ 867ff50620SDavid Howells \ 87821979f5SMatthew Wilcox (Oracle) len = PAGE_SIZE - offset_in_page(start); \ 887ff50620SDavid Howells rcu_read_lock(); \ 89821979f5SMatthew Wilcox (Oracle) xas_for_each(&xas, folio, ULONG_MAX) { \ 901b4fb5ffSAl Viro unsigned left; \ 91821979f5SMatthew Wilcox (Oracle) size_t offset; \ 92821979f5SMatthew Wilcox (Oracle) if (xas_retry(&xas, folio)) \ 937ff50620SDavid Howells continue; \ 94821979f5SMatthew Wilcox (Oracle) if (WARN_ON(xa_is_value(folio))) \ 957ff50620SDavid Howells break; \ 96821979f5SMatthew Wilcox (Oracle) if (WARN_ON(folio_test_hugetlb(folio))) \ 977ff50620SDavid Howells break; \ 98821979f5SMatthew Wilcox (Oracle) offset = offset_in_folio(folio, start + __off); \ 99821979f5SMatthew Wilcox (Oracle) while (offset < folio_size(folio)) { \ 100821979f5SMatthew Wilcox (Oracle) base = kmap_local_folio(folio, offset); \ 1017baa5099SAl Viro len = min(n, len); \ 1021b4fb5ffSAl Viro left = (STEP); \ 103821979f5SMatthew Wilcox (Oracle) kunmap_local(base); \ 1047baa5099SAl Viro len -= left; \ 1057baa5099SAl Viro __off += len; \ 1067baa5099SAl Viro n -= len; \ 1071b4fb5ffSAl Viro if (left || n == 0) \ 1081b4fb5ffSAl Viro goto __out; \ 109821979f5SMatthew Wilcox (Oracle) offset += len; \ 110821979f5SMatthew Wilcox (Oracle) len = PAGE_SIZE; \ 1117ff50620SDavid Howells } \ 1127ff50620SDavid Howells } \ 1131b4fb5ffSAl Viro __out: \ 1147ff50620SDavid Howells rcu_read_unlock(); \ 115a6e4ec7bSAl Viro i->iov_offset += __off; \ 116622838f3SAl Viro n = __off; \ 1177ff50620SDavid Howells } 1187ff50620SDavid Howells 1197baa5099SAl Viro #define __iterate_and_advance(i, n, base, len, off, I, K) { \ 120dd254f5aSAl Viro if (unlikely(i->count < n)) \ 121dd254f5aSAl Viro n = i->count; \ 122f5da8354SAl Viro if (likely(n)) { \ 123fcb14cb1SAl Viro if (likely(iter_is_ubuf(i))) { \ 124fcb14cb1SAl Viro void __user *base; \ 125fcb14cb1SAl Viro size_t len; \ 126fcb14cb1SAl Viro iterate_buf(i, n, base, len, off, \ 127fcb14cb1SAl Viro i->ubuf, (I)) \ 128fcb14cb1SAl Viro } else if (likely(iter_is_iovec(i))) { \ 1295c67aa90SAl Viro const struct iovec *iov = i->iov; \ 1307baa5099SAl Viro void __user *base; \ 1317baa5099SAl Viro size_t len; \ 1327baa5099SAl Viro iterate_iovec(i, n, base, len, off, \ 133a6e4ec7bSAl Viro iov, (I)) \ 134d879cb83SAl Viro i->nr_segs -= iov - i->iov; \ 135d879cb83SAl Viro i->iov = iov; \ 13628f38db7SAl Viro } else if (iov_iter_is_bvec(i)) { \ 13728f38db7SAl Viro const struct bio_vec *bvec = i->bvec; \ 1387baa5099SAl Viro void *base; \ 1397baa5099SAl Viro size_t len; \ 1407baa5099SAl Viro iterate_bvec(i, n, base, len, off, \ 141a6e4ec7bSAl Viro bvec, (K)) \ 1427491a2bfSAl Viro i->nr_segs -= bvec - i->bvec; \ 1437491a2bfSAl Viro i->bvec = bvec; \ 14428f38db7SAl Viro } else if (iov_iter_is_kvec(i)) { \ 1455c67aa90SAl Viro const struct kvec *kvec = i->kvec; \ 1467baa5099SAl Viro void *base; \ 1477baa5099SAl Viro size_t len; \ 1487baa5099SAl Viro iterate_iovec(i, n, base, len, off, \ 149a6e4ec7bSAl Viro kvec, (K)) \ 15028f38db7SAl Viro i->nr_segs -= kvec - i->kvec; \ 15128f38db7SAl Viro i->kvec = kvec; \ 15228f38db7SAl Viro } else if (iov_iter_is_xarray(i)) { \ 1537baa5099SAl Viro void *base; \ 1547baa5099SAl Viro size_t len; \ 1557baa5099SAl Viro iterate_xarray(i, n, base, len, off, \ 156a6e4ec7bSAl Viro (K)) \ 157d879cb83SAl Viro } \ 158d879cb83SAl Viro i->count -= n; \ 159dd254f5aSAl Viro } \ 160d879cb83SAl Viro } 1617baa5099SAl Viro #define iterate_and_advance(i, n, base, len, off, I, K) \ 1627baa5099SAl Viro __iterate_and_advance(i, n, base, len, off, I, ((void)(K),0)) 163d879cb83SAl Viro 16409fc68dcSAl Viro static int copyout(void __user *to, const void *from, size_t n) 16509fc68dcSAl Viro { 1664d0e9df5SAlbert van der Linde if (should_fail_usercopy()) 1674d0e9df5SAlbert van der Linde return n; 16896d4f267SLinus Torvalds if (access_ok(to, n)) { 169d0ef4c36SMarco Elver instrument_copy_to_user(to, from, n); 17009fc68dcSAl Viro n = raw_copy_to_user(to, from, n); 17109fc68dcSAl Viro } 17209fc68dcSAl Viro return n; 17309fc68dcSAl Viro } 17409fc68dcSAl Viro 175*4f80818bSLorenzo Stoakes static int copyout_nofault(void __user *to, const void *from, size_t n) 176*4f80818bSLorenzo Stoakes { 177*4f80818bSLorenzo Stoakes long res; 178*4f80818bSLorenzo Stoakes 179*4f80818bSLorenzo Stoakes if (should_fail_usercopy()) 180*4f80818bSLorenzo Stoakes return n; 181*4f80818bSLorenzo Stoakes 182*4f80818bSLorenzo Stoakes res = copy_to_user_nofault(to, from, n); 183*4f80818bSLorenzo Stoakes 184*4f80818bSLorenzo Stoakes return res < 0 ? n : res; 185*4f80818bSLorenzo Stoakes } 186*4f80818bSLorenzo 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; 370a6294593SAndreas Gruenbacher for (p = i->iov, 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; 413cdd591fcSAndreas Gruenbacher for (p = i->iov, 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, 4373337ab08SAndreas Gruenbacher .nofault = false, 438fcb14cb1SAl Viro .user_backed = true, 4398cd54c1cSAl Viro .data_source = direction, 4408cd54c1cSAl Viro .iov = iov, 4418cd54c1cSAl Viro .nr_segs = nr_segs, 4428cd54c1cSAl Viro .iov_offset = 0, 4438cd54c1cSAl Viro .count = count 4448cd54c1cSAl Viro }; 445d879cb83SAl Viro } 446d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_init); 447d879cb83SAl Viro 44812d426abSAl Viro // returns the offset in partial buffer (if any) 44912d426abSAl Viro static inline unsigned int pipe_npages(const struct iov_iter *i, int *npages) 450241699cdSAl Viro { 45112d426abSAl Viro struct pipe_inode_info *pipe = i->pipe; 45212d426abSAl Viro int used = pipe->head - pipe->tail; 45310f525a8SAl Viro int off = i->last_offset; 4548cefc107SDavid Howells 45512d426abSAl Viro *npages = max((int)pipe->max_usage - used, 0); 45612d426abSAl Viro 45710f525a8SAl Viro if (off > 0 && off < PAGE_SIZE) { // anon and not full 45812d426abSAl Viro (*npages)++; 45912d426abSAl Viro return off; 46010f525a8SAl Viro } 46112d426abSAl Viro return 0; 462241699cdSAl Viro } 463241699cdSAl Viro 464241699cdSAl Viro static size_t copy_pipe_to_iter(const void *addr, size_t bytes, 465241699cdSAl Viro struct iov_iter *i) 466241699cdSAl Viro { 4678fad7767SAl Viro unsigned int off, chunk; 4688fad7767SAl Viro 4698fad7767SAl Viro if (unlikely(bytes > i->count)) 4708fad7767SAl Viro bytes = i->count; 4718fad7767SAl Viro if (unlikely(!bytes)) 4728fad7767SAl Viro return 0; 473241699cdSAl Viro 474241699cdSAl Viro if (!sanity(i)) 475241699cdSAl Viro return 0; 476241699cdSAl Viro 4778fad7767SAl Viro for (size_t n = bytes; n; n -= chunk) { 4788fad7767SAl Viro struct page *page = append_pipe(i, n, &off); 4798fad7767SAl Viro chunk = min_t(size_t, n, PAGE_SIZE - off); 4808fad7767SAl Viro if (!page) 4818fad7767SAl Viro return bytes - n; 4828fad7767SAl Viro memcpy_to_page(page, off, addr, chunk); 483241699cdSAl Viro addr += chunk; 4848fad7767SAl Viro } 485241699cdSAl Viro return bytes; 486241699cdSAl Viro } 487241699cdSAl Viro 488f9152895SAl Viro static __wsum csum_and_memcpy(void *to, const void *from, size_t len, 489f9152895SAl Viro __wsum sum, size_t off) 490f9152895SAl Viro { 491cc44c17bSAl Viro __wsum next = csum_partial_copy_nocheck(from, to, len); 492f9152895SAl Viro return csum_block_add(sum, next, off); 493f9152895SAl Viro } 494f9152895SAl Viro 49578e1f386SAl Viro static size_t csum_and_copy_to_pipe_iter(const void *addr, size_t bytes, 4966852df12SAl Viro struct iov_iter *i, __wsum *sump) 49778e1f386SAl Viro { 4986852df12SAl Viro __wsum sum = *sump; 4996852df12SAl Viro size_t off = 0; 5008fad7767SAl Viro unsigned int chunk, r; 5018fad7767SAl Viro 5028fad7767SAl Viro if (unlikely(bytes > i->count)) 5038fad7767SAl Viro bytes = i->count; 5048fad7767SAl Viro if (unlikely(!bytes)) 5058fad7767SAl Viro return 0; 50678e1f386SAl Viro 50778e1f386SAl Viro if (!sanity(i)) 50878e1f386SAl Viro return 0; 50978e1f386SAl Viro 5106852df12SAl Viro while (bytes) { 5118fad7767SAl Viro struct page *page = append_pipe(i, bytes, &r); 5128fad7767SAl Viro char *p; 5138fad7767SAl Viro 5148fad7767SAl Viro if (!page) 5158fad7767SAl Viro break; 5168fad7767SAl Viro chunk = min_t(size_t, bytes, PAGE_SIZE - r); 5178fad7767SAl Viro p = kmap_local_page(page); 5186852df12SAl Viro sum = csum_and_memcpy(p + r, addr + off, chunk, sum, off); 5192495bdccSAl Viro kunmap_local(p); 52078e1f386SAl Viro off += chunk; 5218fad7767SAl Viro bytes -= chunk; 5226852df12SAl Viro } 5236852df12SAl Viro *sump = sum; 5246852df12SAl Viro return off; 52578e1f386SAl Viro } 52678e1f386SAl Viro 527aa28de27SAl Viro size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i) 528d879cb83SAl Viro { 529a41dad90SAl Viro if (WARN_ON_ONCE(i->data_source)) 530a41dad90SAl Viro return 0; 53100e23707SDavid Howells if (unlikely(iov_iter_is_pipe(i))) 532241699cdSAl Viro return copy_pipe_to_iter(addr, bytes, i); 533fcb14cb1SAl Viro if (user_backed_iter(i)) 53409fc68dcSAl Viro might_fault(); 5357baa5099SAl Viro iterate_and_advance(i, bytes, base, len, off, 5367baa5099SAl Viro copyout(base, addr + off, len), 5377baa5099SAl Viro memcpy(base, addr + off, len) 538d879cb83SAl Viro ) 539d879cb83SAl Viro 540d879cb83SAl Viro return bytes; 541d879cb83SAl Viro } 542aa28de27SAl Viro EXPORT_SYMBOL(_copy_to_iter); 543d879cb83SAl Viro 544ec6347bbSDan Williams #ifdef CONFIG_ARCH_HAS_COPY_MC 545ec6347bbSDan Williams static int copyout_mc(void __user *to, const void *from, size_t n) 5468780356eSDan Williams { 54796d4f267SLinus Torvalds if (access_ok(to, n)) { 548d0ef4c36SMarco Elver instrument_copy_to_user(to, from, n); 549ec6347bbSDan Williams n = copy_mc_to_user((__force void *) to, from, n); 5508780356eSDan Williams } 5518780356eSDan Williams return n; 5528780356eSDan Williams } 5538780356eSDan Williams 554ec6347bbSDan Williams static size_t copy_mc_pipe_to_iter(const void *addr, size_t bytes, 555ca146f6fSDan Williams struct iov_iter *i) 556ca146f6fSDan Williams { 5578fad7767SAl Viro size_t xfer = 0; 5588fad7767SAl Viro unsigned int off, chunk; 5598fad7767SAl Viro 5608fad7767SAl Viro if (unlikely(bytes > i->count)) 5618fad7767SAl Viro bytes = i->count; 5628fad7767SAl Viro if (unlikely(!bytes)) 5638fad7767SAl Viro return 0; 564ca146f6fSDan Williams 565ca146f6fSDan Williams if (!sanity(i)) 566ca146f6fSDan Williams return 0; 567ca146f6fSDan Williams 5688fad7767SAl Viro while (bytes) { 5698fad7767SAl Viro struct page *page = append_pipe(i, bytes, &off); 570ca146f6fSDan Williams unsigned long rem; 5718fad7767SAl Viro char *p; 5728fad7767SAl Viro 5738fad7767SAl Viro if (!page) 5748fad7767SAl Viro break; 5758fad7767SAl Viro chunk = min_t(size_t, bytes, PAGE_SIZE - off); 5768fad7767SAl Viro p = kmap_local_page(page); 5772a510a74SAl Viro rem = copy_mc_to_kernel(p + off, addr + xfer, chunk); 5782a510a74SAl Viro chunk -= rem; 5792a510a74SAl Viro kunmap_local(p); 5802a510a74SAl Viro xfer += chunk; 5818fad7767SAl Viro bytes -= chunk; 582c3497fd0SAl Viro if (rem) { 5838fad7767SAl Viro iov_iter_revert(i, rem); 584ca146f6fSDan Williams break; 585c3497fd0SAl Viro } 5862a510a74SAl Viro } 587ca146f6fSDan Williams return xfer; 588ca146f6fSDan Williams } 589ca146f6fSDan Williams 590bf3eeb9bSDan Williams /** 591ec6347bbSDan Williams * _copy_mc_to_iter - copy to iter with source memory error exception handling 592bf3eeb9bSDan Williams * @addr: source kernel address 593bf3eeb9bSDan Williams * @bytes: total transfer length 59444e55997SRandy Dunlap * @i: destination iterator 595bf3eeb9bSDan Williams * 596ec6347bbSDan Williams * The pmem driver deploys this for the dax operation 597ec6347bbSDan Williams * (dax_copy_to_iter()) for dax reads (bypass page-cache and the 598ec6347bbSDan Williams * block-layer). Upon #MC read(2) aborts and returns EIO or the bytes 599ec6347bbSDan Williams * successfully copied. 600bf3eeb9bSDan Williams * 601ec6347bbSDan Williams * The main differences between this and typical _copy_to_iter(). 602bf3eeb9bSDan Williams * 603bf3eeb9bSDan Williams * * Typical tail/residue handling after a fault retries the copy 604bf3eeb9bSDan Williams * byte-by-byte until the fault happens again. Re-triggering machine 605bf3eeb9bSDan Williams * checks is potentially fatal so the implementation uses source 606bf3eeb9bSDan Williams * alignment and poison alignment assumptions to avoid re-triggering 607bf3eeb9bSDan Williams * hardware exceptions. 608bf3eeb9bSDan Williams * 609bf3eeb9bSDan Williams * * ITER_KVEC, ITER_PIPE, and ITER_BVEC can return short copies. 610bf3eeb9bSDan Williams * Compare to copy_to_iter() where only ITER_IOVEC attempts might return 611bf3eeb9bSDan Williams * a short copy. 61244e55997SRandy Dunlap * 61344e55997SRandy Dunlap * Return: number of bytes copied (may be %0) 614bf3eeb9bSDan Williams */ 615ec6347bbSDan Williams size_t _copy_mc_to_iter(const void *addr, size_t bytes, struct iov_iter *i) 6168780356eSDan Williams { 617a41dad90SAl Viro if (WARN_ON_ONCE(i->data_source)) 618a41dad90SAl Viro return 0; 61900e23707SDavid Howells if (unlikely(iov_iter_is_pipe(i))) 620ec6347bbSDan Williams return copy_mc_pipe_to_iter(addr, bytes, i); 621fcb14cb1SAl Viro if (user_backed_iter(i)) 6228780356eSDan Williams might_fault(); 6237baa5099SAl Viro __iterate_and_advance(i, bytes, base, len, off, 6247baa5099SAl Viro copyout_mc(base, addr + off, len), 6257baa5099SAl Viro copy_mc_to_kernel(base, addr + off, len) 6268780356eSDan Williams ) 6278780356eSDan Williams 6288780356eSDan Williams return bytes; 6298780356eSDan Williams } 630ec6347bbSDan Williams EXPORT_SYMBOL_GPL(_copy_mc_to_iter); 631ec6347bbSDan Williams #endif /* CONFIG_ARCH_HAS_COPY_MC */ 6328780356eSDan Williams 633aa28de27SAl Viro size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i) 634d879cb83SAl Viro { 635a41dad90SAl Viro if (WARN_ON_ONCE(!i->data_source)) 636241699cdSAl Viro return 0; 637a41dad90SAl Viro 638fcb14cb1SAl Viro if (user_backed_iter(i)) 63909fc68dcSAl Viro might_fault(); 6407baa5099SAl Viro iterate_and_advance(i, bytes, base, len, off, 6417baa5099SAl Viro copyin(addr + off, base, len), 6427baa5099SAl Viro memcpy(addr + off, base, len) 643d879cb83SAl Viro ) 644d879cb83SAl Viro 645d879cb83SAl Viro return bytes; 646d879cb83SAl Viro } 647aa28de27SAl Viro EXPORT_SYMBOL(_copy_from_iter); 648d879cb83SAl Viro 649aa28de27SAl Viro size_t _copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i) 650d879cb83SAl Viro { 651a41dad90SAl Viro if (WARN_ON_ONCE(!i->data_source)) 652241699cdSAl Viro return 0; 653a41dad90SAl Viro 6547baa5099SAl Viro iterate_and_advance(i, bytes, base, len, off, 6557baa5099SAl Viro __copy_from_user_inatomic_nocache(addr + off, base, len), 6567baa5099SAl Viro memcpy(addr + off, base, len) 657d879cb83SAl Viro ) 658d879cb83SAl Viro 659d879cb83SAl Viro return bytes; 660d879cb83SAl Viro } 661aa28de27SAl Viro EXPORT_SYMBOL(_copy_from_iter_nocache); 662d879cb83SAl Viro 6630aed55afSDan Williams #ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE 664abd08d7dSDan Williams /** 665abd08d7dSDan Williams * _copy_from_iter_flushcache - write destination through cpu cache 666abd08d7dSDan Williams * @addr: destination kernel address 667abd08d7dSDan Williams * @bytes: total transfer length 66844e55997SRandy Dunlap * @i: source iterator 669abd08d7dSDan Williams * 670abd08d7dSDan Williams * The pmem driver arranges for filesystem-dax to use this facility via 671abd08d7dSDan Williams * dax_copy_from_iter() for ensuring that writes to persistent memory 672abd08d7dSDan Williams * are flushed through the CPU cache. It is differentiated from 673abd08d7dSDan Williams * _copy_from_iter_nocache() in that guarantees all data is flushed for 674abd08d7dSDan Williams * all iterator types. The _copy_from_iter_nocache() only attempts to 675abd08d7dSDan Williams * bypass the cache for the ITER_IOVEC case, and on some archs may use 676abd08d7dSDan Williams * instructions that strand dirty-data in the cache. 67744e55997SRandy Dunlap * 67844e55997SRandy Dunlap * Return: number of bytes copied (may be %0) 679abd08d7dSDan Williams */ 6806a37e940SLinus Torvalds size_t _copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i) 6810aed55afSDan Williams { 682a41dad90SAl Viro if (WARN_ON_ONCE(!i->data_source)) 6830aed55afSDan Williams return 0; 684a41dad90SAl Viro 6857baa5099SAl Viro iterate_and_advance(i, bytes, base, len, off, 6867baa5099SAl Viro __copy_from_user_flushcache(addr + off, base, len), 6877baa5099SAl Viro memcpy_flushcache(addr + off, base, len) 6880aed55afSDan Williams ) 6890aed55afSDan Williams 6900aed55afSDan Williams return bytes; 6910aed55afSDan Williams } 6926a37e940SLinus Torvalds EXPORT_SYMBOL_GPL(_copy_from_iter_flushcache); 6930aed55afSDan Williams #endif 6940aed55afSDan Williams 69572e809edSAl Viro static inline bool page_copy_sane(struct page *page, size_t offset, size_t n) 69672e809edSAl Viro { 6976daef95bSEric Dumazet struct page *head; 6986daef95bSEric Dumazet size_t v = n + offset; 6996daef95bSEric Dumazet 7006daef95bSEric Dumazet /* 7016daef95bSEric Dumazet * The general case needs to access the page order in order 7026daef95bSEric Dumazet * to compute the page size. 7036daef95bSEric Dumazet * However, we mostly deal with order-0 pages and thus can 7046daef95bSEric Dumazet * avoid a possible cache line miss for requests that fit all 7056daef95bSEric Dumazet * page orders. 7066daef95bSEric Dumazet */ 7076daef95bSEric Dumazet if (n <= v && v <= PAGE_SIZE) 7086daef95bSEric Dumazet return true; 7096daef95bSEric Dumazet 7106daef95bSEric Dumazet head = compound_head(page); 7116daef95bSEric Dumazet v += (page - head) << PAGE_SHIFT; 712a90bcb86SPetar Penkov 71340a86061SAl Viro if (WARN_ON(n > v || v > page_size(head))) 71472e809edSAl Viro return false; 71540a86061SAl Viro return true; 71672e809edSAl Viro } 717cbbd26b8SAl Viro 71808aa6479SAl Viro size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes, 71908aa6479SAl Viro struct iov_iter *i) 72008aa6479SAl Viro { 72108aa6479SAl Viro size_t res = 0; 72240a86061SAl Viro if (!page_copy_sane(page, offset, bytes)) 72308aa6479SAl Viro return 0; 724a41dad90SAl Viro if (WARN_ON_ONCE(i->data_source)) 725a41dad90SAl Viro return 0; 726f0f6b614SAl Viro if (unlikely(iov_iter_is_pipe(i))) 727f0f6b614SAl Viro return copy_page_to_iter_pipe(page, offset, bytes, i); 72808aa6479SAl Viro page += offset / PAGE_SIZE; // first subpage 72908aa6479SAl Viro offset %= PAGE_SIZE; 73008aa6479SAl Viro while (1) { 731f0f6b614SAl Viro void *kaddr = kmap_local_page(page); 732f0f6b614SAl Viro size_t n = min(bytes, (size_t)PAGE_SIZE - offset); 733f0f6b614SAl Viro n = _copy_to_iter(kaddr + offset, n, i); 734f0f6b614SAl Viro kunmap_local(kaddr); 73508aa6479SAl Viro res += n; 73608aa6479SAl Viro bytes -= n; 73708aa6479SAl Viro if (!bytes || !n) 73808aa6479SAl Viro break; 73908aa6479SAl Viro offset += n; 74008aa6479SAl Viro if (offset == PAGE_SIZE) { 74108aa6479SAl Viro page++; 74208aa6479SAl Viro offset = 0; 74308aa6479SAl Viro } 74408aa6479SAl Viro } 74508aa6479SAl Viro return res; 74608aa6479SAl Viro } 747d879cb83SAl Viro EXPORT_SYMBOL(copy_page_to_iter); 748d879cb83SAl Viro 749*4f80818bSLorenzo Stoakes size_t copy_page_to_iter_nofault(struct page *page, unsigned offset, size_t bytes, 750*4f80818bSLorenzo Stoakes struct iov_iter *i) 751*4f80818bSLorenzo Stoakes { 752*4f80818bSLorenzo Stoakes size_t res = 0; 753*4f80818bSLorenzo Stoakes 754*4f80818bSLorenzo Stoakes if (!page_copy_sane(page, offset, bytes)) 755*4f80818bSLorenzo Stoakes return 0; 756*4f80818bSLorenzo Stoakes if (WARN_ON_ONCE(i->data_source)) 757*4f80818bSLorenzo Stoakes return 0; 758*4f80818bSLorenzo Stoakes if (unlikely(iov_iter_is_pipe(i))) 759*4f80818bSLorenzo Stoakes return copy_page_to_iter_pipe(page, offset, bytes, i); 760*4f80818bSLorenzo Stoakes page += offset / PAGE_SIZE; // first subpage 761*4f80818bSLorenzo Stoakes offset %= PAGE_SIZE; 762*4f80818bSLorenzo Stoakes while (1) { 763*4f80818bSLorenzo Stoakes void *kaddr = kmap_local_page(page); 764*4f80818bSLorenzo Stoakes size_t n = min(bytes, (size_t)PAGE_SIZE - offset); 765*4f80818bSLorenzo Stoakes 766*4f80818bSLorenzo Stoakes iterate_and_advance(i, n, base, len, off, 767*4f80818bSLorenzo Stoakes copyout_nofault(base, kaddr + offset + off, len), 768*4f80818bSLorenzo Stoakes memcpy(base, kaddr + offset + off, len) 769*4f80818bSLorenzo Stoakes ) 770*4f80818bSLorenzo Stoakes kunmap_local(kaddr); 771*4f80818bSLorenzo Stoakes res += n; 772*4f80818bSLorenzo Stoakes bytes -= n; 773*4f80818bSLorenzo Stoakes if (!bytes || !n) 774*4f80818bSLorenzo Stoakes break; 775*4f80818bSLorenzo Stoakes offset += n; 776*4f80818bSLorenzo Stoakes if (offset == PAGE_SIZE) { 777*4f80818bSLorenzo Stoakes page++; 778*4f80818bSLorenzo Stoakes offset = 0; 779*4f80818bSLorenzo Stoakes } 780*4f80818bSLorenzo Stoakes } 781*4f80818bSLorenzo Stoakes return res; 782*4f80818bSLorenzo Stoakes } 783*4f80818bSLorenzo Stoakes EXPORT_SYMBOL(copy_page_to_iter_nofault); 784*4f80818bSLorenzo Stoakes 785d879cb83SAl Viro size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes, 786d879cb83SAl Viro struct iov_iter *i) 787d879cb83SAl Viro { 788c03f05f1SAl Viro size_t res = 0; 789c03f05f1SAl Viro if (!page_copy_sane(page, offset, bytes)) 79028f38db7SAl Viro return 0; 791c03f05f1SAl Viro page += offset / PAGE_SIZE; // first subpage 792c03f05f1SAl Viro offset %= PAGE_SIZE; 793c03f05f1SAl Viro while (1) { 794c03f05f1SAl Viro void *kaddr = kmap_local_page(page); 795c03f05f1SAl Viro size_t n = min(bytes, (size_t)PAGE_SIZE - offset); 796c03f05f1SAl Viro n = _copy_from_iter(kaddr + offset, n, i); 797c03f05f1SAl Viro kunmap_local(kaddr); 798c03f05f1SAl Viro res += n; 799c03f05f1SAl Viro bytes -= n; 800c03f05f1SAl Viro if (!bytes || !n) 801c03f05f1SAl Viro break; 802c03f05f1SAl Viro offset += n; 803c03f05f1SAl Viro if (offset == PAGE_SIZE) { 804c03f05f1SAl Viro page++; 805c03f05f1SAl Viro offset = 0; 806c03f05f1SAl Viro } 807c03f05f1SAl Viro } 808c03f05f1SAl Viro return res; 809d879cb83SAl Viro } 810d879cb83SAl Viro EXPORT_SYMBOL(copy_page_from_iter); 811d879cb83SAl Viro 812241699cdSAl Viro static size_t pipe_zero(size_t bytes, struct iov_iter *i) 813241699cdSAl Viro { 8148fad7767SAl Viro unsigned int chunk, off; 8158fad7767SAl Viro 8168fad7767SAl Viro if (unlikely(bytes > i->count)) 8178fad7767SAl Viro bytes = i->count; 8188fad7767SAl Viro if (unlikely(!bytes)) 8198fad7767SAl Viro return 0; 820241699cdSAl Viro 821241699cdSAl Viro if (!sanity(i)) 822241699cdSAl Viro return 0; 823241699cdSAl Viro 8248fad7767SAl Viro for (size_t n = bytes; n; n -= chunk) { 8258fad7767SAl Viro struct page *page = append_pipe(i, n, &off); 8268fad7767SAl Viro char *p; 827241699cdSAl Viro 8288fad7767SAl Viro if (!page) 8298fad7767SAl Viro return bytes - n; 8308fad7767SAl Viro chunk = min_t(size_t, n, PAGE_SIZE - off); 8318fad7767SAl Viro p = kmap_local_page(page); 832893839fdSAl Viro memset(p + off, 0, chunk); 833893839fdSAl Viro kunmap_local(p); 8348fad7767SAl Viro } 835241699cdSAl Viro return bytes; 836241699cdSAl Viro } 837241699cdSAl Viro 838d879cb83SAl Viro size_t iov_iter_zero(size_t bytes, struct iov_iter *i) 839d879cb83SAl Viro { 84000e23707SDavid Howells if (unlikely(iov_iter_is_pipe(i))) 841241699cdSAl Viro return pipe_zero(bytes, i); 8427baa5099SAl Viro iterate_and_advance(i, bytes, base, len, count, 8437baa5099SAl Viro clear_user(base, len), 8447baa5099SAl Viro memset(base, 0, len) 845d879cb83SAl Viro ) 846d879cb83SAl Viro 847d879cb83SAl Viro return bytes; 848d879cb83SAl Viro } 849d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_zero); 850d879cb83SAl Viro 851f0b65f39SAl Viro size_t copy_page_from_iter_atomic(struct page *page, unsigned offset, size_t bytes, 852f0b65f39SAl Viro struct iov_iter *i) 853d879cb83SAl Viro { 854d879cb83SAl Viro char *kaddr = kmap_atomic(page), *p = kaddr + offset; 85540a86061SAl Viro if (!page_copy_sane(page, offset, bytes)) { 85672e809edSAl Viro kunmap_atomic(kaddr); 85772e809edSAl Viro return 0; 85872e809edSAl Viro } 859a41dad90SAl Viro if (WARN_ON_ONCE(!i->data_source)) { 860241699cdSAl Viro kunmap_atomic(kaddr); 861241699cdSAl Viro return 0; 862241699cdSAl Viro } 8637baa5099SAl Viro iterate_and_advance(i, bytes, base, len, off, 8647baa5099SAl Viro copyin(p + off, base, len), 8657baa5099SAl Viro memcpy(p + off, base, len) 866d879cb83SAl Viro ) 867d879cb83SAl Viro kunmap_atomic(kaddr); 868d879cb83SAl Viro return bytes; 869d879cb83SAl Viro } 870f0b65f39SAl Viro EXPORT_SYMBOL(copy_page_from_iter_atomic); 871d879cb83SAl Viro 872b9dc6f65SAl Viro static void pipe_advance(struct iov_iter *i, size_t size) 873b9dc6f65SAl Viro { 874b9dc6f65SAl Viro struct pipe_inode_info *pipe = i->pipe; 87510f525a8SAl Viro int off = i->last_offset; 8768cefc107SDavid Howells 8772c855de9SAl Viro if (!off && !size) { 8782c855de9SAl Viro pipe_discard_from(pipe, i->start_head); // discard everything 8792c855de9SAl Viro return; 880b9dc6f65SAl Viro } 881b9dc6f65SAl Viro i->count -= size; 8822c855de9SAl Viro while (1) { 8832c855de9SAl Viro struct pipe_buffer *buf = pipe_buf(pipe, i->head); 8842c855de9SAl Viro if (off) /* make it relative to the beginning of buffer */ 88510f525a8SAl Viro size += abs(off) - buf->offset; 8862c855de9SAl Viro if (size <= buf->len) { 8872c855de9SAl Viro buf->len = size; 88810f525a8SAl Viro i->last_offset = last_offset(buf); 8892c855de9SAl Viro break; 8902c855de9SAl Viro } 8912c855de9SAl Viro size -= buf->len; 8922c855de9SAl Viro i->head++; 8932c855de9SAl Viro off = 0; 8942c855de9SAl Viro } 8952c855de9SAl Viro pipe_discard_from(pipe, i->head + 1); // discard everything past this one 896241699cdSAl Viro } 897241699cdSAl Viro 89854c8195bSPavel Begunkov static void iov_iter_bvec_advance(struct iov_iter *i, size_t size) 89954c8195bSPavel Begunkov { 90018fa9af7SAl Viro const struct bio_vec *bvec, *end; 90154c8195bSPavel Begunkov 90218fa9af7SAl Viro if (!i->count) 90318fa9af7SAl Viro return; 90418fa9af7SAl Viro i->count -= size; 90554c8195bSPavel Begunkov 90618fa9af7SAl Viro size += i->iov_offset; 90718fa9af7SAl Viro 90818fa9af7SAl Viro for (bvec = i->bvec, end = bvec + i->nr_segs; bvec < end; bvec++) { 90918fa9af7SAl Viro if (likely(size < bvec->bv_len)) 91018fa9af7SAl Viro break; 91118fa9af7SAl Viro size -= bvec->bv_len; 91218fa9af7SAl Viro } 91318fa9af7SAl Viro i->iov_offset = size; 91418fa9af7SAl Viro i->nr_segs -= bvec - i->bvec; 91518fa9af7SAl Viro i->bvec = bvec; 91654c8195bSPavel Begunkov } 91754c8195bSPavel Begunkov 918185ac4d4SAl Viro static void iov_iter_iovec_advance(struct iov_iter *i, size_t size) 919185ac4d4SAl Viro { 920185ac4d4SAl Viro const struct iovec *iov, *end; 921185ac4d4SAl Viro 922185ac4d4SAl Viro if (!i->count) 923185ac4d4SAl Viro return; 924185ac4d4SAl Viro i->count -= size; 925185ac4d4SAl Viro 926185ac4d4SAl Viro size += i->iov_offset; // from beginning of current segment 927185ac4d4SAl Viro for (iov = i->iov, end = iov + i->nr_segs; iov < end; iov++) { 928185ac4d4SAl Viro if (likely(size < iov->iov_len)) 929185ac4d4SAl Viro break; 930185ac4d4SAl Viro size -= iov->iov_len; 931185ac4d4SAl Viro } 932185ac4d4SAl Viro i->iov_offset = size; 933185ac4d4SAl Viro i->nr_segs -= iov - i->iov; 934185ac4d4SAl Viro i->iov = iov; 935185ac4d4SAl Viro } 936185ac4d4SAl Viro 937d879cb83SAl Viro void iov_iter_advance(struct iov_iter *i, size_t size) 938d879cb83SAl Viro { 9393b3fc051SAl Viro if (unlikely(i->count < size)) 9403b3fc051SAl Viro size = i->count; 941fcb14cb1SAl Viro if (likely(iter_is_ubuf(i)) || unlikely(iov_iter_is_xarray(i))) { 942fcb14cb1SAl Viro i->iov_offset += size; 943fcb14cb1SAl Viro i->count -= size; 944fcb14cb1SAl Viro } else if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i))) { 945185ac4d4SAl Viro /* iovec and kvec have identical layouts */ 946185ac4d4SAl Viro iov_iter_iovec_advance(i, size); 947185ac4d4SAl Viro } else if (iov_iter_is_bvec(i)) { 948185ac4d4SAl Viro iov_iter_bvec_advance(i, size); 949185ac4d4SAl Viro } else if (iov_iter_is_pipe(i)) { 950241699cdSAl Viro pipe_advance(i, size); 951185ac4d4SAl Viro } else if (iov_iter_is_discard(i)) { 952185ac4d4SAl Viro i->count -= size; 9537ff50620SDavid Howells } 954d879cb83SAl Viro } 955d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_advance); 956d879cb83SAl Viro 95727c0e374SAl Viro void iov_iter_revert(struct iov_iter *i, size_t unroll) 95827c0e374SAl Viro { 95927c0e374SAl Viro if (!unroll) 96027c0e374SAl Viro return; 9615b47d59aSAl Viro if (WARN_ON(unroll > MAX_RW_COUNT)) 9625b47d59aSAl Viro return; 96327c0e374SAl Viro i->count += unroll; 96400e23707SDavid Howells if (unlikely(iov_iter_is_pipe(i))) { 96527c0e374SAl Viro struct pipe_inode_info *pipe = i->pipe; 96692acdc4fSAl Viro unsigned int head = pipe->head; 96792acdc4fSAl Viro 96892acdc4fSAl Viro while (head > i->start_head) { 96992acdc4fSAl Viro struct pipe_buffer *b = pipe_buf(pipe, --head); 97092acdc4fSAl Viro if (unroll < b->len) { 97192acdc4fSAl Viro b->len -= unroll; 97210f525a8SAl Viro i->last_offset = last_offset(b); 97392acdc4fSAl Viro i->head = head; 97492acdc4fSAl Viro return; 97527c0e374SAl Viro } 97692acdc4fSAl Viro unroll -= b->len; 97792acdc4fSAl Viro pipe_buf_release(pipe, b); 97892acdc4fSAl Viro pipe->head--; 97927c0e374SAl Viro } 98010f525a8SAl Viro i->last_offset = 0; 98192acdc4fSAl Viro i->head = head; 98227c0e374SAl Viro return; 98327c0e374SAl Viro } 9849ea9ce04SDavid Howells if (unlikely(iov_iter_is_discard(i))) 9859ea9ce04SDavid Howells return; 98627c0e374SAl Viro if (unroll <= i->iov_offset) { 98727c0e374SAl Viro i->iov_offset -= unroll; 98827c0e374SAl Viro return; 98927c0e374SAl Viro } 99027c0e374SAl Viro unroll -= i->iov_offset; 991fcb14cb1SAl Viro if (iov_iter_is_xarray(i) || iter_is_ubuf(i)) { 9927ff50620SDavid Howells BUG(); /* We should never go beyond the start of the specified 9937ff50620SDavid Howells * range since we might then be straying into pages that 9947ff50620SDavid Howells * aren't pinned. 9957ff50620SDavid Howells */ 9967ff50620SDavid Howells } else if (iov_iter_is_bvec(i)) { 99727c0e374SAl Viro const struct bio_vec *bvec = i->bvec; 99827c0e374SAl Viro while (1) { 99927c0e374SAl Viro size_t n = (--bvec)->bv_len; 100027c0e374SAl Viro i->nr_segs++; 100127c0e374SAl Viro if (unroll <= n) { 100227c0e374SAl Viro i->bvec = bvec; 100327c0e374SAl Viro i->iov_offset = n - unroll; 100427c0e374SAl Viro return; 100527c0e374SAl Viro } 100627c0e374SAl Viro unroll -= n; 100727c0e374SAl Viro } 100827c0e374SAl Viro } else { /* same logics for iovec and kvec */ 100927c0e374SAl Viro const struct iovec *iov = i->iov; 101027c0e374SAl Viro while (1) { 101127c0e374SAl Viro size_t n = (--iov)->iov_len; 101227c0e374SAl Viro i->nr_segs++; 101327c0e374SAl Viro if (unroll <= n) { 101427c0e374SAl Viro i->iov = iov; 101527c0e374SAl Viro i->iov_offset = n - unroll; 101627c0e374SAl Viro return; 101727c0e374SAl Viro } 101827c0e374SAl Viro unroll -= n; 101927c0e374SAl Viro } 102027c0e374SAl Viro } 102127c0e374SAl Viro } 102227c0e374SAl Viro EXPORT_SYMBOL(iov_iter_revert); 102327c0e374SAl Viro 1024d879cb83SAl Viro /* 1025d879cb83SAl Viro * Return the count of just the current iov_iter segment. 1026d879cb83SAl Viro */ 1027d879cb83SAl Viro size_t iov_iter_single_seg_count(const struct iov_iter *i) 1028d879cb83SAl Viro { 102928f38db7SAl Viro if (i->nr_segs > 1) { 103028f38db7SAl Viro if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i))) 103128f38db7SAl Viro return min(i->count, i->iov->iov_len - i->iov_offset); 10327ff50620SDavid Howells if (iov_iter_is_bvec(i)) 1033d879cb83SAl Viro return min(i->count, i->bvec->bv_len - i->iov_offset); 103428f38db7SAl Viro } 103528f38db7SAl Viro return i->count; 1036d879cb83SAl Viro } 1037d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_single_seg_count); 1038d879cb83SAl Viro 1039aa563d7bSDavid Howells void iov_iter_kvec(struct iov_iter *i, unsigned int direction, 1040d879cb83SAl Viro const struct kvec *kvec, unsigned long nr_segs, 1041d879cb83SAl Viro size_t count) 1042d879cb83SAl Viro { 1043aa563d7bSDavid Howells WARN_ON(direction & ~(READ | WRITE)); 10448cd54c1cSAl Viro *i = (struct iov_iter){ 10458cd54c1cSAl Viro .iter_type = ITER_KVEC, 10468cd54c1cSAl Viro .data_source = direction, 10478cd54c1cSAl Viro .kvec = kvec, 10488cd54c1cSAl Viro .nr_segs = nr_segs, 10498cd54c1cSAl Viro .iov_offset = 0, 10508cd54c1cSAl Viro .count = count 10518cd54c1cSAl Viro }; 1052d879cb83SAl Viro } 1053d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_kvec); 1054d879cb83SAl Viro 1055aa563d7bSDavid Howells void iov_iter_bvec(struct iov_iter *i, unsigned int direction, 1056d879cb83SAl Viro const struct bio_vec *bvec, unsigned long nr_segs, 1057d879cb83SAl Viro size_t count) 1058d879cb83SAl Viro { 1059aa563d7bSDavid Howells WARN_ON(direction & ~(READ | WRITE)); 10608cd54c1cSAl Viro *i = (struct iov_iter){ 10618cd54c1cSAl Viro .iter_type = ITER_BVEC, 10628cd54c1cSAl Viro .data_source = direction, 10638cd54c1cSAl Viro .bvec = bvec, 10648cd54c1cSAl Viro .nr_segs = nr_segs, 10658cd54c1cSAl Viro .iov_offset = 0, 10668cd54c1cSAl Viro .count = count 10678cd54c1cSAl Viro }; 1068d879cb83SAl Viro } 1069d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_bvec); 1070d879cb83SAl Viro 1071aa563d7bSDavid Howells void iov_iter_pipe(struct iov_iter *i, unsigned int direction, 1072241699cdSAl Viro struct pipe_inode_info *pipe, 1073241699cdSAl Viro size_t count) 1074241699cdSAl Viro { 1075aa563d7bSDavid Howells BUG_ON(direction != READ); 10768cefc107SDavid Howells WARN_ON(pipe_full(pipe->head, pipe->tail, pipe->ring_size)); 10778cd54c1cSAl Viro *i = (struct iov_iter){ 10788cd54c1cSAl Viro .iter_type = ITER_PIPE, 10798cd54c1cSAl Viro .data_source = false, 10808cd54c1cSAl Viro .pipe = pipe, 10818cd54c1cSAl Viro .head = pipe->head, 10828cd54c1cSAl Viro .start_head = pipe->head, 108310f525a8SAl Viro .last_offset = 0, 10848cd54c1cSAl Viro .count = count 10858cd54c1cSAl Viro }; 1086241699cdSAl Viro } 1087241699cdSAl Viro EXPORT_SYMBOL(iov_iter_pipe); 1088241699cdSAl Viro 10899ea9ce04SDavid Howells /** 10907ff50620SDavid Howells * iov_iter_xarray - Initialise an I/O iterator to use the pages in an xarray 10917ff50620SDavid Howells * @i: The iterator to initialise. 10927ff50620SDavid Howells * @direction: The direction of the transfer. 10937ff50620SDavid Howells * @xarray: The xarray to access. 10947ff50620SDavid Howells * @start: The start file position. 10957ff50620SDavid Howells * @count: The size of the I/O buffer in bytes. 10967ff50620SDavid Howells * 10977ff50620SDavid Howells * Set up an I/O iterator to either draw data out of the pages attached to an 10987ff50620SDavid Howells * inode or to inject data into those pages. The pages *must* be prevented 10997ff50620SDavid Howells * from evaporation, either by taking a ref on them or locking them by the 11007ff50620SDavid Howells * caller. 11017ff50620SDavid Howells */ 11027ff50620SDavid Howells void iov_iter_xarray(struct iov_iter *i, unsigned int direction, 11037ff50620SDavid Howells struct xarray *xarray, loff_t start, size_t count) 11047ff50620SDavid Howells { 11057ff50620SDavid Howells BUG_ON(direction & ~1); 11068cd54c1cSAl Viro *i = (struct iov_iter) { 11078cd54c1cSAl Viro .iter_type = ITER_XARRAY, 11088cd54c1cSAl Viro .data_source = direction, 11098cd54c1cSAl Viro .xarray = xarray, 11108cd54c1cSAl Viro .xarray_start = start, 11118cd54c1cSAl Viro .count = count, 11128cd54c1cSAl Viro .iov_offset = 0 11138cd54c1cSAl Viro }; 11147ff50620SDavid Howells } 11157ff50620SDavid Howells EXPORT_SYMBOL(iov_iter_xarray); 11167ff50620SDavid Howells 11177ff50620SDavid Howells /** 11189ea9ce04SDavid Howells * iov_iter_discard - Initialise an I/O iterator that discards data 11199ea9ce04SDavid Howells * @i: The iterator to initialise. 11209ea9ce04SDavid Howells * @direction: The direction of the transfer. 11219ea9ce04SDavid Howells * @count: The size of the I/O buffer in bytes. 11229ea9ce04SDavid Howells * 11239ea9ce04SDavid Howells * Set up an I/O iterator that just discards everything that's written to it. 11249ea9ce04SDavid Howells * It's only available as a READ iterator. 11259ea9ce04SDavid Howells */ 11269ea9ce04SDavid Howells void iov_iter_discard(struct iov_iter *i, unsigned int direction, size_t count) 11279ea9ce04SDavid Howells { 11289ea9ce04SDavid Howells BUG_ON(direction != READ); 11298cd54c1cSAl Viro *i = (struct iov_iter){ 11308cd54c1cSAl Viro .iter_type = ITER_DISCARD, 11318cd54c1cSAl Viro .data_source = false, 11328cd54c1cSAl Viro .count = count, 11338cd54c1cSAl Viro .iov_offset = 0 11348cd54c1cSAl Viro }; 11359ea9ce04SDavid Howells } 11369ea9ce04SDavid Howells EXPORT_SYMBOL(iov_iter_discard); 11379ea9ce04SDavid Howells 1138cfa320f7SKeith Busch static bool iov_iter_aligned_iovec(const struct iov_iter *i, unsigned addr_mask, 1139cfa320f7SKeith Busch unsigned len_mask) 1140cfa320f7SKeith Busch { 1141cfa320f7SKeith Busch size_t size = i->count; 1142cfa320f7SKeith Busch size_t skip = i->iov_offset; 1143cfa320f7SKeith Busch unsigned k; 1144cfa320f7SKeith Busch 1145cfa320f7SKeith Busch for (k = 0; k < i->nr_segs; k++, skip = 0) { 1146cfa320f7SKeith Busch size_t len = i->iov[k].iov_len - skip; 1147cfa320f7SKeith Busch 1148cfa320f7SKeith Busch if (len > size) 1149cfa320f7SKeith Busch len = size; 1150cfa320f7SKeith Busch if (len & len_mask) 1151cfa320f7SKeith Busch return false; 1152cfa320f7SKeith Busch if ((unsigned long)(i->iov[k].iov_base + skip) & addr_mask) 1153cfa320f7SKeith Busch return false; 1154cfa320f7SKeith Busch 1155cfa320f7SKeith Busch size -= len; 1156cfa320f7SKeith Busch if (!size) 1157cfa320f7SKeith Busch break; 1158cfa320f7SKeith Busch } 1159cfa320f7SKeith Busch return true; 1160cfa320f7SKeith Busch } 1161cfa320f7SKeith Busch 1162cfa320f7SKeith Busch static bool iov_iter_aligned_bvec(const struct iov_iter *i, unsigned addr_mask, 1163cfa320f7SKeith Busch unsigned len_mask) 1164cfa320f7SKeith Busch { 1165cfa320f7SKeith Busch size_t size = i->count; 1166cfa320f7SKeith Busch unsigned skip = i->iov_offset; 1167cfa320f7SKeith Busch unsigned k; 1168cfa320f7SKeith Busch 1169cfa320f7SKeith Busch for (k = 0; k < i->nr_segs; k++, skip = 0) { 1170cfa320f7SKeith Busch size_t len = i->bvec[k].bv_len - skip; 1171cfa320f7SKeith Busch 1172cfa320f7SKeith Busch if (len > size) 1173cfa320f7SKeith Busch len = size; 1174cfa320f7SKeith Busch if (len & len_mask) 1175cfa320f7SKeith Busch return false; 1176cfa320f7SKeith Busch if ((unsigned long)(i->bvec[k].bv_offset + skip) & addr_mask) 1177cfa320f7SKeith Busch return false; 1178cfa320f7SKeith Busch 1179cfa320f7SKeith Busch size -= len; 1180cfa320f7SKeith Busch if (!size) 1181cfa320f7SKeith Busch break; 1182cfa320f7SKeith Busch } 1183cfa320f7SKeith Busch return true; 1184cfa320f7SKeith Busch } 1185cfa320f7SKeith Busch 1186cfa320f7SKeith Busch /** 1187cfa320f7SKeith Busch * iov_iter_is_aligned() - Check if the addresses and lengths of each segments 1188cfa320f7SKeith Busch * are aligned to the parameters. 1189cfa320f7SKeith Busch * 1190cfa320f7SKeith Busch * @i: &struct iov_iter to restore 1191cfa320f7SKeith Busch * @addr_mask: bit mask to check against the iov element's addresses 1192cfa320f7SKeith Busch * @len_mask: bit mask to check against the iov element's lengths 1193cfa320f7SKeith Busch * 1194cfa320f7SKeith Busch * Return: false if any addresses or lengths intersect with the provided masks 1195cfa320f7SKeith Busch */ 1196cfa320f7SKeith Busch bool iov_iter_is_aligned(const struct iov_iter *i, unsigned addr_mask, 1197cfa320f7SKeith Busch unsigned len_mask) 1198cfa320f7SKeith Busch { 1199fcb14cb1SAl Viro if (likely(iter_is_ubuf(i))) { 1200fcb14cb1SAl Viro if (i->count & len_mask) 1201fcb14cb1SAl Viro return false; 1202fcb14cb1SAl Viro if ((unsigned long)(i->ubuf + i->iov_offset) & addr_mask) 1203fcb14cb1SAl Viro return false; 1204fcb14cb1SAl Viro return true; 1205fcb14cb1SAl Viro } 1206fcb14cb1SAl Viro 1207cfa320f7SKeith Busch if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i))) 1208cfa320f7SKeith Busch return iov_iter_aligned_iovec(i, addr_mask, len_mask); 1209cfa320f7SKeith Busch 1210cfa320f7SKeith Busch if (iov_iter_is_bvec(i)) 1211cfa320f7SKeith Busch return iov_iter_aligned_bvec(i, addr_mask, len_mask); 1212cfa320f7SKeith Busch 1213cfa320f7SKeith Busch if (iov_iter_is_pipe(i)) { 1214cfa320f7SKeith Busch size_t size = i->count; 1215cfa320f7SKeith Busch 1216cfa320f7SKeith Busch if (size & len_mask) 1217cfa320f7SKeith Busch return false; 121810f525a8SAl Viro if (size && i->last_offset > 0) { 121910f525a8SAl Viro if (i->last_offset & addr_mask) 1220cfa320f7SKeith Busch return false; 1221cfa320f7SKeith Busch } 1222cfa320f7SKeith Busch 1223cfa320f7SKeith Busch return true; 1224cfa320f7SKeith Busch } 1225cfa320f7SKeith Busch 1226cfa320f7SKeith Busch if (iov_iter_is_xarray(i)) { 1227cfa320f7SKeith Busch if (i->count & len_mask) 1228cfa320f7SKeith Busch return false; 1229cfa320f7SKeith Busch if ((i->xarray_start + i->iov_offset) & addr_mask) 1230cfa320f7SKeith Busch return false; 1231cfa320f7SKeith Busch } 1232cfa320f7SKeith Busch 1233cfa320f7SKeith Busch return true; 1234cfa320f7SKeith Busch } 1235cfa320f7SKeith Busch EXPORT_SYMBOL_GPL(iov_iter_is_aligned); 1236cfa320f7SKeith Busch 12379221d2e3SAl Viro static unsigned long iov_iter_alignment_iovec(const struct iov_iter *i) 1238d879cb83SAl Viro { 1239d879cb83SAl Viro unsigned long res = 0; 1240d879cb83SAl Viro size_t size = i->count; 12419221d2e3SAl Viro size_t skip = i->iov_offset; 12429221d2e3SAl Viro unsigned k; 1243d879cb83SAl Viro 12449221d2e3SAl Viro for (k = 0; k < i->nr_segs; k++, skip = 0) { 12459221d2e3SAl Viro size_t len = i->iov[k].iov_len - skip; 12469221d2e3SAl Viro if (len) { 12479221d2e3SAl Viro res |= (unsigned long)i->iov[k].iov_base + skip; 12489221d2e3SAl Viro if (len > size) 12499221d2e3SAl Viro len = size; 12509221d2e3SAl Viro res |= len; 12519221d2e3SAl Viro size -= len; 12529221d2e3SAl Viro if (!size) 12539221d2e3SAl Viro break; 12549221d2e3SAl Viro } 12559221d2e3SAl Viro } 12569221d2e3SAl Viro return res; 12579221d2e3SAl Viro } 12589221d2e3SAl Viro 12599221d2e3SAl Viro static unsigned long iov_iter_alignment_bvec(const struct iov_iter *i) 12609221d2e3SAl Viro { 12619221d2e3SAl Viro unsigned res = 0; 12629221d2e3SAl Viro size_t size = i->count; 12639221d2e3SAl Viro unsigned skip = i->iov_offset; 12649221d2e3SAl Viro unsigned k; 12659221d2e3SAl Viro 12669221d2e3SAl Viro for (k = 0; k < i->nr_segs; k++, skip = 0) { 12679221d2e3SAl Viro size_t len = i->bvec[k].bv_len - skip; 12689221d2e3SAl Viro res |= (unsigned long)i->bvec[k].bv_offset + skip; 12699221d2e3SAl Viro if (len > size) 12709221d2e3SAl Viro len = size; 12719221d2e3SAl Viro res |= len; 12729221d2e3SAl Viro size -= len; 12739221d2e3SAl Viro if (!size) 12749221d2e3SAl Viro break; 12759221d2e3SAl Viro } 12769221d2e3SAl Viro return res; 12779221d2e3SAl Viro } 12789221d2e3SAl Viro 12799221d2e3SAl Viro unsigned long iov_iter_alignment(const struct iov_iter *i) 12809221d2e3SAl Viro { 1281fcb14cb1SAl Viro if (likely(iter_is_ubuf(i))) { 1282fcb14cb1SAl Viro size_t size = i->count; 1283fcb14cb1SAl Viro if (size) 1284fcb14cb1SAl Viro return ((unsigned long)i->ubuf + i->iov_offset) | size; 1285fcb14cb1SAl Viro return 0; 1286fcb14cb1SAl Viro } 1287fcb14cb1SAl Viro 12889221d2e3SAl Viro /* iovec and kvec have identical layouts */ 12899221d2e3SAl Viro if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i))) 12909221d2e3SAl Viro return iov_iter_alignment_iovec(i); 12919221d2e3SAl Viro 12929221d2e3SAl Viro if (iov_iter_is_bvec(i)) 12939221d2e3SAl Viro return iov_iter_alignment_bvec(i); 12949221d2e3SAl Viro 12959221d2e3SAl Viro if (iov_iter_is_pipe(i)) { 12969221d2e3SAl Viro size_t size = i->count; 1297e0ff126eSJan Kara 129810f525a8SAl Viro if (size && i->last_offset > 0) 129910f525a8SAl Viro return size | i->last_offset; 1300241699cdSAl Viro return size; 1301241699cdSAl Viro } 13029221d2e3SAl Viro 13039221d2e3SAl Viro if (iov_iter_is_xarray(i)) 13043d14ec1fSDavid Howells return (i->xarray_start + i->iov_offset) | i->count; 13059221d2e3SAl Viro 13069221d2e3SAl Viro return 0; 1307d879cb83SAl Viro } 1308d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_alignment); 1309d879cb83SAl Viro 1310357f435dSAl Viro unsigned long iov_iter_gap_alignment(const struct iov_iter *i) 1311357f435dSAl Viro { 1312357f435dSAl Viro unsigned long res = 0; 1313610c7a71SAl Viro unsigned long v = 0; 1314357f435dSAl Viro size_t size = i->count; 1315610c7a71SAl Viro unsigned k; 1316357f435dSAl Viro 1317fcb14cb1SAl Viro if (iter_is_ubuf(i)) 1318fcb14cb1SAl Viro return 0; 1319fcb14cb1SAl Viro 1320610c7a71SAl Viro if (WARN_ON(!iter_is_iovec(i))) 1321241699cdSAl Viro return ~0U; 1322241699cdSAl Viro 1323610c7a71SAl Viro for (k = 0; k < i->nr_segs; k++) { 1324610c7a71SAl Viro if (i->iov[k].iov_len) { 1325610c7a71SAl Viro unsigned long base = (unsigned long)i->iov[k].iov_base; 1326610c7a71SAl Viro if (v) // if not the first one 1327610c7a71SAl Viro res |= base | v; // this start | previous end 1328610c7a71SAl Viro v = base + i->iov[k].iov_len; 1329610c7a71SAl Viro if (size <= i->iov[k].iov_len) 1330610c7a71SAl Viro break; 1331610c7a71SAl Viro size -= i->iov[k].iov_len; 1332610c7a71SAl Viro } 1333610c7a71SAl Viro } 1334357f435dSAl Viro return res; 1335357f435dSAl Viro } 1336357f435dSAl Viro EXPORT_SYMBOL(iov_iter_gap_alignment); 1337357f435dSAl Viro 13383cf42da3SAl Viro static int want_pages_array(struct page ***res, size_t size, 13393cf42da3SAl Viro size_t start, unsigned int maxpages) 1340acbdeb83SAl Viro { 13413cf42da3SAl Viro unsigned int count = DIV_ROUND_UP(size + start, PAGE_SIZE); 13423cf42da3SAl Viro 13433cf42da3SAl Viro if (count > maxpages) 13443cf42da3SAl Viro count = maxpages; 13453cf42da3SAl Viro WARN_ON(!count); // caller should've prevented that 13463cf42da3SAl Viro if (!*res) { 13473cf42da3SAl Viro *res = kvmalloc_array(count, sizeof(struct page *), GFP_KERNEL); 13483cf42da3SAl Viro if (!*res) 13493cf42da3SAl Viro return 0; 13503cf42da3SAl Viro } 13513cf42da3SAl Viro return count; 1352acbdeb83SAl Viro } 1353acbdeb83SAl Viro 135485200084SAl Viro static ssize_t pipe_get_pages(struct iov_iter *i, 135585200084SAl Viro struct page ***pages, size_t maxsize, unsigned maxpages, 135685200084SAl Viro size_t *start) 1357241699cdSAl Viro { 1358746de1f8SAl Viro unsigned int npages, count, off, chunk; 135985200084SAl Viro struct page **p; 1360746de1f8SAl Viro size_t left; 1361241699cdSAl Viro 136285200084SAl Viro if (!sanity(i)) 136385200084SAl Viro return -EFAULT; 136485200084SAl Viro 136585200084SAl Viro *start = off = pipe_npages(i, &npages); 13663cf42da3SAl Viro if (!npages) 13673cf42da3SAl Viro return -EFAULT; 13683cf42da3SAl Viro count = want_pages_array(pages, maxsize, off, min(npages, maxpages)); 13693cf42da3SAl Viro if (!count) 137085200084SAl Viro return -ENOMEM; 13713cf42da3SAl Viro p = *pages; 1372746de1f8SAl Viro for (npages = 0, left = maxsize ; npages < count; npages++, left -= chunk) { 1373746de1f8SAl Viro struct page *page = append_pipe(i, left, &off); 1374e3b42964SAl Viro if (!page) 1375e3b42964SAl Viro break; 1376746de1f8SAl Viro chunk = min_t(size_t, left, PAGE_SIZE - off); 137785200084SAl Viro get_page(*p++ = page); 1378e3b42964SAl Viro } 137985200084SAl Viro if (!npages) 1380241699cdSAl Viro return -EFAULT; 1381746de1f8SAl Viro return maxsize - left; 1382241699cdSAl Viro } 1383241699cdSAl Viro 13847ff50620SDavid Howells static ssize_t iter_xarray_populate_pages(struct page **pages, struct xarray *xa, 13857ff50620SDavid Howells pgoff_t index, unsigned int nr_pages) 13867ff50620SDavid Howells { 13877ff50620SDavid Howells XA_STATE(xas, xa, index); 13887ff50620SDavid Howells struct page *page; 13897ff50620SDavid Howells unsigned int ret = 0; 13907ff50620SDavid Howells 13917ff50620SDavid Howells rcu_read_lock(); 13927ff50620SDavid Howells for (page = xas_load(&xas); page; page = xas_next(&xas)) { 13937ff50620SDavid Howells if (xas_retry(&xas, page)) 13947ff50620SDavid Howells continue; 13957ff50620SDavid Howells 13967ff50620SDavid Howells /* Has the page moved or been split? */ 13977ff50620SDavid Howells if (unlikely(page != xas_reload(&xas))) { 13987ff50620SDavid Howells xas_reset(&xas); 13997ff50620SDavid Howells continue; 14007ff50620SDavid Howells } 14017ff50620SDavid Howells 14027ff50620SDavid Howells pages[ret] = find_subpage(page, xas.xa_index); 14037ff50620SDavid Howells get_page(pages[ret]); 14047ff50620SDavid Howells if (++ret == nr_pages) 14057ff50620SDavid Howells break; 14067ff50620SDavid Howells } 14077ff50620SDavid Howells rcu_read_unlock(); 14087ff50620SDavid Howells return ret; 14097ff50620SDavid Howells } 14107ff50620SDavid Howells 14117ff50620SDavid Howells static ssize_t iter_xarray_get_pages(struct iov_iter *i, 141268fe506fSAl Viro struct page ***pages, size_t maxsize, 14137ff50620SDavid Howells unsigned maxpages, size_t *_start_offset) 14147ff50620SDavid Howells { 14153cf42da3SAl Viro unsigned nr, offset, count; 14163cf42da3SAl Viro pgoff_t index; 14177ff50620SDavid Howells loff_t pos; 14187ff50620SDavid Howells 14197ff50620SDavid Howells pos = i->xarray_start + i->iov_offset; 14207ff50620SDavid Howells index = pos >> PAGE_SHIFT; 14217ff50620SDavid Howells offset = pos & ~PAGE_MASK; 14227ff50620SDavid Howells *_start_offset = offset; 14237ff50620SDavid Howells 14243cf42da3SAl Viro count = want_pages_array(pages, maxsize, offset, maxpages); 14253cf42da3SAl Viro if (!count) 142668fe506fSAl Viro return -ENOMEM; 142768fe506fSAl Viro nr = iter_xarray_populate_pages(*pages, i->xarray, index, count); 14287ff50620SDavid Howells if (nr == 0) 14297ff50620SDavid Howells return 0; 14307ff50620SDavid Howells 1431eba2d3d7SAl Viro maxsize = min_t(size_t, nr * PAGE_SIZE - offset, maxsize); 1432310d9d5aSAl Viro i->iov_offset += maxsize; 1433310d9d5aSAl Viro i->count -= maxsize; 1434eba2d3d7SAl Viro return maxsize; 14357ff50620SDavid Howells } 14367ff50620SDavid Howells 1437fcb14cb1SAl Viro /* must be done on non-empty ITER_UBUF or ITER_IOVEC one */ 1438dd45ab9dSAl Viro static unsigned long first_iovec_segment(const struct iov_iter *i, size_t *size) 14393d671ca6SAl Viro { 14403d671ca6SAl Viro size_t skip; 14413d671ca6SAl Viro long k; 14423d671ca6SAl Viro 1443fcb14cb1SAl Viro if (iter_is_ubuf(i)) 1444fcb14cb1SAl Viro return (unsigned long)i->ubuf + i->iov_offset; 1445fcb14cb1SAl Viro 14463d671ca6SAl Viro for (k = 0, skip = i->iov_offset; k < i->nr_segs; k++, skip = 0) { 14473d671ca6SAl Viro size_t len = i->iov[k].iov_len - skip; 14483d671ca6SAl Viro 14493d671ca6SAl Viro if (unlikely(!len)) 14503d671ca6SAl Viro continue; 145159dbd7d0SAl Viro if (*size > len) 14523d671ca6SAl Viro *size = len; 1453dd45ab9dSAl Viro return (unsigned long)i->iov[k].iov_base + skip; 14543d671ca6SAl Viro } 14553d671ca6SAl Viro BUG(); // if it had been empty, we wouldn't get called 14563d671ca6SAl Viro } 14573d671ca6SAl Viro 14583d671ca6SAl Viro /* must be done on non-empty ITER_BVEC one */ 14593d671ca6SAl Viro static struct page *first_bvec_segment(const struct iov_iter *i, 146059dbd7d0SAl Viro size_t *size, size_t *start) 14613d671ca6SAl Viro { 14623d671ca6SAl Viro struct page *page; 14633d671ca6SAl Viro size_t skip = i->iov_offset, len; 14643d671ca6SAl Viro 14653d671ca6SAl Viro len = i->bvec->bv_len - skip; 146659dbd7d0SAl Viro if (*size > len) 146759dbd7d0SAl Viro *size = len; 14683d671ca6SAl Viro skip += i->bvec->bv_offset; 14693d671ca6SAl Viro page = i->bvec->bv_page + skip / PAGE_SIZE; 1470dda8e5d1SAl Viro *start = skip % PAGE_SIZE; 14713d671ca6SAl Viro return page; 14723d671ca6SAl Viro } 14733d671ca6SAl Viro 147491329559SAl Viro static ssize_t __iov_iter_get_pages_alloc(struct iov_iter *i, 1475d879cb83SAl Viro struct page ***pages, size_t maxsize, 1476d8207640SLogan Gunthorpe unsigned int maxpages, size_t *start, 1477f62e52d1SDavid Howells iov_iter_extraction_t extraction_flags) 1478d879cb83SAl Viro { 1479f62e52d1SDavid Howells unsigned int n, gup_flags = 0; 1480d879cb83SAl Viro 1481d879cb83SAl Viro if (maxsize > i->count) 1482d879cb83SAl Viro maxsize = i->count; 14833d671ca6SAl Viro if (!maxsize) 14843d671ca6SAl Viro return 0; 14857392ed17SAl Viro if (maxsize > MAX_RW_COUNT) 14867392ed17SAl Viro maxsize = MAX_RW_COUNT; 1487f62e52d1SDavid Howells if (extraction_flags & ITER_ALLOW_P2PDMA) 1488f62e52d1SDavid Howells gup_flags |= FOLL_PCI_P2PDMA; 1489d879cb83SAl Viro 1490fcb14cb1SAl Viro if (likely(user_backed_iter(i))) { 14913d671ca6SAl Viro unsigned long addr; 14923cf42da3SAl Viro int res; 14939ea9ce04SDavid Howells 14943337ab08SAndreas Gruenbacher if (iov_iter_rw(i) != WRITE) 14953337ab08SAndreas Gruenbacher gup_flags |= FOLL_WRITE; 14963337ab08SAndreas Gruenbacher if (i->nofault) 14973337ab08SAndreas Gruenbacher gup_flags |= FOLL_NOFAULT; 14983337ab08SAndreas Gruenbacher 1499dd45ab9dSAl Viro addr = first_iovec_segment(i, &maxsize); 1500dd45ab9dSAl Viro *start = addr % PAGE_SIZE; 1501dd45ab9dSAl Viro addr &= PAGE_MASK; 15023cf42da3SAl Viro n = want_pages_array(pages, maxsize, *start, maxpages); 15033cf42da3SAl Viro if (!n) 1504d879cb83SAl Viro return -ENOMEM; 1505451c0ba9SAl Viro res = get_user_pages_fast(addr, n, gup_flags, *pages); 150691329559SAl Viro if (unlikely(res <= 0)) 1507d879cb83SAl Viro return res; 1508eba2d3d7SAl Viro maxsize = min_t(size_t, maxsize, res * PAGE_SIZE - *start); 1509eba2d3d7SAl Viro iov_iter_advance(i, maxsize); 1510eba2d3d7SAl Viro return maxsize; 15113d671ca6SAl Viro } 15123d671ca6SAl Viro if (iov_iter_is_bvec(i)) { 1513451c0ba9SAl Viro struct page **p; 15143d671ca6SAl Viro struct page *page; 15153d671ca6SAl Viro 151659dbd7d0SAl Viro page = first_bvec_segment(i, &maxsize, start); 15173cf42da3SAl Viro n = want_pages_array(pages, maxsize, *start, maxpages); 15183cf42da3SAl Viro if (!n) 1519d879cb83SAl Viro return -ENOMEM; 15203cf42da3SAl Viro p = *pages; 1521dda8e5d1SAl Viro for (int k = 0; k < n; k++) 1522eba2d3d7SAl Viro get_page(p[k] = page + k); 1523eba2d3d7SAl Viro maxsize = min_t(size_t, maxsize, n * PAGE_SIZE - *start); 1524310d9d5aSAl Viro i->count -= maxsize; 1525310d9d5aSAl Viro i->iov_offset += maxsize; 1526310d9d5aSAl Viro if (i->iov_offset == i->bvec->bv_len) { 1527310d9d5aSAl Viro i->iov_offset = 0; 1528310d9d5aSAl Viro i->bvec++; 1529310d9d5aSAl Viro i->nr_segs--; 1530310d9d5aSAl Viro } 1531eba2d3d7SAl Viro return maxsize; 15323d671ca6SAl Viro } 15333d671ca6SAl Viro if (iov_iter_is_pipe(i)) 1534451c0ba9SAl Viro return pipe_get_pages(i, pages, maxsize, maxpages, start); 15353d671ca6SAl Viro if (iov_iter_is_xarray(i)) 1536451c0ba9SAl Viro return iter_xarray_get_pages(i, pages, maxsize, maxpages, start); 1537d879cb83SAl Viro return -EFAULT; 1538d879cb83SAl Viro } 153991329559SAl Viro 1540d8207640SLogan Gunthorpe ssize_t iov_iter_get_pages(struct iov_iter *i, 1541451c0ba9SAl Viro struct page **pages, size_t maxsize, unsigned maxpages, 1542f62e52d1SDavid Howells size_t *start, iov_iter_extraction_t extraction_flags) 1543451c0ba9SAl Viro { 1544451c0ba9SAl Viro if (!maxpages) 1545451c0ba9SAl Viro return 0; 1546451c0ba9SAl Viro BUG_ON(!pages); 1547451c0ba9SAl Viro 1548d8207640SLogan Gunthorpe return __iov_iter_get_pages_alloc(i, &pages, maxsize, maxpages, 1549f62e52d1SDavid Howells start, extraction_flags); 1550d8207640SLogan Gunthorpe } 1551d8207640SLogan Gunthorpe EXPORT_SYMBOL_GPL(iov_iter_get_pages); 1552d8207640SLogan Gunthorpe 1553d8207640SLogan Gunthorpe ssize_t iov_iter_get_pages2(struct iov_iter *i, struct page **pages, 1554d8207640SLogan Gunthorpe size_t maxsize, unsigned maxpages, size_t *start) 1555d8207640SLogan Gunthorpe { 1556d8207640SLogan Gunthorpe return iov_iter_get_pages(i, pages, maxsize, maxpages, start, 0); 1557451c0ba9SAl Viro } 1558eba2d3d7SAl Viro EXPORT_SYMBOL(iov_iter_get_pages2); 1559451c0ba9SAl Viro 1560d8207640SLogan Gunthorpe ssize_t iov_iter_get_pages_alloc(struct iov_iter *i, 156191329559SAl Viro struct page ***pages, size_t maxsize, 1562f62e52d1SDavid Howells size_t *start, iov_iter_extraction_t extraction_flags) 156391329559SAl Viro { 156491329559SAl Viro ssize_t len; 156591329559SAl Viro 156691329559SAl Viro *pages = NULL; 156791329559SAl Viro 1568d8207640SLogan Gunthorpe len = __iov_iter_get_pages_alloc(i, pages, maxsize, ~0U, start, 1569f62e52d1SDavid Howells extraction_flags); 157091329559SAl Viro if (len <= 0) { 157191329559SAl Viro kvfree(*pages); 157291329559SAl Viro *pages = NULL; 157391329559SAl Viro } 157491329559SAl Viro return len; 157591329559SAl Viro } 1576d8207640SLogan Gunthorpe EXPORT_SYMBOL_GPL(iov_iter_get_pages_alloc); 1577d8207640SLogan Gunthorpe 1578d8207640SLogan Gunthorpe ssize_t iov_iter_get_pages_alloc2(struct iov_iter *i, 1579d8207640SLogan Gunthorpe struct page ***pages, size_t maxsize, size_t *start) 1580d8207640SLogan Gunthorpe { 1581d8207640SLogan Gunthorpe return iov_iter_get_pages_alloc(i, pages, maxsize, start, 0); 1582d8207640SLogan Gunthorpe } 1583eba2d3d7SAl Viro EXPORT_SYMBOL(iov_iter_get_pages_alloc2); 1584d879cb83SAl Viro 1585d879cb83SAl Viro size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum, 1586d879cb83SAl Viro struct iov_iter *i) 1587d879cb83SAl Viro { 1588d879cb83SAl Viro __wsum sum, next; 1589d879cb83SAl Viro sum = *csum; 1590a41dad90SAl Viro if (WARN_ON_ONCE(!i->data_source)) 1591241699cdSAl Viro return 0; 1592a41dad90SAl Viro 15937baa5099SAl Viro iterate_and_advance(i, bytes, base, len, off, ({ 15947baa5099SAl Viro next = csum_and_copy_from_user(base, addr + off, len); 1595d879cb83SAl Viro sum = csum_block_add(sum, next, off); 15967baa5099SAl Viro next ? 0 : len; 1597d879cb83SAl Viro }), ({ 15987baa5099SAl Viro sum = csum_and_memcpy(addr + off, base, len, sum, off); 1599d879cb83SAl Viro }) 1600d879cb83SAl Viro ) 1601d879cb83SAl Viro *csum = sum; 1602d879cb83SAl Viro return bytes; 1603d879cb83SAl Viro } 1604d879cb83SAl Viro EXPORT_SYMBOL(csum_and_copy_from_iter); 1605d879cb83SAl Viro 160652cbd23aSWillem de Bruijn size_t csum_and_copy_to_iter(const void *addr, size_t bytes, void *_csstate, 1607d879cb83SAl Viro struct iov_iter *i) 1608d879cb83SAl Viro { 160952cbd23aSWillem de Bruijn struct csum_state *csstate = _csstate; 1610d879cb83SAl Viro __wsum sum, next; 161178e1f386SAl Viro 1612a41dad90SAl Viro if (WARN_ON_ONCE(i->data_source)) 1613a41dad90SAl Viro return 0; 161478e1f386SAl Viro if (unlikely(iov_iter_is_discard(i))) { 1615c67f1fd2SAl Viro // can't use csum_memcpy() for that one - data is not copied 1616c67f1fd2SAl Viro csstate->csum = csum_block_add(csstate->csum, 1617c67f1fd2SAl Viro csum_partial(addr, bytes, 0), 1618c67f1fd2SAl Viro csstate->off); 1619c67f1fd2SAl Viro csstate->off += bytes; 1620c67f1fd2SAl Viro return bytes; 1621241699cdSAl Viro } 16226852df12SAl Viro 16236852df12SAl Viro sum = csum_shift(csstate->csum, csstate->off); 16246852df12SAl Viro if (unlikely(iov_iter_is_pipe(i))) 16256852df12SAl Viro bytes = csum_and_copy_to_pipe_iter(addr, bytes, i, &sum); 16266852df12SAl Viro else iterate_and_advance(i, bytes, base, len, off, ({ 16277baa5099SAl Viro next = csum_and_copy_to_user(addr + off, base, len); 1628d879cb83SAl Viro sum = csum_block_add(sum, next, off); 16297baa5099SAl Viro next ? 0 : len; 1630d879cb83SAl Viro }), ({ 16317baa5099SAl Viro sum = csum_and_memcpy(base, addr + off, len, sum, off); 1632d879cb83SAl Viro }) 1633d879cb83SAl Viro ) 1634594e450bSAl Viro csstate->csum = csum_shift(sum, csstate->off); 1635594e450bSAl Viro csstate->off += bytes; 1636d879cb83SAl Viro return bytes; 1637d879cb83SAl Viro } 1638d879cb83SAl Viro EXPORT_SYMBOL(csum_and_copy_to_iter); 1639d879cb83SAl Viro 1640d05f4435SSagi Grimberg size_t hash_and_copy_to_iter(const void *addr, size_t bytes, void *hashp, 1641d05f4435SSagi Grimberg struct iov_iter *i) 1642d05f4435SSagi Grimberg { 16437999096fSHerbert Xu #ifdef CONFIG_CRYPTO_HASH 1644d05f4435SSagi Grimberg struct ahash_request *hash = hashp; 1645d05f4435SSagi Grimberg struct scatterlist sg; 1646d05f4435SSagi Grimberg size_t copied; 1647d05f4435SSagi Grimberg 1648d05f4435SSagi Grimberg copied = copy_to_iter(addr, bytes, i); 1649d05f4435SSagi Grimberg sg_init_one(&sg, addr, copied); 1650d05f4435SSagi Grimberg ahash_request_set_crypt(hash, &sg, NULL, copied); 1651d05f4435SSagi Grimberg crypto_ahash_update(hash); 1652d05f4435SSagi Grimberg return copied; 165327fad74aSYueHaibing #else 165427fad74aSYueHaibing return 0; 165527fad74aSYueHaibing #endif 1656d05f4435SSagi Grimberg } 1657d05f4435SSagi Grimberg EXPORT_SYMBOL(hash_and_copy_to_iter); 1658d05f4435SSagi Grimberg 165966531c65SAl Viro static int iov_npages(const struct iov_iter *i, int maxpages) 1660d879cb83SAl Viro { 166166531c65SAl Viro size_t skip = i->iov_offset, size = i->count; 166266531c65SAl Viro const struct iovec *p; 1663d879cb83SAl Viro int npages = 0; 1664d879cb83SAl Viro 166566531c65SAl Viro for (p = i->iov; size; skip = 0, p++) { 166666531c65SAl Viro unsigned offs = offset_in_page(p->iov_base + skip); 166766531c65SAl Viro size_t len = min(p->iov_len - skip, size); 1668d879cb83SAl Viro 166966531c65SAl Viro if (len) { 167066531c65SAl Viro size -= len; 167166531c65SAl Viro npages += DIV_ROUND_UP(offs + len, PAGE_SIZE); 167266531c65SAl Viro if (unlikely(npages > maxpages)) 167366531c65SAl Viro return maxpages; 167466531c65SAl Viro } 167566531c65SAl Viro } 167666531c65SAl Viro return npages; 167766531c65SAl Viro } 167866531c65SAl Viro 167966531c65SAl Viro static int bvec_npages(const struct iov_iter *i, int maxpages) 168066531c65SAl Viro { 168166531c65SAl Viro size_t skip = i->iov_offset, size = i->count; 168266531c65SAl Viro const struct bio_vec *p; 168366531c65SAl Viro int npages = 0; 168466531c65SAl Viro 168566531c65SAl Viro for (p = i->bvec; size; skip = 0, p++) { 168666531c65SAl Viro unsigned offs = (p->bv_offset + skip) % PAGE_SIZE; 168766531c65SAl Viro size_t len = min(p->bv_len - skip, size); 168866531c65SAl Viro 168966531c65SAl Viro size -= len; 169066531c65SAl Viro npages += DIV_ROUND_UP(offs + len, PAGE_SIZE); 169166531c65SAl Viro if (unlikely(npages > maxpages)) 169266531c65SAl Viro return maxpages; 169366531c65SAl Viro } 169466531c65SAl Viro return npages; 169566531c65SAl Viro } 169666531c65SAl Viro 169766531c65SAl Viro int iov_iter_npages(const struct iov_iter *i, int maxpages) 169866531c65SAl Viro { 169966531c65SAl Viro if (unlikely(!i->count)) 170066531c65SAl Viro return 0; 1701fcb14cb1SAl Viro if (likely(iter_is_ubuf(i))) { 1702fcb14cb1SAl Viro unsigned offs = offset_in_page(i->ubuf + i->iov_offset); 1703fcb14cb1SAl Viro int npages = DIV_ROUND_UP(offs + i->count, PAGE_SIZE); 1704fcb14cb1SAl Viro return min(npages, maxpages); 1705fcb14cb1SAl Viro } 170666531c65SAl Viro /* iovec and kvec have identical layouts */ 170766531c65SAl Viro if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i))) 170866531c65SAl Viro return iov_npages(i, maxpages); 170966531c65SAl Viro if (iov_iter_is_bvec(i)) 171066531c65SAl Viro return bvec_npages(i, maxpages); 171166531c65SAl Viro if (iov_iter_is_pipe(i)) { 171266531c65SAl Viro int npages; 1713241699cdSAl Viro 1714241699cdSAl Viro if (!sanity(i)) 1715241699cdSAl Viro return 0; 1716241699cdSAl Viro 171712d426abSAl Viro pipe_npages(i, &npages); 171866531c65SAl Viro return min(npages, maxpages); 171966531c65SAl Viro } 172066531c65SAl Viro if (iov_iter_is_xarray(i)) { 1721e4f8df86SAl Viro unsigned offset = (i->xarray_start + i->iov_offset) % PAGE_SIZE; 1722e4f8df86SAl Viro int npages = DIV_ROUND_UP(offset + i->count, PAGE_SIZE); 172366531c65SAl Viro return min(npages, maxpages); 172466531c65SAl Viro } 172566531c65SAl Viro return 0; 1726d879cb83SAl Viro } 1727d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_npages); 1728d879cb83SAl Viro 1729d879cb83SAl Viro const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags) 1730d879cb83SAl Viro { 1731d879cb83SAl Viro *new = *old; 173200e23707SDavid Howells if (unlikely(iov_iter_is_pipe(new))) { 1733241699cdSAl Viro WARN_ON(1); 1734241699cdSAl Viro return NULL; 1735241699cdSAl Viro } 173600e23707SDavid Howells if (iov_iter_is_bvec(new)) 1737d879cb83SAl Viro return new->bvec = kmemdup(new->bvec, 1738d879cb83SAl Viro new->nr_segs * sizeof(struct bio_vec), 1739d879cb83SAl Viro flags); 1740fcb14cb1SAl Viro else if (iov_iter_is_kvec(new) || iter_is_iovec(new)) 1741d879cb83SAl Viro /* iovec and kvec have identical layout */ 1742d879cb83SAl Viro return new->iov = kmemdup(new->iov, 1743d879cb83SAl Viro new->nr_segs * sizeof(struct iovec), 1744d879cb83SAl Viro flags); 1745fcb14cb1SAl Viro return NULL; 1746d879cb83SAl Viro } 1747d879cb83SAl Viro EXPORT_SYMBOL(dup_iter); 1748bc917be8SAl Viro 1749bfdc5970SChristoph Hellwig static int copy_compat_iovec_from_user(struct iovec *iov, 1750bfdc5970SChristoph Hellwig const struct iovec __user *uvec, unsigned long nr_segs) 1751bfdc5970SChristoph Hellwig { 1752bfdc5970SChristoph Hellwig const struct compat_iovec __user *uiov = 1753bfdc5970SChristoph Hellwig (const struct compat_iovec __user *)uvec; 1754bfdc5970SChristoph Hellwig int ret = -EFAULT, i; 1755bfdc5970SChristoph Hellwig 1756a959a978SChristoph Hellwig if (!user_access_begin(uiov, nr_segs * sizeof(*uiov))) 1757bfdc5970SChristoph Hellwig return -EFAULT; 1758bfdc5970SChristoph Hellwig 1759bfdc5970SChristoph Hellwig for (i = 0; i < nr_segs; i++) { 1760bfdc5970SChristoph Hellwig compat_uptr_t buf; 1761bfdc5970SChristoph Hellwig compat_ssize_t len; 1762bfdc5970SChristoph Hellwig 1763bfdc5970SChristoph Hellwig unsafe_get_user(len, &uiov[i].iov_len, uaccess_end); 1764bfdc5970SChristoph Hellwig unsafe_get_user(buf, &uiov[i].iov_base, uaccess_end); 1765bfdc5970SChristoph Hellwig 1766bfdc5970SChristoph Hellwig /* check for compat_size_t not fitting in compat_ssize_t .. */ 1767bfdc5970SChristoph Hellwig if (len < 0) { 1768bfdc5970SChristoph Hellwig ret = -EINVAL; 1769bfdc5970SChristoph Hellwig goto uaccess_end; 1770bfdc5970SChristoph Hellwig } 1771bfdc5970SChristoph Hellwig iov[i].iov_base = compat_ptr(buf); 1772bfdc5970SChristoph Hellwig iov[i].iov_len = len; 1773bfdc5970SChristoph Hellwig } 1774bfdc5970SChristoph Hellwig 1775bfdc5970SChristoph Hellwig ret = 0; 1776bfdc5970SChristoph Hellwig uaccess_end: 1777bfdc5970SChristoph Hellwig user_access_end(); 1778bfdc5970SChristoph Hellwig return ret; 1779bfdc5970SChristoph Hellwig } 1780bfdc5970SChristoph Hellwig 1781bfdc5970SChristoph Hellwig static int copy_iovec_from_user(struct iovec *iov, 1782bfdc5970SChristoph Hellwig const struct iovec __user *uvec, unsigned long nr_segs) 1783fb041b59SDavid Laight { 1784fb041b59SDavid Laight unsigned long seg; 1785bfdc5970SChristoph Hellwig 1786bfdc5970SChristoph Hellwig if (copy_from_user(iov, uvec, nr_segs * sizeof(*uvec))) 1787bfdc5970SChristoph Hellwig return -EFAULT; 1788bfdc5970SChristoph Hellwig for (seg = 0; seg < nr_segs; seg++) { 1789bfdc5970SChristoph Hellwig if ((ssize_t)iov[seg].iov_len < 0) 1790bfdc5970SChristoph Hellwig return -EINVAL; 1791bfdc5970SChristoph Hellwig } 1792bfdc5970SChristoph Hellwig 1793bfdc5970SChristoph Hellwig return 0; 1794bfdc5970SChristoph Hellwig } 1795bfdc5970SChristoph Hellwig 1796bfdc5970SChristoph Hellwig struct iovec *iovec_from_user(const struct iovec __user *uvec, 1797bfdc5970SChristoph Hellwig unsigned long nr_segs, unsigned long fast_segs, 1798bfdc5970SChristoph Hellwig struct iovec *fast_iov, bool compat) 1799bfdc5970SChristoph Hellwig { 1800bfdc5970SChristoph Hellwig struct iovec *iov = fast_iov; 1801bfdc5970SChristoph Hellwig int ret; 1802fb041b59SDavid Laight 1803fb041b59SDavid Laight /* 1804bfdc5970SChristoph Hellwig * SuS says "The readv() function *may* fail if the iovcnt argument was 1805bfdc5970SChristoph Hellwig * less than or equal to 0, or greater than {IOV_MAX}. Linux has 1806fb041b59SDavid Laight * traditionally returned zero for zero segments, so... 1807fb041b59SDavid Laight */ 1808bfdc5970SChristoph Hellwig if (nr_segs == 0) 1809bfdc5970SChristoph Hellwig return iov; 1810bfdc5970SChristoph Hellwig if (nr_segs > UIO_MAXIOV) 1811bfdc5970SChristoph Hellwig return ERR_PTR(-EINVAL); 1812fb041b59SDavid Laight if (nr_segs > fast_segs) { 1813fb041b59SDavid Laight iov = kmalloc_array(nr_segs, sizeof(struct iovec), GFP_KERNEL); 1814bfdc5970SChristoph Hellwig if (!iov) 1815bfdc5970SChristoph Hellwig return ERR_PTR(-ENOMEM); 1816fb041b59SDavid Laight } 1817bfdc5970SChristoph Hellwig 1818bfdc5970SChristoph Hellwig if (compat) 1819bfdc5970SChristoph Hellwig ret = copy_compat_iovec_from_user(iov, uvec, nr_segs); 1820bfdc5970SChristoph Hellwig else 1821bfdc5970SChristoph Hellwig ret = copy_iovec_from_user(iov, uvec, nr_segs); 1822bfdc5970SChristoph Hellwig if (ret) { 1823bfdc5970SChristoph Hellwig if (iov != fast_iov) 1824bfdc5970SChristoph Hellwig kfree(iov); 1825bfdc5970SChristoph Hellwig return ERR_PTR(ret); 1826fb041b59SDavid Laight } 1827bfdc5970SChristoph Hellwig 1828bfdc5970SChristoph Hellwig return iov; 1829bfdc5970SChristoph Hellwig } 1830bfdc5970SChristoph Hellwig 1831bfdc5970SChristoph Hellwig ssize_t __import_iovec(int type, const struct iovec __user *uvec, 1832bfdc5970SChristoph Hellwig unsigned nr_segs, unsigned fast_segs, struct iovec **iovp, 1833bfdc5970SChristoph Hellwig struct iov_iter *i, bool compat) 1834bfdc5970SChristoph Hellwig { 1835bfdc5970SChristoph Hellwig ssize_t total_len = 0; 1836bfdc5970SChristoph Hellwig unsigned long seg; 1837bfdc5970SChristoph Hellwig struct iovec *iov; 1838bfdc5970SChristoph Hellwig 1839bfdc5970SChristoph Hellwig iov = iovec_from_user(uvec, nr_segs, fast_segs, *iovp, compat); 1840bfdc5970SChristoph Hellwig if (IS_ERR(iov)) { 1841bfdc5970SChristoph Hellwig *iovp = NULL; 1842bfdc5970SChristoph Hellwig return PTR_ERR(iov); 1843fb041b59SDavid Laight } 1844fb041b59SDavid Laight 1845fb041b59SDavid Laight /* 1846bfdc5970SChristoph Hellwig * According to the Single Unix Specification we should return EINVAL if 1847bfdc5970SChristoph Hellwig * an element length is < 0 when cast to ssize_t or if the total length 1848bfdc5970SChristoph Hellwig * would overflow the ssize_t return value of the system call. 1849fb041b59SDavid Laight * 1850fb041b59SDavid Laight * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the 1851fb041b59SDavid Laight * overflow case. 1852fb041b59SDavid Laight */ 1853fb041b59SDavid Laight for (seg = 0; seg < nr_segs; seg++) { 1854fb041b59SDavid Laight ssize_t len = (ssize_t)iov[seg].iov_len; 1855fb041b59SDavid Laight 1856bfdc5970SChristoph Hellwig if (!access_ok(iov[seg].iov_base, len)) { 1857bfdc5970SChristoph Hellwig if (iov != *iovp) 1858bfdc5970SChristoph Hellwig kfree(iov); 1859bfdc5970SChristoph Hellwig *iovp = NULL; 1860bfdc5970SChristoph Hellwig return -EFAULT; 1861fb041b59SDavid Laight } 1862bfdc5970SChristoph Hellwig 1863bfdc5970SChristoph Hellwig if (len > MAX_RW_COUNT - total_len) { 1864bfdc5970SChristoph Hellwig len = MAX_RW_COUNT - total_len; 1865fb041b59SDavid Laight iov[seg].iov_len = len; 1866fb041b59SDavid Laight } 1867bfdc5970SChristoph Hellwig total_len += len; 1868fb041b59SDavid Laight } 1869bfdc5970SChristoph Hellwig 1870bfdc5970SChristoph Hellwig iov_iter_init(i, type, iov, nr_segs, total_len); 1871bfdc5970SChristoph Hellwig if (iov == *iovp) 1872bfdc5970SChristoph Hellwig *iovp = NULL; 1873bfdc5970SChristoph Hellwig else 1874bfdc5970SChristoph Hellwig *iovp = iov; 1875bfdc5970SChristoph Hellwig return total_len; 1876fb041b59SDavid Laight } 1877fb041b59SDavid Laight 1878ffecee4fSVegard Nossum /** 1879ffecee4fSVegard Nossum * import_iovec() - Copy an array of &struct iovec from userspace 1880ffecee4fSVegard Nossum * into the kernel, check that it is valid, and initialize a new 1881ffecee4fSVegard Nossum * &struct iov_iter iterator to access it. 1882ffecee4fSVegard Nossum * 1883ffecee4fSVegard Nossum * @type: One of %READ or %WRITE. 1884bfdc5970SChristoph Hellwig * @uvec: Pointer to the userspace array. 1885ffecee4fSVegard Nossum * @nr_segs: Number of elements in userspace array. 1886ffecee4fSVegard Nossum * @fast_segs: Number of elements in @iov. 1887bfdc5970SChristoph Hellwig * @iovp: (input and output parameter) Pointer to pointer to (usually small 1888ffecee4fSVegard Nossum * on-stack) kernel array. 1889ffecee4fSVegard Nossum * @i: Pointer to iterator that will be initialized on success. 1890ffecee4fSVegard Nossum * 1891ffecee4fSVegard Nossum * If the array pointed to by *@iov is large enough to hold all @nr_segs, 1892ffecee4fSVegard Nossum * then this function places %NULL in *@iov on return. Otherwise, a new 1893ffecee4fSVegard Nossum * array will be allocated and the result placed in *@iov. This means that 1894ffecee4fSVegard Nossum * the caller may call kfree() on *@iov regardless of whether the small 1895ffecee4fSVegard Nossum * on-stack array was used or not (and regardless of whether this function 1896ffecee4fSVegard Nossum * returns an error or not). 1897ffecee4fSVegard Nossum * 189887e5e6daSJens Axboe * Return: Negative error code on error, bytes imported on success 1899ffecee4fSVegard Nossum */ 1900bfdc5970SChristoph Hellwig ssize_t import_iovec(int type, const struct iovec __user *uvec, 1901bc917be8SAl Viro unsigned nr_segs, unsigned fast_segs, 1902bfdc5970SChristoph Hellwig struct iovec **iovp, struct iov_iter *i) 1903bc917be8SAl Viro { 190489cd35c5SChristoph Hellwig return __import_iovec(type, uvec, nr_segs, fast_segs, iovp, i, 190589cd35c5SChristoph Hellwig in_compat_syscall()); 1906bc917be8SAl Viro } 1907bc917be8SAl Viro EXPORT_SYMBOL(import_iovec); 1908bc917be8SAl Viro 1909bc917be8SAl Viro int import_single_range(int rw, void __user *buf, size_t len, 1910bc917be8SAl Viro struct iovec *iov, struct iov_iter *i) 1911bc917be8SAl Viro { 1912bc917be8SAl Viro if (len > MAX_RW_COUNT) 1913bc917be8SAl Viro len = MAX_RW_COUNT; 191496d4f267SLinus Torvalds if (unlikely(!access_ok(buf, len))) 1915bc917be8SAl Viro return -EFAULT; 1916bc917be8SAl Viro 1917bc917be8SAl Viro iov->iov_base = buf; 1918bc917be8SAl Viro iov->iov_len = len; 1919bc917be8SAl Viro iov_iter_init(i, rw, iov, 1, len); 1920bc917be8SAl Viro return 0; 1921bc917be8SAl Viro } 1922e1267585SAl Viro EXPORT_SYMBOL(import_single_range); 19238fb0f47aSJens Axboe 19242ad9bd83SJens Axboe int import_ubuf(int rw, void __user *buf, size_t len, struct iov_iter *i) 19252ad9bd83SJens Axboe { 19262ad9bd83SJens Axboe if (len > MAX_RW_COUNT) 19272ad9bd83SJens Axboe len = MAX_RW_COUNT; 19282ad9bd83SJens Axboe if (unlikely(!access_ok(buf, len))) 19292ad9bd83SJens Axboe return -EFAULT; 19302ad9bd83SJens Axboe 19312ad9bd83SJens Axboe iov_iter_ubuf(i, rw, buf, len); 19322ad9bd83SJens Axboe return 0; 19332ad9bd83SJens Axboe } 19342ad9bd83SJens Axboe 19358fb0f47aSJens Axboe /** 19368fb0f47aSJens Axboe * iov_iter_restore() - Restore a &struct iov_iter to the same state as when 19378fb0f47aSJens Axboe * iov_iter_save_state() was called. 19388fb0f47aSJens Axboe * 19398fb0f47aSJens Axboe * @i: &struct iov_iter to restore 19408fb0f47aSJens Axboe * @state: state to restore from 19418fb0f47aSJens Axboe * 19428fb0f47aSJens Axboe * Used after iov_iter_save_state() to bring restore @i, if operations may 19438fb0f47aSJens Axboe * have advanced it. 19448fb0f47aSJens Axboe * 19458fb0f47aSJens Axboe * Note: only works on ITER_IOVEC, ITER_BVEC, and ITER_KVEC 19468fb0f47aSJens Axboe */ 19478fb0f47aSJens Axboe void iov_iter_restore(struct iov_iter *i, struct iov_iter_state *state) 19488fb0f47aSJens Axboe { 19494397a17cSKeith Busch if (WARN_ON_ONCE(!iov_iter_is_bvec(i) && !iter_is_iovec(i) && 19504397a17cSKeith Busch !iter_is_ubuf(i)) && !iov_iter_is_kvec(i)) 19518fb0f47aSJens Axboe return; 19528fb0f47aSJens Axboe i->iov_offset = state->iov_offset; 19538fb0f47aSJens Axboe i->count = state->count; 1954fcb14cb1SAl Viro if (iter_is_ubuf(i)) 1955fcb14cb1SAl Viro return; 19568fb0f47aSJens Axboe /* 19578fb0f47aSJens Axboe * For the *vec iters, nr_segs + iov is constant - if we increment 19588fb0f47aSJens Axboe * the vec, then we also decrement the nr_segs count. Hence we don't 19598fb0f47aSJens Axboe * need to track both of these, just one is enough and we can deduct 19608fb0f47aSJens Axboe * the other from that. ITER_KVEC and ITER_IOVEC are the same struct 19618fb0f47aSJens Axboe * size, so we can just increment the iov pointer as they are unionzed. 19628fb0f47aSJens Axboe * ITER_BVEC _may_ be the same size on some archs, but on others it is 19638fb0f47aSJens Axboe * not. Be safe and handle it separately. 19648fb0f47aSJens Axboe */ 19658fb0f47aSJens Axboe BUILD_BUG_ON(sizeof(struct iovec) != sizeof(struct kvec)); 19668fb0f47aSJens Axboe if (iov_iter_is_bvec(i)) 19678fb0f47aSJens Axboe i->bvec -= state->nr_segs - i->nr_segs; 19688fb0f47aSJens Axboe else 19698fb0f47aSJens Axboe i->iov -= state->nr_segs - i->nr_segs; 19708fb0f47aSJens Axboe i->nr_segs = state->nr_segs; 19718fb0f47aSJens Axboe } 19727d58fe73SDavid Howells 19737d58fe73SDavid Howells /* 19747d58fe73SDavid Howells * Extract a list of contiguous pages from an ITER_XARRAY iterator. This does not 19757d58fe73SDavid Howells * get references on the pages, nor does it get a pin on them. 19767d58fe73SDavid Howells */ 19777d58fe73SDavid Howells static ssize_t iov_iter_extract_xarray_pages(struct iov_iter *i, 19787d58fe73SDavid Howells struct page ***pages, size_t maxsize, 19797d58fe73SDavid Howells unsigned int maxpages, 19807d58fe73SDavid Howells iov_iter_extraction_t extraction_flags, 19817d58fe73SDavid Howells size_t *offset0) 19827d58fe73SDavid Howells { 19837d58fe73SDavid Howells struct page *page, **p; 19847d58fe73SDavid Howells unsigned int nr = 0, offset; 19857d58fe73SDavid Howells loff_t pos = i->xarray_start + i->iov_offset; 19867d58fe73SDavid Howells pgoff_t index = pos >> PAGE_SHIFT; 19877d58fe73SDavid Howells XA_STATE(xas, i->xarray, index); 19887d58fe73SDavid Howells 19897d58fe73SDavid Howells offset = pos & ~PAGE_MASK; 19907d58fe73SDavid Howells *offset0 = offset; 19917d58fe73SDavid Howells 19927d58fe73SDavid Howells maxpages = want_pages_array(pages, maxsize, offset, maxpages); 19937d58fe73SDavid Howells if (!maxpages) 19947d58fe73SDavid Howells return -ENOMEM; 19957d58fe73SDavid Howells p = *pages; 19967d58fe73SDavid Howells 19977d58fe73SDavid Howells rcu_read_lock(); 19987d58fe73SDavid Howells for (page = xas_load(&xas); page; page = xas_next(&xas)) { 19997d58fe73SDavid Howells if (xas_retry(&xas, page)) 20007d58fe73SDavid Howells continue; 20017d58fe73SDavid Howells 20027d58fe73SDavid Howells /* Has the page moved or been split? */ 20037d58fe73SDavid Howells if (unlikely(page != xas_reload(&xas))) { 20047d58fe73SDavid Howells xas_reset(&xas); 20057d58fe73SDavid Howells continue; 20067d58fe73SDavid Howells } 20077d58fe73SDavid Howells 20087d58fe73SDavid Howells p[nr++] = find_subpage(page, xas.xa_index); 20097d58fe73SDavid Howells if (nr == maxpages) 20107d58fe73SDavid Howells break; 20117d58fe73SDavid Howells } 20127d58fe73SDavid Howells rcu_read_unlock(); 20137d58fe73SDavid Howells 20147d58fe73SDavid Howells maxsize = min_t(size_t, nr * PAGE_SIZE - offset, maxsize); 20157d58fe73SDavid Howells iov_iter_advance(i, maxsize); 20167d58fe73SDavid Howells return maxsize; 20177d58fe73SDavid Howells } 20187d58fe73SDavid Howells 20197d58fe73SDavid Howells /* 20207d58fe73SDavid Howells * Extract a list of contiguous pages from an ITER_BVEC iterator. This does 20217d58fe73SDavid Howells * not get references on the pages, nor does it get a pin on them. 20227d58fe73SDavid Howells */ 20237d58fe73SDavid Howells static ssize_t iov_iter_extract_bvec_pages(struct iov_iter *i, 20247d58fe73SDavid Howells struct page ***pages, size_t maxsize, 20257d58fe73SDavid Howells unsigned int maxpages, 20267d58fe73SDavid Howells iov_iter_extraction_t extraction_flags, 20277d58fe73SDavid Howells size_t *offset0) 20287d58fe73SDavid Howells { 20297d58fe73SDavid Howells struct page **p, *page; 20307d58fe73SDavid Howells size_t skip = i->iov_offset, offset; 20317d58fe73SDavid Howells int k; 20327d58fe73SDavid Howells 20337d58fe73SDavid Howells for (;;) { 20347d58fe73SDavid Howells if (i->nr_segs == 0) 20357d58fe73SDavid Howells return 0; 20367d58fe73SDavid Howells maxsize = min(maxsize, i->bvec->bv_len - skip); 20377d58fe73SDavid Howells if (maxsize) 20387d58fe73SDavid Howells break; 20397d58fe73SDavid Howells i->iov_offset = 0; 20407d58fe73SDavid Howells i->nr_segs--; 20417d58fe73SDavid Howells i->bvec++; 20427d58fe73SDavid Howells skip = 0; 20437d58fe73SDavid Howells } 20447d58fe73SDavid Howells 20457d58fe73SDavid Howells skip += i->bvec->bv_offset; 20467d58fe73SDavid Howells page = i->bvec->bv_page + skip / PAGE_SIZE; 20477d58fe73SDavid Howells offset = skip % PAGE_SIZE; 20487d58fe73SDavid Howells *offset0 = offset; 20497d58fe73SDavid Howells 20507d58fe73SDavid Howells maxpages = want_pages_array(pages, maxsize, offset, maxpages); 20517d58fe73SDavid Howells if (!maxpages) 20527d58fe73SDavid Howells return -ENOMEM; 20537d58fe73SDavid Howells p = *pages; 20547d58fe73SDavid Howells for (k = 0; k < maxpages; k++) 20557d58fe73SDavid Howells p[k] = page + k; 20567d58fe73SDavid Howells 20577d58fe73SDavid Howells maxsize = min_t(size_t, maxsize, maxpages * PAGE_SIZE - offset); 20587d58fe73SDavid Howells iov_iter_advance(i, maxsize); 20597d58fe73SDavid Howells return maxsize; 20607d58fe73SDavid Howells } 20617d58fe73SDavid Howells 20627d58fe73SDavid Howells /* 20637d58fe73SDavid Howells * Extract a list of virtually contiguous pages from an ITER_KVEC iterator. 20647d58fe73SDavid Howells * This does not get references on the pages, nor does it get a pin on them. 20657d58fe73SDavid Howells */ 20667d58fe73SDavid Howells static ssize_t iov_iter_extract_kvec_pages(struct iov_iter *i, 20677d58fe73SDavid Howells struct page ***pages, size_t maxsize, 20687d58fe73SDavid Howells unsigned int maxpages, 20697d58fe73SDavid Howells iov_iter_extraction_t extraction_flags, 20707d58fe73SDavid Howells size_t *offset0) 20717d58fe73SDavid Howells { 20727d58fe73SDavid Howells struct page **p, *page; 20737d58fe73SDavid Howells const void *kaddr; 20747d58fe73SDavid Howells size_t skip = i->iov_offset, offset, len; 20757d58fe73SDavid Howells int k; 20767d58fe73SDavid Howells 20777d58fe73SDavid Howells for (;;) { 20787d58fe73SDavid Howells if (i->nr_segs == 0) 20797d58fe73SDavid Howells return 0; 20807d58fe73SDavid Howells maxsize = min(maxsize, i->kvec->iov_len - skip); 20817d58fe73SDavid Howells if (maxsize) 20827d58fe73SDavid Howells break; 20837d58fe73SDavid Howells i->iov_offset = 0; 20847d58fe73SDavid Howells i->nr_segs--; 20857d58fe73SDavid Howells i->kvec++; 20867d58fe73SDavid Howells skip = 0; 20877d58fe73SDavid Howells } 20887d58fe73SDavid Howells 20897d58fe73SDavid Howells kaddr = i->kvec->iov_base + skip; 20907d58fe73SDavid Howells offset = (unsigned long)kaddr & ~PAGE_MASK; 20917d58fe73SDavid Howells *offset0 = offset; 20927d58fe73SDavid Howells 20937d58fe73SDavid Howells maxpages = want_pages_array(pages, maxsize, offset, maxpages); 20947d58fe73SDavid Howells if (!maxpages) 20957d58fe73SDavid Howells return -ENOMEM; 20967d58fe73SDavid Howells p = *pages; 20977d58fe73SDavid Howells 20987d58fe73SDavid Howells kaddr -= offset; 20997d58fe73SDavid Howells len = offset + maxsize; 21007d58fe73SDavid Howells for (k = 0; k < maxpages; k++) { 21017d58fe73SDavid Howells size_t seg = min_t(size_t, len, PAGE_SIZE); 21027d58fe73SDavid Howells 21037d58fe73SDavid Howells if (is_vmalloc_or_module_addr(kaddr)) 21047d58fe73SDavid Howells page = vmalloc_to_page(kaddr); 21057d58fe73SDavid Howells else 21067d58fe73SDavid Howells page = virt_to_page(kaddr); 21077d58fe73SDavid Howells 21087d58fe73SDavid Howells p[k] = page; 21097d58fe73SDavid Howells len -= seg; 21107d58fe73SDavid Howells kaddr += PAGE_SIZE; 21117d58fe73SDavid Howells } 21127d58fe73SDavid Howells 21137d58fe73SDavid Howells maxsize = min_t(size_t, maxsize, maxpages * PAGE_SIZE - offset); 21147d58fe73SDavid Howells iov_iter_advance(i, maxsize); 21157d58fe73SDavid Howells return maxsize; 21167d58fe73SDavid Howells } 21177d58fe73SDavid Howells 21187d58fe73SDavid Howells /* 21197d58fe73SDavid Howells * Extract a list of contiguous pages from a user iterator and get a pin on 21207d58fe73SDavid Howells * each of them. This should only be used if the iterator is user-backed 21217d58fe73SDavid Howells * (IOBUF/UBUF). 21227d58fe73SDavid Howells * 21237d58fe73SDavid Howells * It does not get refs on the pages, but the pages must be unpinned by the 21247d58fe73SDavid Howells * caller once the transfer is complete. 21257d58fe73SDavid Howells * 21267d58fe73SDavid Howells * This is safe to be used where background IO/DMA *is* going to be modifying 21277d58fe73SDavid Howells * the buffer; using a pin rather than a ref makes forces fork() to give the 21287d58fe73SDavid Howells * child a copy of the page. 21297d58fe73SDavid Howells */ 21307d58fe73SDavid Howells static ssize_t iov_iter_extract_user_pages(struct iov_iter *i, 21317d58fe73SDavid Howells struct page ***pages, 21327d58fe73SDavid Howells size_t maxsize, 21337d58fe73SDavid Howells unsigned int maxpages, 21347d58fe73SDavid Howells iov_iter_extraction_t extraction_flags, 21357d58fe73SDavid Howells size_t *offset0) 21367d58fe73SDavid Howells { 21377d58fe73SDavid Howells unsigned long addr; 21387d58fe73SDavid Howells unsigned int gup_flags = 0; 21397d58fe73SDavid Howells size_t offset; 21407d58fe73SDavid Howells int res; 21417d58fe73SDavid Howells 21427d58fe73SDavid Howells if (i->data_source == ITER_DEST) 21437d58fe73SDavid Howells gup_flags |= FOLL_WRITE; 21447d58fe73SDavid Howells if (extraction_flags & ITER_ALLOW_P2PDMA) 21457d58fe73SDavid Howells gup_flags |= FOLL_PCI_P2PDMA; 21467d58fe73SDavid Howells if (i->nofault) 21477d58fe73SDavid Howells gup_flags |= FOLL_NOFAULT; 21487d58fe73SDavid Howells 21497d58fe73SDavid Howells addr = first_iovec_segment(i, &maxsize); 21507d58fe73SDavid Howells *offset0 = offset = addr % PAGE_SIZE; 21517d58fe73SDavid Howells addr &= PAGE_MASK; 21527d58fe73SDavid Howells maxpages = want_pages_array(pages, maxsize, offset, maxpages); 21537d58fe73SDavid Howells if (!maxpages) 21547d58fe73SDavid Howells return -ENOMEM; 21557d58fe73SDavid Howells res = pin_user_pages_fast(addr, maxpages, gup_flags, *pages); 21567d58fe73SDavid Howells if (unlikely(res <= 0)) 21577d58fe73SDavid Howells return res; 21587d58fe73SDavid Howells maxsize = min_t(size_t, maxsize, res * PAGE_SIZE - offset); 21597d58fe73SDavid Howells iov_iter_advance(i, maxsize); 21607d58fe73SDavid Howells return maxsize; 21617d58fe73SDavid Howells } 21627d58fe73SDavid Howells 21637d58fe73SDavid Howells /** 21647d58fe73SDavid Howells * iov_iter_extract_pages - Extract a list of contiguous pages from an iterator 21657d58fe73SDavid Howells * @i: The iterator to extract from 21667d58fe73SDavid Howells * @pages: Where to return the list of pages 21677d58fe73SDavid Howells * @maxsize: The maximum amount of iterator to extract 21687d58fe73SDavid Howells * @maxpages: The maximum size of the list of pages 21697d58fe73SDavid Howells * @extraction_flags: Flags to qualify request 21707d58fe73SDavid Howells * @offset0: Where to return the starting offset into (*@pages)[0] 21717d58fe73SDavid Howells * 21727d58fe73SDavid Howells * Extract a list of contiguous pages from the current point of the iterator, 21737d58fe73SDavid Howells * advancing the iterator. The maximum number of pages and the maximum amount 21747d58fe73SDavid Howells * of page contents can be set. 21757d58fe73SDavid Howells * 21767d58fe73SDavid Howells * If *@pages is NULL, a page list will be allocated to the required size and 21777d58fe73SDavid Howells * *@pages will be set to its base. If *@pages is not NULL, it will be assumed 21787d58fe73SDavid Howells * that the caller allocated a page list at least @maxpages in size and this 21797d58fe73SDavid Howells * will be filled in. 21807d58fe73SDavid Howells * 21817d58fe73SDavid Howells * @extraction_flags can have ITER_ALLOW_P2PDMA set to request peer-to-peer DMA 21827d58fe73SDavid Howells * be allowed on the pages extracted. 21837d58fe73SDavid Howells * 21847d58fe73SDavid Howells * The iov_iter_extract_will_pin() function can be used to query how cleanup 21857d58fe73SDavid Howells * should be performed. 21867d58fe73SDavid Howells * 21877d58fe73SDavid Howells * Extra refs or pins on the pages may be obtained as follows: 21887d58fe73SDavid Howells * 21897d58fe73SDavid Howells * (*) If the iterator is user-backed (ITER_IOVEC/ITER_UBUF), pins will be 21907d58fe73SDavid Howells * added to the pages, but refs will not be taken. 21917d58fe73SDavid Howells * iov_iter_extract_will_pin() will return true. 21927d58fe73SDavid Howells * 21937d58fe73SDavid Howells * (*) If the iterator is ITER_KVEC, ITER_BVEC or ITER_XARRAY, the pages are 21947d58fe73SDavid Howells * merely listed; no extra refs or pins are obtained. 21957d58fe73SDavid Howells * iov_iter_extract_will_pin() will return 0. 21967d58fe73SDavid Howells * 21977d58fe73SDavid Howells * Note also: 21987d58fe73SDavid Howells * 21997d58fe73SDavid Howells * (*) Use with ITER_DISCARD is not supported as that has no content. 22007d58fe73SDavid Howells * 22017d58fe73SDavid Howells * On success, the function sets *@pages to the new pagelist, if allocated, and 22027d58fe73SDavid Howells * sets *offset0 to the offset into the first page. 22037d58fe73SDavid Howells * 22047d58fe73SDavid Howells * It may also return -ENOMEM and -EFAULT. 22057d58fe73SDavid Howells */ 22067d58fe73SDavid Howells ssize_t iov_iter_extract_pages(struct iov_iter *i, 22077d58fe73SDavid Howells struct page ***pages, 22087d58fe73SDavid Howells size_t maxsize, 22097d58fe73SDavid Howells unsigned int maxpages, 22107d58fe73SDavid Howells iov_iter_extraction_t extraction_flags, 22117d58fe73SDavid Howells size_t *offset0) 22127d58fe73SDavid Howells { 22137d58fe73SDavid Howells maxsize = min_t(size_t, min_t(size_t, maxsize, i->count), MAX_RW_COUNT); 22147d58fe73SDavid Howells if (!maxsize) 22157d58fe73SDavid Howells return 0; 22167d58fe73SDavid Howells 22177d58fe73SDavid Howells if (likely(user_backed_iter(i))) 22187d58fe73SDavid Howells return iov_iter_extract_user_pages(i, pages, maxsize, 22197d58fe73SDavid Howells maxpages, extraction_flags, 22207d58fe73SDavid Howells offset0); 22217d58fe73SDavid Howells if (iov_iter_is_kvec(i)) 22227d58fe73SDavid Howells return iov_iter_extract_kvec_pages(i, pages, maxsize, 22237d58fe73SDavid Howells maxpages, extraction_flags, 22247d58fe73SDavid Howells offset0); 22257d58fe73SDavid Howells if (iov_iter_is_bvec(i)) 22267d58fe73SDavid Howells return iov_iter_extract_bvec_pages(i, pages, maxsize, 22277d58fe73SDavid Howells maxpages, extraction_flags, 22287d58fe73SDavid Howells offset0); 22297d58fe73SDavid Howells if (iov_iter_is_xarray(i)) 22307d58fe73SDavid Howells return iov_iter_extract_xarray_pages(i, pages, maxsize, 22317d58fe73SDavid Howells maxpages, extraction_flags, 22327d58fe73SDavid Howells offset0); 22337d58fe73SDavid Howells return -EFAULT; 22347d58fe73SDavid Howells } 22357d58fe73SDavid Howells EXPORT_SYMBOL_GPL(iov_iter_extract_pages); 2236