1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 27999096fSHerbert Xu #include <crypto/hash.h> 3d879cb83SAl Viro #include <linux/export.h> 42f8b5444SChristoph Hellwig #include <linux/bvec.h> 54d0e9df5SAlbert van der Linde #include <linux/fault-inject-usercopy.h> 6d879cb83SAl Viro #include <linux/uio.h> 7d879cb83SAl Viro #include <linux/pagemap.h> 828961998SIra Weiny #include <linux/highmem.h> 9d879cb83SAl Viro #include <linux/slab.h> 10d879cb83SAl Viro #include <linux/vmalloc.h> 11241699cdSAl Viro #include <linux/splice.h> 12bfdc5970SChristoph Hellwig #include <linux/compat.h> 13d879cb83SAl Viro #include <net/checksum.h> 14d05f4435SSagi Grimberg #include <linux/scatterlist.h> 15d0ef4c36SMarco Elver #include <linux/instrumented.h> 16d879cb83SAl Viro 17241699cdSAl Viro #define PIPE_PARANOIA /* for now */ 18241699cdSAl Viro 19fcb14cb1SAl Viro /* covers ubuf and kbuf alike */ 20fcb14cb1SAl Viro #define iterate_buf(i, n, base, len, off, __p, STEP) { \ 21fcb14cb1SAl Viro size_t __maybe_unused off = 0; \ 22fcb14cb1SAl Viro len = n; \ 23fcb14cb1SAl Viro base = __p + i->iov_offset; \ 24fcb14cb1SAl Viro len -= (STEP); \ 25fcb14cb1SAl Viro i->iov_offset += len; \ 26fcb14cb1SAl Viro n = len; \ 27fcb14cb1SAl Viro } 28fcb14cb1SAl Viro 295c67aa90SAl Viro /* covers iovec and kvec alike */ 30a6e4ec7bSAl Viro #define iterate_iovec(i, n, base, len, off, __p, STEP) { \ 317baa5099SAl Viro size_t off = 0; \ 32a6e4ec7bSAl Viro size_t skip = i->iov_offset; \ 337a1bcb5dSAl Viro do { \ 347baa5099SAl Viro len = min(n, __p->iov_len - skip); \ 357baa5099SAl Viro if (likely(len)) { \ 367baa5099SAl Viro base = __p->iov_base + skip; \ 377baa5099SAl Viro len -= (STEP); \ 387baa5099SAl Viro off += len; \ 397baa5099SAl Viro skip += len; \ 407baa5099SAl Viro n -= len; \ 417a1bcb5dSAl Viro if (skip < __p->iov_len) \ 427a1bcb5dSAl Viro break; \ 43d879cb83SAl Viro } \ 44d879cb83SAl Viro __p++; \ 457a1bcb5dSAl Viro skip = 0; \ 467a1bcb5dSAl Viro } while (n); \ 47a6e4ec7bSAl Viro i->iov_offset = skip; \ 487baa5099SAl Viro n = off; \ 49d879cb83SAl Viro } 50d879cb83SAl Viro 51a6e4ec7bSAl Viro #define iterate_bvec(i, n, base, len, off, p, STEP) { \ 527baa5099SAl Viro size_t off = 0; \ 53a6e4ec7bSAl Viro unsigned skip = i->iov_offset; \ 547491a2bfSAl Viro while (n) { \ 557491a2bfSAl Viro unsigned offset = p->bv_offset + skip; \ 561b4fb5ffSAl Viro unsigned left; \ 5721b56c84SAl Viro void *kaddr = kmap_local_page(p->bv_page + \ 5821b56c84SAl Viro offset / PAGE_SIZE); \ 597baa5099SAl Viro base = kaddr + offset % PAGE_SIZE; \ 60a6e4ec7bSAl Viro len = min(min(n, (size_t)(p->bv_len - skip)), \ 617491a2bfSAl Viro (size_t)(PAGE_SIZE - offset % PAGE_SIZE)); \ 621b4fb5ffSAl Viro left = (STEP); \ 6321b56c84SAl Viro kunmap_local(kaddr); \ 647baa5099SAl Viro len -= left; \ 657baa5099SAl Viro off += len; \ 667baa5099SAl Viro skip += len; \ 677491a2bfSAl Viro if (skip == p->bv_len) { \ 687491a2bfSAl Viro skip = 0; \ 697491a2bfSAl Viro p++; \ 70d879cb83SAl Viro } \ 717baa5099SAl Viro n -= len; \ 721b4fb5ffSAl Viro if (left) \ 731b4fb5ffSAl Viro break; \ 747491a2bfSAl Viro } \ 75a6e4ec7bSAl Viro i->iov_offset = skip; \ 767baa5099SAl Viro n = off; \ 77d879cb83SAl Viro } 78d879cb83SAl Viro 79a6e4ec7bSAl Viro #define iterate_xarray(i, n, base, len, __off, STEP) { \ 801b4fb5ffSAl Viro __label__ __out; \ 81622838f3SAl Viro size_t __off = 0; \ 82821979f5SMatthew Wilcox (Oracle) struct folio *folio; \ 83a6e4ec7bSAl Viro loff_t start = i->xarray_start + i->iov_offset; \ 844b179e9aSAl Viro pgoff_t index = start / PAGE_SIZE; \ 857ff50620SDavid Howells XA_STATE(xas, i->xarray, index); \ 867ff50620SDavid Howells \ 87821979f5SMatthew Wilcox (Oracle) len = PAGE_SIZE - offset_in_page(start); \ 887ff50620SDavid Howells rcu_read_lock(); \ 89821979f5SMatthew Wilcox (Oracle) xas_for_each(&xas, folio, ULONG_MAX) { \ 901b4fb5ffSAl Viro unsigned left; \ 91821979f5SMatthew Wilcox (Oracle) size_t offset; \ 92821979f5SMatthew Wilcox (Oracle) if (xas_retry(&xas, folio)) \ 937ff50620SDavid Howells continue; \ 94821979f5SMatthew Wilcox (Oracle) if (WARN_ON(xa_is_value(folio))) \ 957ff50620SDavid Howells break; \ 96821979f5SMatthew Wilcox (Oracle) if (WARN_ON(folio_test_hugetlb(folio))) \ 977ff50620SDavid Howells break; \ 98821979f5SMatthew Wilcox (Oracle) offset = offset_in_folio(folio, start + __off); \ 99821979f5SMatthew Wilcox (Oracle) while (offset < folio_size(folio)) { \ 100821979f5SMatthew Wilcox (Oracle) base = kmap_local_folio(folio, offset); \ 1017baa5099SAl Viro len = min(n, len); \ 1021b4fb5ffSAl Viro left = (STEP); \ 103821979f5SMatthew Wilcox (Oracle) kunmap_local(base); \ 1047baa5099SAl Viro len -= left; \ 1057baa5099SAl Viro __off += len; \ 1067baa5099SAl Viro n -= len; \ 1071b4fb5ffSAl Viro if (left || n == 0) \ 1081b4fb5ffSAl Viro goto __out; \ 109821979f5SMatthew Wilcox (Oracle) offset += len; \ 110821979f5SMatthew Wilcox (Oracle) len = PAGE_SIZE; \ 1117ff50620SDavid Howells } \ 1127ff50620SDavid Howells } \ 1131b4fb5ffSAl Viro __out: \ 1147ff50620SDavid Howells rcu_read_unlock(); \ 115a6e4ec7bSAl Viro i->iov_offset += __off; \ 116622838f3SAl Viro n = __off; \ 1177ff50620SDavid Howells } 1187ff50620SDavid Howells 1197baa5099SAl Viro #define __iterate_and_advance(i, n, base, len, off, I, K) { \ 120dd254f5aSAl Viro if (unlikely(i->count < n)) \ 121dd254f5aSAl Viro n = i->count; \ 122f5da8354SAl Viro if (likely(n)) { \ 123fcb14cb1SAl Viro if (likely(iter_is_ubuf(i))) { \ 124fcb14cb1SAl Viro void __user *base; \ 125fcb14cb1SAl Viro size_t len; \ 126fcb14cb1SAl Viro iterate_buf(i, n, base, len, off, \ 127fcb14cb1SAl Viro i->ubuf, (I)) \ 128fcb14cb1SAl Viro } else if (likely(iter_is_iovec(i))) { \ 1295c67aa90SAl Viro const struct iovec *iov = i->iov; \ 1307baa5099SAl Viro void __user *base; \ 1317baa5099SAl Viro size_t len; \ 1327baa5099SAl Viro iterate_iovec(i, n, base, len, off, \ 133a6e4ec7bSAl Viro iov, (I)) \ 134d879cb83SAl Viro i->nr_segs -= iov - i->iov; \ 135d879cb83SAl Viro i->iov = iov; \ 13628f38db7SAl Viro } else if (iov_iter_is_bvec(i)) { \ 13728f38db7SAl Viro const struct bio_vec *bvec = i->bvec; \ 1387baa5099SAl Viro void *base; \ 1397baa5099SAl Viro size_t len; \ 1407baa5099SAl Viro iterate_bvec(i, n, base, len, off, \ 141a6e4ec7bSAl Viro bvec, (K)) \ 1427491a2bfSAl Viro i->nr_segs -= bvec - i->bvec; \ 1437491a2bfSAl Viro i->bvec = bvec; \ 14428f38db7SAl Viro } else if (iov_iter_is_kvec(i)) { \ 1455c67aa90SAl Viro const struct kvec *kvec = i->kvec; \ 1467baa5099SAl Viro void *base; \ 1477baa5099SAl Viro size_t len; \ 1487baa5099SAl Viro iterate_iovec(i, n, base, len, off, \ 149a6e4ec7bSAl Viro kvec, (K)) \ 15028f38db7SAl Viro i->nr_segs -= kvec - i->kvec; \ 15128f38db7SAl Viro i->kvec = kvec; \ 15228f38db7SAl Viro } else if (iov_iter_is_xarray(i)) { \ 1537baa5099SAl Viro void *base; \ 1547baa5099SAl Viro size_t len; \ 1557baa5099SAl Viro iterate_xarray(i, n, base, len, off, \ 156a6e4ec7bSAl Viro (K)) \ 157d879cb83SAl Viro } \ 158d879cb83SAl Viro i->count -= n; \ 159dd254f5aSAl Viro } \ 160d879cb83SAl Viro } 1617baa5099SAl Viro #define iterate_and_advance(i, n, base, len, off, I, K) \ 1627baa5099SAl Viro __iterate_and_advance(i, n, base, len, off, I, ((void)(K),0)) 163d879cb83SAl Viro 16409fc68dcSAl Viro static int copyout(void __user *to, const void *from, size_t n) 16509fc68dcSAl Viro { 1664d0e9df5SAlbert van der Linde if (should_fail_usercopy()) 1674d0e9df5SAlbert van der Linde return n; 16896d4f267SLinus Torvalds if (access_ok(to, n)) { 169d0ef4c36SMarco Elver instrument_copy_to_user(to, from, n); 17009fc68dcSAl Viro n = raw_copy_to_user(to, from, n); 17109fc68dcSAl Viro } 17209fc68dcSAl Viro return n; 17309fc68dcSAl Viro } 17409fc68dcSAl Viro 17509fc68dcSAl Viro static int copyin(void *to, const void __user *from, size_t n) 17609fc68dcSAl Viro { 17733b75c1dSAlexander Potapenko size_t res = n; 17833b75c1dSAlexander Potapenko 1794d0e9df5SAlbert van der Linde if (should_fail_usercopy()) 1804d0e9df5SAlbert van der Linde return n; 18196d4f267SLinus Torvalds if (access_ok(from, n)) { 18233b75c1dSAlexander Potapenko instrument_copy_from_user_before(to, from, n); 18333b75c1dSAlexander Potapenko res = raw_copy_from_user(to, from, n); 18433b75c1dSAlexander Potapenko instrument_copy_from_user_after(to, from, n, res); 18509fc68dcSAl Viro } 18633b75c1dSAlexander Potapenko return res; 18709fc68dcSAl Viro } 18809fc68dcSAl Viro 1892dcedb2aSAl Viro static inline struct pipe_buffer *pipe_buf(const struct pipe_inode_info *pipe, 1902dcedb2aSAl Viro unsigned int slot) 1912dcedb2aSAl Viro { 1922dcedb2aSAl Viro return &pipe->bufs[slot & (pipe->ring_size - 1)]; 1932dcedb2aSAl Viro } 1942dcedb2aSAl Viro 195241699cdSAl Viro #ifdef PIPE_PARANOIA 196241699cdSAl Viro static bool sanity(const struct iov_iter *i) 197241699cdSAl Viro { 198241699cdSAl Viro struct pipe_inode_info *pipe = i->pipe; 1998cefc107SDavid Howells unsigned int p_head = pipe->head; 2008cefc107SDavid Howells unsigned int p_tail = pipe->tail; 2018cefc107SDavid Howells unsigned int p_occupancy = pipe_occupancy(p_head, p_tail); 2028cefc107SDavid Howells unsigned int i_head = i->head; 2038cefc107SDavid Howells unsigned int idx; 2048cefc107SDavid Howells 20510f525a8SAl Viro if (i->last_offset) { 206241699cdSAl Viro struct pipe_buffer *p; 2078cefc107SDavid Howells if (unlikely(p_occupancy == 0)) 208241699cdSAl Viro goto Bad; // pipe must be non-empty 2098cefc107SDavid Howells if (unlikely(i_head != p_head - 1)) 210241699cdSAl Viro goto Bad; // must be at the last buffer... 211241699cdSAl Viro 2122dcedb2aSAl Viro p = pipe_buf(pipe, i_head); 21310f525a8SAl Viro if (unlikely(p->offset + p->len != abs(i->last_offset))) 214241699cdSAl Viro goto Bad; // ... at the end of segment 215241699cdSAl Viro } else { 2168cefc107SDavid Howells if (i_head != p_head) 217241699cdSAl Viro goto Bad; // must be right after the last buffer 218241699cdSAl Viro } 219241699cdSAl Viro return true; 220241699cdSAl Viro Bad: 22110f525a8SAl Viro printk(KERN_ERR "idx = %d, offset = %d\n", i_head, i->last_offset); 2228cefc107SDavid Howells printk(KERN_ERR "head = %d, tail = %d, buffers = %d\n", 2238cefc107SDavid Howells p_head, p_tail, pipe->ring_size); 2248cefc107SDavid Howells for (idx = 0; idx < pipe->ring_size; idx++) 225241699cdSAl Viro printk(KERN_ERR "[%p %p %d %d]\n", 226241699cdSAl Viro pipe->bufs[idx].ops, 227241699cdSAl Viro pipe->bufs[idx].page, 228241699cdSAl Viro pipe->bufs[idx].offset, 229241699cdSAl Viro pipe->bufs[idx].len); 230241699cdSAl Viro WARN_ON(1); 231241699cdSAl Viro return false; 232241699cdSAl Viro } 233241699cdSAl Viro #else 234241699cdSAl Viro #define sanity(i) true 235241699cdSAl Viro #endif 236241699cdSAl Viro 23747b7fcaeSAl Viro static struct page *push_anon(struct pipe_inode_info *pipe, unsigned size) 23847b7fcaeSAl Viro { 23947b7fcaeSAl Viro struct page *page = alloc_page(GFP_USER); 24047b7fcaeSAl Viro if (page) { 24147b7fcaeSAl Viro struct pipe_buffer *buf = pipe_buf(pipe, pipe->head++); 24247b7fcaeSAl Viro *buf = (struct pipe_buffer) { 24347b7fcaeSAl Viro .ops = &default_pipe_buf_ops, 24447b7fcaeSAl Viro .page = page, 24547b7fcaeSAl Viro .offset = 0, 24647b7fcaeSAl Viro .len = size 24747b7fcaeSAl Viro }; 24847b7fcaeSAl Viro } 24947b7fcaeSAl Viro return page; 25047b7fcaeSAl Viro } 25147b7fcaeSAl Viro 25247b7fcaeSAl Viro static void push_page(struct pipe_inode_info *pipe, struct page *page, 25347b7fcaeSAl Viro unsigned int offset, unsigned int size) 25447b7fcaeSAl Viro { 25547b7fcaeSAl Viro struct pipe_buffer *buf = pipe_buf(pipe, pipe->head++); 25647b7fcaeSAl Viro *buf = (struct pipe_buffer) { 25747b7fcaeSAl Viro .ops = &page_cache_pipe_buf_ops, 25847b7fcaeSAl Viro .page = page, 25947b7fcaeSAl Viro .offset = offset, 26047b7fcaeSAl Viro .len = size 26147b7fcaeSAl Viro }; 26247b7fcaeSAl Viro get_page(page); 26347b7fcaeSAl Viro } 26447b7fcaeSAl Viro 26510f525a8SAl Viro static inline int last_offset(const struct pipe_buffer *buf) 2668fad7767SAl Viro { 26710f525a8SAl Viro if (buf->ops == &default_pipe_buf_ops) 26810f525a8SAl Viro return buf->len; // buf->offset is 0 for those 26910f525a8SAl Viro else 27010f525a8SAl Viro return -(buf->offset + buf->len); 2718fad7767SAl Viro } 2728fad7767SAl Viro 2738fad7767SAl Viro static struct page *append_pipe(struct iov_iter *i, size_t size, 2748fad7767SAl Viro unsigned int *off) 2758fad7767SAl Viro { 2768fad7767SAl Viro struct pipe_inode_info *pipe = i->pipe; 27710f525a8SAl Viro int offset = i->last_offset; 2788fad7767SAl Viro struct pipe_buffer *buf; 2798fad7767SAl Viro struct page *page; 2808fad7767SAl Viro 28110f525a8SAl Viro if (offset > 0 && offset < PAGE_SIZE) { 28210f525a8SAl Viro // some space in the last buffer; add to it 2838fad7767SAl Viro buf = pipe_buf(pipe, pipe->head - 1); 2848fad7767SAl Viro size = min_t(size_t, size, PAGE_SIZE - offset); 2858fad7767SAl Viro buf->len += size; 28610f525a8SAl Viro i->last_offset += size; 2878fad7767SAl Viro i->count -= size; 2888fad7767SAl Viro *off = offset; 2898fad7767SAl Viro return buf->page; 2908fad7767SAl Viro } 2918fad7767SAl Viro // OK, we need a new buffer 2928fad7767SAl Viro *off = 0; 2938fad7767SAl Viro size = min_t(size_t, size, PAGE_SIZE); 2948fad7767SAl Viro if (pipe_full(pipe->head, pipe->tail, pipe->max_usage)) 2958fad7767SAl Viro return NULL; 2968fad7767SAl Viro page = push_anon(pipe, size); 2978fad7767SAl Viro if (!page) 2988fad7767SAl Viro return NULL; 2998fad7767SAl Viro i->head = pipe->head - 1; 30010f525a8SAl Viro i->last_offset = size; 3018fad7767SAl Viro i->count -= size; 3028fad7767SAl Viro return page; 3038fad7767SAl Viro } 3048fad7767SAl Viro 305241699cdSAl Viro static size_t copy_page_to_iter_pipe(struct page *page, size_t offset, size_t bytes, 306241699cdSAl Viro struct iov_iter *i) 307241699cdSAl Viro { 308241699cdSAl Viro struct pipe_inode_info *pipe = i->pipe; 30947b7fcaeSAl Viro unsigned int head = pipe->head; 310241699cdSAl Viro 311241699cdSAl Viro if (unlikely(bytes > i->count)) 312241699cdSAl Viro bytes = i->count; 313241699cdSAl Viro 314241699cdSAl Viro if (unlikely(!bytes)) 315241699cdSAl Viro return 0; 316241699cdSAl Viro 317241699cdSAl Viro if (!sanity(i)) 318241699cdSAl Viro return 0; 319241699cdSAl Viro 32010f525a8SAl Viro if (offset && i->last_offset == -offset) { // could we merge it? 32147b7fcaeSAl Viro struct pipe_buffer *buf = pipe_buf(pipe, head - 1); 32247b7fcaeSAl Viro if (buf->page == page) { 323241699cdSAl Viro buf->len += bytes; 32410f525a8SAl Viro i->last_offset -= bytes; 32547b7fcaeSAl Viro i->count -= bytes; 32647b7fcaeSAl Viro return bytes; 327241699cdSAl Viro } 328241699cdSAl Viro } 32947b7fcaeSAl Viro if (pipe_full(pipe->head, pipe->tail, pipe->max_usage)) 330241699cdSAl Viro return 0; 3318cefc107SDavid Howells 33247b7fcaeSAl Viro push_page(pipe, page, offset, bytes); 33310f525a8SAl Viro i->last_offset = -(offset + bytes); 33447b7fcaeSAl Viro i->head = head; 335241699cdSAl Viro i->count -= bytes; 336241699cdSAl Viro return bytes; 337241699cdSAl Viro } 338241699cdSAl Viro 339d879cb83SAl Viro /* 340a6294593SAndreas Gruenbacher * fault_in_iov_iter_readable - fault in iov iterator for reading 341a6294593SAndreas Gruenbacher * @i: iterator 342a6294593SAndreas Gruenbacher * @size: maximum length 343171a0203SAnton Altaparmakov * 344a6294593SAndreas Gruenbacher * Fault in one or more iovecs of the given iov_iter, to a maximum length of 345a6294593SAndreas Gruenbacher * @size. For each iovec, fault in each page that constitutes the iovec. 346a6294593SAndreas Gruenbacher * 347a6294593SAndreas Gruenbacher * Returns the number of bytes not faulted in (like copy_to_user() and 348a6294593SAndreas Gruenbacher * copy_from_user()). 349a6294593SAndreas Gruenbacher * 350a6294593SAndreas Gruenbacher * Always returns 0 for non-userspace iterators. 351171a0203SAnton Altaparmakov */ 352a6294593SAndreas Gruenbacher size_t fault_in_iov_iter_readable(const struct iov_iter *i, size_t size) 353171a0203SAnton Altaparmakov { 354fcb14cb1SAl Viro if (iter_is_ubuf(i)) { 355fcb14cb1SAl Viro size_t n = min(size, iov_iter_count(i)); 356fcb14cb1SAl Viro n -= fault_in_readable(i->ubuf + i->iov_offset, n); 357fcb14cb1SAl Viro return size - n; 358fcb14cb1SAl Viro } else if (iter_is_iovec(i)) { 359a6294593SAndreas Gruenbacher size_t count = min(size, iov_iter_count(i)); 3608409a0d2SAl Viro const struct iovec *p; 3618409a0d2SAl Viro size_t skip; 3628409a0d2SAl Viro 363a6294593SAndreas Gruenbacher size -= count; 364a6294593SAndreas Gruenbacher for (p = i->iov, skip = i->iov_offset; count; p++, skip = 0) { 365a6294593SAndreas Gruenbacher size_t len = min(count, p->iov_len - skip); 366a6294593SAndreas Gruenbacher size_t ret; 3678409a0d2SAl Viro 3688409a0d2SAl Viro if (unlikely(!len)) 3698409a0d2SAl Viro continue; 370a6294593SAndreas Gruenbacher ret = fault_in_readable(p->iov_base + skip, len); 371a6294593SAndreas Gruenbacher count -= len - ret; 372a6294593SAndreas Gruenbacher if (ret) 373a6294593SAndreas Gruenbacher break; 3748409a0d2SAl Viro } 375a6294593SAndreas Gruenbacher return count + size; 376171a0203SAnton Altaparmakov } 377171a0203SAnton Altaparmakov return 0; 378171a0203SAnton Altaparmakov } 379a6294593SAndreas Gruenbacher EXPORT_SYMBOL(fault_in_iov_iter_readable); 380171a0203SAnton Altaparmakov 381cdd591fcSAndreas Gruenbacher /* 382cdd591fcSAndreas Gruenbacher * fault_in_iov_iter_writeable - fault in iov iterator for writing 383cdd591fcSAndreas Gruenbacher * @i: iterator 384cdd591fcSAndreas Gruenbacher * @size: maximum length 385cdd591fcSAndreas Gruenbacher * 386cdd591fcSAndreas Gruenbacher * Faults in the iterator using get_user_pages(), i.e., without triggering 387cdd591fcSAndreas Gruenbacher * hardware page faults. This is primarily useful when we already know that 388cdd591fcSAndreas Gruenbacher * some or all of the pages in @i aren't in memory. 389cdd591fcSAndreas Gruenbacher * 390cdd591fcSAndreas Gruenbacher * Returns the number of bytes not faulted in, like copy_to_user() and 391cdd591fcSAndreas Gruenbacher * copy_from_user(). 392cdd591fcSAndreas Gruenbacher * 393cdd591fcSAndreas Gruenbacher * Always returns 0 for non-user-space iterators. 394cdd591fcSAndreas Gruenbacher */ 395cdd591fcSAndreas Gruenbacher size_t fault_in_iov_iter_writeable(const struct iov_iter *i, size_t size) 396cdd591fcSAndreas Gruenbacher { 397fcb14cb1SAl Viro if (iter_is_ubuf(i)) { 398fcb14cb1SAl Viro size_t n = min(size, iov_iter_count(i)); 399fcb14cb1SAl Viro n -= fault_in_safe_writeable(i->ubuf + i->iov_offset, n); 400fcb14cb1SAl Viro return size - n; 401fcb14cb1SAl Viro } else if (iter_is_iovec(i)) { 402cdd591fcSAndreas Gruenbacher size_t count = min(size, iov_iter_count(i)); 403cdd591fcSAndreas Gruenbacher const struct iovec *p; 404cdd591fcSAndreas Gruenbacher size_t skip; 405cdd591fcSAndreas Gruenbacher 406cdd591fcSAndreas Gruenbacher size -= count; 407cdd591fcSAndreas Gruenbacher for (p = i->iov, skip = i->iov_offset; count; p++, skip = 0) { 408cdd591fcSAndreas Gruenbacher size_t len = min(count, p->iov_len - skip); 409cdd591fcSAndreas Gruenbacher size_t ret; 410cdd591fcSAndreas Gruenbacher 411cdd591fcSAndreas Gruenbacher if (unlikely(!len)) 412cdd591fcSAndreas Gruenbacher continue; 413cdd591fcSAndreas Gruenbacher ret = fault_in_safe_writeable(p->iov_base + skip, len); 414cdd591fcSAndreas Gruenbacher count -= len - ret; 415cdd591fcSAndreas Gruenbacher if (ret) 416cdd591fcSAndreas Gruenbacher break; 417cdd591fcSAndreas Gruenbacher } 418cdd591fcSAndreas Gruenbacher return count + size; 419cdd591fcSAndreas Gruenbacher } 420cdd591fcSAndreas Gruenbacher return 0; 421cdd591fcSAndreas Gruenbacher } 422cdd591fcSAndreas Gruenbacher EXPORT_SYMBOL(fault_in_iov_iter_writeable); 423cdd591fcSAndreas Gruenbacher 424aa563d7bSDavid Howells void iov_iter_init(struct iov_iter *i, unsigned int direction, 425d879cb83SAl Viro const struct iovec *iov, unsigned long nr_segs, 426d879cb83SAl Viro size_t count) 427d879cb83SAl Viro { 428aa563d7bSDavid Howells WARN_ON(direction & ~(READ | WRITE)); 4298cd54c1cSAl Viro *i = (struct iov_iter) { 4308cd54c1cSAl Viro .iter_type = ITER_IOVEC, 4313337ab08SAndreas Gruenbacher .nofault = false, 432fcb14cb1SAl Viro .user_backed = true, 4338cd54c1cSAl Viro .data_source = direction, 4348cd54c1cSAl Viro .iov = iov, 4358cd54c1cSAl Viro .nr_segs = nr_segs, 4368cd54c1cSAl Viro .iov_offset = 0, 4378cd54c1cSAl Viro .count = count 4388cd54c1cSAl Viro }; 439d879cb83SAl Viro } 440d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_init); 441d879cb83SAl Viro 44212d426abSAl Viro // returns the offset in partial buffer (if any) 44312d426abSAl Viro static inline unsigned int pipe_npages(const struct iov_iter *i, int *npages) 444241699cdSAl Viro { 44512d426abSAl Viro struct pipe_inode_info *pipe = i->pipe; 44612d426abSAl Viro int used = pipe->head - pipe->tail; 44710f525a8SAl Viro int off = i->last_offset; 4488cefc107SDavid Howells 44912d426abSAl Viro *npages = max((int)pipe->max_usage - used, 0); 45012d426abSAl Viro 45110f525a8SAl Viro if (off > 0 && off < PAGE_SIZE) { // anon and not full 45212d426abSAl Viro (*npages)++; 45312d426abSAl Viro return off; 45410f525a8SAl Viro } 45512d426abSAl Viro return 0; 456241699cdSAl Viro } 457241699cdSAl Viro 458241699cdSAl Viro static size_t copy_pipe_to_iter(const void *addr, size_t bytes, 459241699cdSAl Viro struct iov_iter *i) 460241699cdSAl Viro { 4618fad7767SAl Viro unsigned int off, chunk; 4628fad7767SAl Viro 4638fad7767SAl Viro if (unlikely(bytes > i->count)) 4648fad7767SAl Viro bytes = i->count; 4658fad7767SAl Viro if (unlikely(!bytes)) 4668fad7767SAl Viro return 0; 467241699cdSAl Viro 468241699cdSAl Viro if (!sanity(i)) 469241699cdSAl Viro return 0; 470241699cdSAl Viro 4718fad7767SAl Viro for (size_t n = bytes; n; n -= chunk) { 4728fad7767SAl Viro struct page *page = append_pipe(i, n, &off); 4738fad7767SAl Viro chunk = min_t(size_t, n, PAGE_SIZE - off); 4748fad7767SAl Viro if (!page) 4758fad7767SAl Viro return bytes - n; 4768fad7767SAl Viro memcpy_to_page(page, off, addr, chunk); 477241699cdSAl Viro addr += chunk; 4788fad7767SAl Viro } 479241699cdSAl Viro return bytes; 480241699cdSAl Viro } 481241699cdSAl Viro 482f9152895SAl Viro static __wsum csum_and_memcpy(void *to, const void *from, size_t len, 483f9152895SAl Viro __wsum sum, size_t off) 484f9152895SAl Viro { 485cc44c17bSAl Viro __wsum next = csum_partial_copy_nocheck(from, to, len); 486f9152895SAl Viro return csum_block_add(sum, next, off); 487f9152895SAl Viro } 488f9152895SAl Viro 48978e1f386SAl Viro static size_t csum_and_copy_to_pipe_iter(const void *addr, size_t bytes, 4906852df12SAl Viro struct iov_iter *i, __wsum *sump) 49178e1f386SAl Viro { 4926852df12SAl Viro __wsum sum = *sump; 4936852df12SAl Viro size_t off = 0; 4948fad7767SAl Viro unsigned int chunk, r; 4958fad7767SAl Viro 4968fad7767SAl Viro if (unlikely(bytes > i->count)) 4978fad7767SAl Viro bytes = i->count; 4988fad7767SAl Viro if (unlikely(!bytes)) 4998fad7767SAl Viro return 0; 50078e1f386SAl Viro 50178e1f386SAl Viro if (!sanity(i)) 50278e1f386SAl Viro return 0; 50378e1f386SAl Viro 5046852df12SAl Viro while (bytes) { 5058fad7767SAl Viro struct page *page = append_pipe(i, bytes, &r); 5068fad7767SAl Viro char *p; 5078fad7767SAl Viro 5088fad7767SAl Viro if (!page) 5098fad7767SAl Viro break; 5108fad7767SAl Viro chunk = min_t(size_t, bytes, PAGE_SIZE - r); 5118fad7767SAl Viro p = kmap_local_page(page); 5126852df12SAl Viro sum = csum_and_memcpy(p + r, addr + off, chunk, sum, off); 5132495bdccSAl Viro kunmap_local(p); 51478e1f386SAl Viro off += chunk; 5158fad7767SAl Viro bytes -= chunk; 5166852df12SAl Viro } 5176852df12SAl Viro *sump = sum; 5186852df12SAl Viro return off; 51978e1f386SAl Viro } 52078e1f386SAl Viro 521aa28de27SAl Viro size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i) 522d879cb83SAl Viro { 523a41dad90SAl Viro if (WARN_ON_ONCE(i->data_source)) 524a41dad90SAl Viro return 0; 52500e23707SDavid Howells if (unlikely(iov_iter_is_pipe(i))) 526241699cdSAl Viro return copy_pipe_to_iter(addr, bytes, i); 527fcb14cb1SAl Viro if (user_backed_iter(i)) 52809fc68dcSAl Viro might_fault(); 5297baa5099SAl Viro iterate_and_advance(i, bytes, base, len, off, 5307baa5099SAl Viro copyout(base, addr + off, len), 5317baa5099SAl Viro memcpy(base, addr + off, len) 532d879cb83SAl Viro ) 533d879cb83SAl Viro 534d879cb83SAl Viro return bytes; 535d879cb83SAl Viro } 536aa28de27SAl Viro EXPORT_SYMBOL(_copy_to_iter); 537d879cb83SAl Viro 538ec6347bbSDan Williams #ifdef CONFIG_ARCH_HAS_COPY_MC 539ec6347bbSDan Williams static int copyout_mc(void __user *to, const void *from, size_t n) 5408780356eSDan Williams { 54196d4f267SLinus Torvalds if (access_ok(to, n)) { 542d0ef4c36SMarco Elver instrument_copy_to_user(to, from, n); 543ec6347bbSDan Williams n = copy_mc_to_user((__force void *) to, from, n); 5448780356eSDan Williams } 5458780356eSDan Williams return n; 5468780356eSDan Williams } 5478780356eSDan Williams 548ec6347bbSDan Williams static size_t copy_mc_pipe_to_iter(const void *addr, size_t bytes, 549ca146f6fSDan Williams struct iov_iter *i) 550ca146f6fSDan Williams { 5518fad7767SAl Viro size_t xfer = 0; 5528fad7767SAl Viro unsigned int off, chunk; 5538fad7767SAl Viro 5548fad7767SAl Viro if (unlikely(bytes > i->count)) 5558fad7767SAl Viro bytes = i->count; 5568fad7767SAl Viro if (unlikely(!bytes)) 5578fad7767SAl Viro return 0; 558ca146f6fSDan Williams 559ca146f6fSDan Williams if (!sanity(i)) 560ca146f6fSDan Williams return 0; 561ca146f6fSDan Williams 5628fad7767SAl Viro while (bytes) { 5638fad7767SAl Viro struct page *page = append_pipe(i, bytes, &off); 564ca146f6fSDan Williams unsigned long rem; 5658fad7767SAl Viro char *p; 5668fad7767SAl Viro 5678fad7767SAl Viro if (!page) 5688fad7767SAl Viro break; 5698fad7767SAl Viro chunk = min_t(size_t, bytes, PAGE_SIZE - off); 5708fad7767SAl Viro p = kmap_local_page(page); 5712a510a74SAl Viro rem = copy_mc_to_kernel(p + off, addr + xfer, chunk); 5722a510a74SAl Viro chunk -= rem; 5732a510a74SAl Viro kunmap_local(p); 5742a510a74SAl Viro xfer += chunk; 5758fad7767SAl Viro bytes -= chunk; 576c3497fd0SAl Viro if (rem) { 5778fad7767SAl Viro iov_iter_revert(i, rem); 578ca146f6fSDan Williams break; 579c3497fd0SAl Viro } 5802a510a74SAl Viro } 581ca146f6fSDan Williams return xfer; 582ca146f6fSDan Williams } 583ca146f6fSDan Williams 584bf3eeb9bSDan Williams /** 585ec6347bbSDan Williams * _copy_mc_to_iter - copy to iter with source memory error exception handling 586bf3eeb9bSDan Williams * @addr: source kernel address 587bf3eeb9bSDan Williams * @bytes: total transfer length 58844e55997SRandy Dunlap * @i: destination iterator 589bf3eeb9bSDan Williams * 590ec6347bbSDan Williams * The pmem driver deploys this for the dax operation 591ec6347bbSDan Williams * (dax_copy_to_iter()) for dax reads (bypass page-cache and the 592ec6347bbSDan Williams * block-layer). Upon #MC read(2) aborts and returns EIO or the bytes 593ec6347bbSDan Williams * successfully copied. 594bf3eeb9bSDan Williams * 595ec6347bbSDan Williams * The main differences between this and typical _copy_to_iter(). 596bf3eeb9bSDan Williams * 597bf3eeb9bSDan Williams * * Typical tail/residue handling after a fault retries the copy 598bf3eeb9bSDan Williams * byte-by-byte until the fault happens again. Re-triggering machine 599bf3eeb9bSDan Williams * checks is potentially fatal so the implementation uses source 600bf3eeb9bSDan Williams * alignment and poison alignment assumptions to avoid re-triggering 601bf3eeb9bSDan Williams * hardware exceptions. 602bf3eeb9bSDan Williams * 603bf3eeb9bSDan Williams * * ITER_KVEC, ITER_PIPE, and ITER_BVEC can return short copies. 604bf3eeb9bSDan Williams * Compare to copy_to_iter() where only ITER_IOVEC attempts might return 605bf3eeb9bSDan Williams * a short copy. 60644e55997SRandy Dunlap * 60744e55997SRandy Dunlap * Return: number of bytes copied (may be %0) 608bf3eeb9bSDan Williams */ 609ec6347bbSDan Williams size_t _copy_mc_to_iter(const void *addr, size_t bytes, struct iov_iter *i) 6108780356eSDan Williams { 611a41dad90SAl Viro if (WARN_ON_ONCE(i->data_source)) 612a41dad90SAl Viro return 0; 61300e23707SDavid Howells if (unlikely(iov_iter_is_pipe(i))) 614ec6347bbSDan Williams return copy_mc_pipe_to_iter(addr, bytes, i); 615fcb14cb1SAl Viro if (user_backed_iter(i)) 6168780356eSDan Williams might_fault(); 6177baa5099SAl Viro __iterate_and_advance(i, bytes, base, len, off, 6187baa5099SAl Viro copyout_mc(base, addr + off, len), 6197baa5099SAl Viro copy_mc_to_kernel(base, addr + off, len) 6208780356eSDan Williams ) 6218780356eSDan Williams 6228780356eSDan Williams return bytes; 6238780356eSDan Williams } 624ec6347bbSDan Williams EXPORT_SYMBOL_GPL(_copy_mc_to_iter); 625ec6347bbSDan Williams #endif /* CONFIG_ARCH_HAS_COPY_MC */ 6268780356eSDan Williams 627aa28de27SAl Viro size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i) 628d879cb83SAl Viro { 629a41dad90SAl Viro if (WARN_ON_ONCE(!i->data_source)) 630241699cdSAl Viro return 0; 631a41dad90SAl Viro 632fcb14cb1SAl Viro if (user_backed_iter(i)) 63309fc68dcSAl Viro might_fault(); 6347baa5099SAl Viro iterate_and_advance(i, bytes, base, len, off, 6357baa5099SAl Viro copyin(addr + off, base, len), 6367baa5099SAl Viro memcpy(addr + off, base, len) 637d879cb83SAl Viro ) 638d879cb83SAl Viro 639d879cb83SAl Viro return bytes; 640d879cb83SAl Viro } 641aa28de27SAl Viro EXPORT_SYMBOL(_copy_from_iter); 642d879cb83SAl Viro 643aa28de27SAl Viro size_t _copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i) 644d879cb83SAl Viro { 645a41dad90SAl Viro if (WARN_ON_ONCE(!i->data_source)) 646241699cdSAl Viro return 0; 647a41dad90SAl Viro 6487baa5099SAl Viro iterate_and_advance(i, bytes, base, len, off, 6497baa5099SAl Viro __copy_from_user_inatomic_nocache(addr + off, base, len), 6507baa5099SAl Viro memcpy(addr + off, base, len) 651d879cb83SAl Viro ) 652d879cb83SAl Viro 653d879cb83SAl Viro return bytes; 654d879cb83SAl Viro } 655aa28de27SAl Viro EXPORT_SYMBOL(_copy_from_iter_nocache); 656d879cb83SAl Viro 6570aed55afSDan Williams #ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE 658abd08d7dSDan Williams /** 659abd08d7dSDan Williams * _copy_from_iter_flushcache - write destination through cpu cache 660abd08d7dSDan Williams * @addr: destination kernel address 661abd08d7dSDan Williams * @bytes: total transfer length 66244e55997SRandy Dunlap * @i: source iterator 663abd08d7dSDan Williams * 664abd08d7dSDan Williams * The pmem driver arranges for filesystem-dax to use this facility via 665abd08d7dSDan Williams * dax_copy_from_iter() for ensuring that writes to persistent memory 666abd08d7dSDan Williams * are flushed through the CPU cache. It is differentiated from 667abd08d7dSDan Williams * _copy_from_iter_nocache() in that guarantees all data is flushed for 668abd08d7dSDan Williams * all iterator types. The _copy_from_iter_nocache() only attempts to 669abd08d7dSDan Williams * bypass the cache for the ITER_IOVEC case, and on some archs may use 670abd08d7dSDan Williams * instructions that strand dirty-data in the cache. 67144e55997SRandy Dunlap * 67244e55997SRandy Dunlap * Return: number of bytes copied (may be %0) 673abd08d7dSDan Williams */ 6746a37e940SLinus Torvalds size_t _copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i) 6750aed55afSDan Williams { 676a41dad90SAl Viro if (WARN_ON_ONCE(!i->data_source)) 6770aed55afSDan Williams return 0; 678a41dad90SAl Viro 6797baa5099SAl Viro iterate_and_advance(i, bytes, base, len, off, 6807baa5099SAl Viro __copy_from_user_flushcache(addr + off, base, len), 6817baa5099SAl Viro memcpy_flushcache(addr + off, base, len) 6820aed55afSDan Williams ) 6830aed55afSDan Williams 6840aed55afSDan Williams return bytes; 6850aed55afSDan Williams } 6866a37e940SLinus Torvalds EXPORT_SYMBOL_GPL(_copy_from_iter_flushcache); 6870aed55afSDan Williams #endif 6880aed55afSDan Williams 68972e809edSAl Viro static inline bool page_copy_sane(struct page *page, size_t offset, size_t n) 69072e809edSAl Viro { 6916daef95bSEric Dumazet struct page *head; 6926daef95bSEric Dumazet size_t v = n + offset; 6936daef95bSEric Dumazet 6946daef95bSEric Dumazet /* 6956daef95bSEric Dumazet * The general case needs to access the page order in order 6966daef95bSEric Dumazet * to compute the page size. 6976daef95bSEric Dumazet * However, we mostly deal with order-0 pages and thus can 6986daef95bSEric Dumazet * avoid a possible cache line miss for requests that fit all 6996daef95bSEric Dumazet * page orders. 7006daef95bSEric Dumazet */ 7016daef95bSEric Dumazet if (n <= v && v <= PAGE_SIZE) 7026daef95bSEric Dumazet return true; 7036daef95bSEric Dumazet 7046daef95bSEric Dumazet head = compound_head(page); 7056daef95bSEric Dumazet v += (page - head) << PAGE_SHIFT; 706a90bcb86SPetar Penkov 70740a86061SAl Viro if (WARN_ON(n > v || v > page_size(head))) 70872e809edSAl Viro return false; 70940a86061SAl Viro return true; 71072e809edSAl Viro } 711cbbd26b8SAl Viro 71208aa6479SAl Viro size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes, 71308aa6479SAl Viro struct iov_iter *i) 71408aa6479SAl Viro { 71508aa6479SAl Viro size_t res = 0; 71640a86061SAl Viro if (!page_copy_sane(page, offset, bytes)) 71708aa6479SAl Viro return 0; 718a41dad90SAl Viro if (WARN_ON_ONCE(i->data_source)) 719a41dad90SAl Viro return 0; 720f0f6b614SAl Viro if (unlikely(iov_iter_is_pipe(i))) 721f0f6b614SAl Viro return copy_page_to_iter_pipe(page, offset, bytes, i); 72208aa6479SAl Viro page += offset / PAGE_SIZE; // first subpage 72308aa6479SAl Viro offset %= PAGE_SIZE; 72408aa6479SAl Viro while (1) { 725f0f6b614SAl Viro void *kaddr = kmap_local_page(page); 726f0f6b614SAl Viro size_t n = min(bytes, (size_t)PAGE_SIZE - offset); 727f0f6b614SAl Viro n = _copy_to_iter(kaddr + offset, n, i); 728f0f6b614SAl Viro kunmap_local(kaddr); 72908aa6479SAl Viro res += n; 73008aa6479SAl Viro bytes -= n; 73108aa6479SAl Viro if (!bytes || !n) 73208aa6479SAl Viro break; 73308aa6479SAl Viro offset += n; 73408aa6479SAl Viro if (offset == PAGE_SIZE) { 73508aa6479SAl Viro page++; 73608aa6479SAl Viro offset = 0; 73708aa6479SAl Viro } 73808aa6479SAl Viro } 73908aa6479SAl Viro return res; 74008aa6479SAl Viro } 741d879cb83SAl Viro EXPORT_SYMBOL(copy_page_to_iter); 742d879cb83SAl Viro 743d879cb83SAl Viro size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes, 744d879cb83SAl Viro struct iov_iter *i) 745d879cb83SAl Viro { 746c03f05f1SAl Viro size_t res = 0; 747c03f05f1SAl Viro if (!page_copy_sane(page, offset, bytes)) 74828f38db7SAl Viro return 0; 749c03f05f1SAl Viro page += offset / PAGE_SIZE; // first subpage 750c03f05f1SAl Viro offset %= PAGE_SIZE; 751c03f05f1SAl Viro while (1) { 752c03f05f1SAl Viro void *kaddr = kmap_local_page(page); 753c03f05f1SAl Viro size_t n = min(bytes, (size_t)PAGE_SIZE - offset); 754c03f05f1SAl Viro n = _copy_from_iter(kaddr + offset, n, i); 755c03f05f1SAl Viro kunmap_local(kaddr); 756c03f05f1SAl Viro res += n; 757c03f05f1SAl Viro bytes -= n; 758c03f05f1SAl Viro if (!bytes || !n) 759c03f05f1SAl Viro break; 760c03f05f1SAl Viro offset += n; 761c03f05f1SAl Viro if (offset == PAGE_SIZE) { 762c03f05f1SAl Viro page++; 763c03f05f1SAl Viro offset = 0; 764c03f05f1SAl Viro } 765c03f05f1SAl Viro } 766c03f05f1SAl Viro return res; 767d879cb83SAl Viro } 768d879cb83SAl Viro EXPORT_SYMBOL(copy_page_from_iter); 769d879cb83SAl Viro 770241699cdSAl Viro static size_t pipe_zero(size_t bytes, struct iov_iter *i) 771241699cdSAl Viro { 7728fad7767SAl Viro unsigned int chunk, off; 7738fad7767SAl Viro 7748fad7767SAl Viro if (unlikely(bytes > i->count)) 7758fad7767SAl Viro bytes = i->count; 7768fad7767SAl Viro if (unlikely(!bytes)) 7778fad7767SAl Viro return 0; 778241699cdSAl Viro 779241699cdSAl Viro if (!sanity(i)) 780241699cdSAl Viro return 0; 781241699cdSAl Viro 7828fad7767SAl Viro for (size_t n = bytes; n; n -= chunk) { 7838fad7767SAl Viro struct page *page = append_pipe(i, n, &off); 7848fad7767SAl Viro char *p; 785241699cdSAl Viro 7868fad7767SAl Viro if (!page) 7878fad7767SAl Viro return bytes - n; 7888fad7767SAl Viro chunk = min_t(size_t, n, PAGE_SIZE - off); 7898fad7767SAl Viro p = kmap_local_page(page); 790893839fdSAl Viro memset(p + off, 0, chunk); 791893839fdSAl Viro kunmap_local(p); 7928fad7767SAl Viro } 793241699cdSAl Viro return bytes; 794241699cdSAl Viro } 795241699cdSAl Viro 796d879cb83SAl Viro size_t iov_iter_zero(size_t bytes, struct iov_iter *i) 797d879cb83SAl Viro { 79800e23707SDavid Howells if (unlikely(iov_iter_is_pipe(i))) 799241699cdSAl Viro return pipe_zero(bytes, i); 8007baa5099SAl Viro iterate_and_advance(i, bytes, base, len, count, 8017baa5099SAl Viro clear_user(base, len), 8027baa5099SAl Viro memset(base, 0, len) 803d879cb83SAl Viro ) 804d879cb83SAl Viro 805d879cb83SAl Viro return bytes; 806d879cb83SAl Viro } 807d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_zero); 808d879cb83SAl Viro 809f0b65f39SAl Viro size_t copy_page_from_iter_atomic(struct page *page, unsigned offset, size_t bytes, 810f0b65f39SAl Viro struct iov_iter *i) 811d879cb83SAl Viro { 812d879cb83SAl Viro char *kaddr = kmap_atomic(page), *p = kaddr + offset; 81340a86061SAl Viro if (!page_copy_sane(page, offset, bytes)) { 81472e809edSAl Viro kunmap_atomic(kaddr); 81572e809edSAl Viro return 0; 81672e809edSAl Viro } 817a41dad90SAl Viro if (WARN_ON_ONCE(!i->data_source)) { 818241699cdSAl Viro kunmap_atomic(kaddr); 819241699cdSAl Viro return 0; 820241699cdSAl Viro } 8217baa5099SAl Viro iterate_and_advance(i, bytes, base, len, off, 8227baa5099SAl Viro copyin(p + off, base, len), 8237baa5099SAl Viro memcpy(p + off, base, len) 824d879cb83SAl Viro ) 825d879cb83SAl Viro kunmap_atomic(kaddr); 826d879cb83SAl Viro return bytes; 827d879cb83SAl Viro } 828f0b65f39SAl Viro EXPORT_SYMBOL(copy_page_from_iter_atomic); 829d879cb83SAl Viro 830b9dc6f65SAl Viro static void pipe_advance(struct iov_iter *i, size_t size) 831b9dc6f65SAl Viro { 832b9dc6f65SAl Viro struct pipe_inode_info *pipe = i->pipe; 83310f525a8SAl Viro int off = i->last_offset; 8348cefc107SDavid Howells 8352c855de9SAl Viro if (!off && !size) { 8362c855de9SAl Viro pipe_discard_from(pipe, i->start_head); // discard everything 8372c855de9SAl Viro return; 838b9dc6f65SAl Viro } 839b9dc6f65SAl Viro i->count -= size; 8402c855de9SAl Viro while (1) { 8412c855de9SAl Viro struct pipe_buffer *buf = pipe_buf(pipe, i->head); 8422c855de9SAl Viro if (off) /* make it relative to the beginning of buffer */ 84310f525a8SAl Viro size += abs(off) - buf->offset; 8442c855de9SAl Viro if (size <= buf->len) { 8452c855de9SAl Viro buf->len = size; 84610f525a8SAl Viro i->last_offset = last_offset(buf); 8472c855de9SAl Viro break; 8482c855de9SAl Viro } 8492c855de9SAl Viro size -= buf->len; 8502c855de9SAl Viro i->head++; 8512c855de9SAl Viro off = 0; 8522c855de9SAl Viro } 8532c855de9SAl Viro pipe_discard_from(pipe, i->head + 1); // discard everything past this one 854241699cdSAl Viro } 855241699cdSAl Viro 85654c8195bSPavel Begunkov static void iov_iter_bvec_advance(struct iov_iter *i, size_t size) 85754c8195bSPavel Begunkov { 85818fa9af7SAl Viro const struct bio_vec *bvec, *end; 85954c8195bSPavel Begunkov 86018fa9af7SAl Viro if (!i->count) 86118fa9af7SAl Viro return; 86218fa9af7SAl Viro i->count -= size; 86354c8195bSPavel Begunkov 86418fa9af7SAl Viro size += i->iov_offset; 86518fa9af7SAl Viro 86618fa9af7SAl Viro for (bvec = i->bvec, end = bvec + i->nr_segs; bvec < end; bvec++) { 86718fa9af7SAl Viro if (likely(size < bvec->bv_len)) 86818fa9af7SAl Viro break; 86918fa9af7SAl Viro size -= bvec->bv_len; 87018fa9af7SAl Viro } 87118fa9af7SAl Viro i->iov_offset = size; 87218fa9af7SAl Viro i->nr_segs -= bvec - i->bvec; 87318fa9af7SAl Viro i->bvec = bvec; 87454c8195bSPavel Begunkov } 87554c8195bSPavel Begunkov 876185ac4d4SAl Viro static void iov_iter_iovec_advance(struct iov_iter *i, size_t size) 877185ac4d4SAl Viro { 878185ac4d4SAl Viro const struct iovec *iov, *end; 879185ac4d4SAl Viro 880185ac4d4SAl Viro if (!i->count) 881185ac4d4SAl Viro return; 882185ac4d4SAl Viro i->count -= size; 883185ac4d4SAl Viro 884185ac4d4SAl Viro size += i->iov_offset; // from beginning of current segment 885185ac4d4SAl Viro for (iov = i->iov, end = iov + i->nr_segs; iov < end; iov++) { 886185ac4d4SAl Viro if (likely(size < iov->iov_len)) 887185ac4d4SAl Viro break; 888185ac4d4SAl Viro size -= iov->iov_len; 889185ac4d4SAl Viro } 890185ac4d4SAl Viro i->iov_offset = size; 891185ac4d4SAl Viro i->nr_segs -= iov - i->iov; 892185ac4d4SAl Viro i->iov = iov; 893185ac4d4SAl Viro } 894185ac4d4SAl Viro 895d879cb83SAl Viro void iov_iter_advance(struct iov_iter *i, size_t size) 896d879cb83SAl Viro { 8973b3fc051SAl Viro if (unlikely(i->count < size)) 8983b3fc051SAl Viro size = i->count; 899fcb14cb1SAl Viro if (likely(iter_is_ubuf(i)) || unlikely(iov_iter_is_xarray(i))) { 900fcb14cb1SAl Viro i->iov_offset += size; 901fcb14cb1SAl Viro i->count -= size; 902fcb14cb1SAl Viro } else if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i))) { 903185ac4d4SAl Viro /* iovec and kvec have identical layouts */ 904185ac4d4SAl Viro iov_iter_iovec_advance(i, size); 905185ac4d4SAl Viro } else if (iov_iter_is_bvec(i)) { 906185ac4d4SAl Viro iov_iter_bvec_advance(i, size); 907185ac4d4SAl Viro } else if (iov_iter_is_pipe(i)) { 908241699cdSAl Viro pipe_advance(i, size); 909185ac4d4SAl Viro } else if (iov_iter_is_discard(i)) { 910185ac4d4SAl Viro i->count -= size; 9117ff50620SDavid Howells } 912d879cb83SAl Viro } 913d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_advance); 914d879cb83SAl Viro 91527c0e374SAl Viro void iov_iter_revert(struct iov_iter *i, size_t unroll) 91627c0e374SAl Viro { 91727c0e374SAl Viro if (!unroll) 91827c0e374SAl Viro return; 9195b47d59aSAl Viro if (WARN_ON(unroll > MAX_RW_COUNT)) 9205b47d59aSAl Viro return; 92127c0e374SAl Viro i->count += unroll; 92200e23707SDavid Howells if (unlikely(iov_iter_is_pipe(i))) { 92327c0e374SAl Viro struct pipe_inode_info *pipe = i->pipe; 92492acdc4fSAl Viro unsigned int head = pipe->head; 92592acdc4fSAl Viro 92692acdc4fSAl Viro while (head > i->start_head) { 92792acdc4fSAl Viro struct pipe_buffer *b = pipe_buf(pipe, --head); 92892acdc4fSAl Viro if (unroll < b->len) { 92992acdc4fSAl Viro b->len -= unroll; 93010f525a8SAl Viro i->last_offset = last_offset(b); 93192acdc4fSAl Viro i->head = head; 93292acdc4fSAl Viro return; 93327c0e374SAl Viro } 93492acdc4fSAl Viro unroll -= b->len; 93592acdc4fSAl Viro pipe_buf_release(pipe, b); 93692acdc4fSAl Viro pipe->head--; 93727c0e374SAl Viro } 93810f525a8SAl Viro i->last_offset = 0; 93992acdc4fSAl Viro i->head = head; 94027c0e374SAl Viro return; 94127c0e374SAl Viro } 9429ea9ce04SDavid Howells if (unlikely(iov_iter_is_discard(i))) 9439ea9ce04SDavid Howells return; 94427c0e374SAl Viro if (unroll <= i->iov_offset) { 94527c0e374SAl Viro i->iov_offset -= unroll; 94627c0e374SAl Viro return; 94727c0e374SAl Viro } 94827c0e374SAl Viro unroll -= i->iov_offset; 949fcb14cb1SAl Viro if (iov_iter_is_xarray(i) || iter_is_ubuf(i)) { 9507ff50620SDavid Howells BUG(); /* We should never go beyond the start of the specified 9517ff50620SDavid Howells * range since we might then be straying into pages that 9527ff50620SDavid Howells * aren't pinned. 9537ff50620SDavid Howells */ 9547ff50620SDavid Howells } else if (iov_iter_is_bvec(i)) { 95527c0e374SAl Viro const struct bio_vec *bvec = i->bvec; 95627c0e374SAl Viro while (1) { 95727c0e374SAl Viro size_t n = (--bvec)->bv_len; 95827c0e374SAl Viro i->nr_segs++; 95927c0e374SAl Viro if (unroll <= n) { 96027c0e374SAl Viro i->bvec = bvec; 96127c0e374SAl Viro i->iov_offset = n - unroll; 96227c0e374SAl Viro return; 96327c0e374SAl Viro } 96427c0e374SAl Viro unroll -= n; 96527c0e374SAl Viro } 96627c0e374SAl Viro } else { /* same logics for iovec and kvec */ 96727c0e374SAl Viro const struct iovec *iov = i->iov; 96827c0e374SAl Viro while (1) { 96927c0e374SAl Viro size_t n = (--iov)->iov_len; 97027c0e374SAl Viro i->nr_segs++; 97127c0e374SAl Viro if (unroll <= n) { 97227c0e374SAl Viro i->iov = iov; 97327c0e374SAl Viro i->iov_offset = n - unroll; 97427c0e374SAl Viro return; 97527c0e374SAl Viro } 97627c0e374SAl Viro unroll -= n; 97727c0e374SAl Viro } 97827c0e374SAl Viro } 97927c0e374SAl Viro } 98027c0e374SAl Viro EXPORT_SYMBOL(iov_iter_revert); 98127c0e374SAl Viro 982d879cb83SAl Viro /* 983d879cb83SAl Viro * Return the count of just the current iov_iter segment. 984d879cb83SAl Viro */ 985d879cb83SAl Viro size_t iov_iter_single_seg_count(const struct iov_iter *i) 986d879cb83SAl Viro { 98728f38db7SAl Viro if (i->nr_segs > 1) { 98828f38db7SAl Viro if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i))) 98928f38db7SAl Viro return min(i->count, i->iov->iov_len - i->iov_offset); 9907ff50620SDavid Howells if (iov_iter_is_bvec(i)) 991d879cb83SAl Viro return min(i->count, i->bvec->bv_len - i->iov_offset); 99228f38db7SAl Viro } 99328f38db7SAl Viro return i->count; 994d879cb83SAl Viro } 995d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_single_seg_count); 996d879cb83SAl Viro 997aa563d7bSDavid Howells void iov_iter_kvec(struct iov_iter *i, unsigned int direction, 998d879cb83SAl Viro const struct kvec *kvec, unsigned long nr_segs, 999d879cb83SAl Viro size_t count) 1000d879cb83SAl Viro { 1001aa563d7bSDavid Howells WARN_ON(direction & ~(READ | WRITE)); 10028cd54c1cSAl Viro *i = (struct iov_iter){ 10038cd54c1cSAl Viro .iter_type = ITER_KVEC, 10048cd54c1cSAl Viro .data_source = direction, 10058cd54c1cSAl Viro .kvec = kvec, 10068cd54c1cSAl Viro .nr_segs = nr_segs, 10078cd54c1cSAl Viro .iov_offset = 0, 10088cd54c1cSAl Viro .count = count 10098cd54c1cSAl Viro }; 1010d879cb83SAl Viro } 1011d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_kvec); 1012d879cb83SAl Viro 1013aa563d7bSDavid Howells void iov_iter_bvec(struct iov_iter *i, unsigned int direction, 1014d879cb83SAl Viro const struct bio_vec *bvec, unsigned long nr_segs, 1015d879cb83SAl Viro size_t count) 1016d879cb83SAl Viro { 1017aa563d7bSDavid Howells WARN_ON(direction & ~(READ | WRITE)); 10188cd54c1cSAl Viro *i = (struct iov_iter){ 10198cd54c1cSAl Viro .iter_type = ITER_BVEC, 10208cd54c1cSAl Viro .data_source = direction, 10218cd54c1cSAl Viro .bvec = bvec, 10228cd54c1cSAl Viro .nr_segs = nr_segs, 10238cd54c1cSAl Viro .iov_offset = 0, 10248cd54c1cSAl Viro .count = count 10258cd54c1cSAl Viro }; 1026d879cb83SAl Viro } 1027d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_bvec); 1028d879cb83SAl Viro 1029aa563d7bSDavid Howells void iov_iter_pipe(struct iov_iter *i, unsigned int direction, 1030241699cdSAl Viro struct pipe_inode_info *pipe, 1031241699cdSAl Viro size_t count) 1032241699cdSAl Viro { 1033aa563d7bSDavid Howells BUG_ON(direction != READ); 10348cefc107SDavid Howells WARN_ON(pipe_full(pipe->head, pipe->tail, pipe->ring_size)); 10358cd54c1cSAl Viro *i = (struct iov_iter){ 10368cd54c1cSAl Viro .iter_type = ITER_PIPE, 10378cd54c1cSAl Viro .data_source = false, 10388cd54c1cSAl Viro .pipe = pipe, 10398cd54c1cSAl Viro .head = pipe->head, 10408cd54c1cSAl Viro .start_head = pipe->head, 104110f525a8SAl Viro .last_offset = 0, 10428cd54c1cSAl Viro .count = count 10438cd54c1cSAl Viro }; 1044241699cdSAl Viro } 1045241699cdSAl Viro EXPORT_SYMBOL(iov_iter_pipe); 1046241699cdSAl Viro 10479ea9ce04SDavid Howells /** 10487ff50620SDavid Howells * iov_iter_xarray - Initialise an I/O iterator to use the pages in an xarray 10497ff50620SDavid Howells * @i: The iterator to initialise. 10507ff50620SDavid Howells * @direction: The direction of the transfer. 10517ff50620SDavid Howells * @xarray: The xarray to access. 10527ff50620SDavid Howells * @start: The start file position. 10537ff50620SDavid Howells * @count: The size of the I/O buffer in bytes. 10547ff50620SDavid Howells * 10557ff50620SDavid Howells * Set up an I/O iterator to either draw data out of the pages attached to an 10567ff50620SDavid Howells * inode or to inject data into those pages. The pages *must* be prevented 10577ff50620SDavid Howells * from evaporation, either by taking a ref on them or locking them by the 10587ff50620SDavid Howells * caller. 10597ff50620SDavid Howells */ 10607ff50620SDavid Howells void iov_iter_xarray(struct iov_iter *i, unsigned int direction, 10617ff50620SDavid Howells struct xarray *xarray, loff_t start, size_t count) 10627ff50620SDavid Howells { 10637ff50620SDavid Howells BUG_ON(direction & ~1); 10648cd54c1cSAl Viro *i = (struct iov_iter) { 10658cd54c1cSAl Viro .iter_type = ITER_XARRAY, 10668cd54c1cSAl Viro .data_source = direction, 10678cd54c1cSAl Viro .xarray = xarray, 10688cd54c1cSAl Viro .xarray_start = start, 10698cd54c1cSAl Viro .count = count, 10708cd54c1cSAl Viro .iov_offset = 0 10718cd54c1cSAl Viro }; 10727ff50620SDavid Howells } 10737ff50620SDavid Howells EXPORT_SYMBOL(iov_iter_xarray); 10747ff50620SDavid Howells 10757ff50620SDavid Howells /** 10769ea9ce04SDavid Howells * iov_iter_discard - Initialise an I/O iterator that discards data 10779ea9ce04SDavid Howells * @i: The iterator to initialise. 10789ea9ce04SDavid Howells * @direction: The direction of the transfer. 10799ea9ce04SDavid Howells * @count: The size of the I/O buffer in bytes. 10809ea9ce04SDavid Howells * 10819ea9ce04SDavid Howells * Set up an I/O iterator that just discards everything that's written to it. 10829ea9ce04SDavid Howells * It's only available as a READ iterator. 10839ea9ce04SDavid Howells */ 10849ea9ce04SDavid Howells void iov_iter_discard(struct iov_iter *i, unsigned int direction, size_t count) 10859ea9ce04SDavid Howells { 10869ea9ce04SDavid Howells BUG_ON(direction != READ); 10878cd54c1cSAl Viro *i = (struct iov_iter){ 10888cd54c1cSAl Viro .iter_type = ITER_DISCARD, 10898cd54c1cSAl Viro .data_source = false, 10908cd54c1cSAl Viro .count = count, 10918cd54c1cSAl Viro .iov_offset = 0 10928cd54c1cSAl Viro }; 10939ea9ce04SDavid Howells } 10949ea9ce04SDavid Howells EXPORT_SYMBOL(iov_iter_discard); 10959ea9ce04SDavid Howells 1096cfa320f7SKeith Busch static bool iov_iter_aligned_iovec(const struct iov_iter *i, unsigned addr_mask, 1097cfa320f7SKeith Busch unsigned len_mask) 1098cfa320f7SKeith Busch { 1099cfa320f7SKeith Busch size_t size = i->count; 1100cfa320f7SKeith Busch size_t skip = i->iov_offset; 1101cfa320f7SKeith Busch unsigned k; 1102cfa320f7SKeith Busch 1103cfa320f7SKeith Busch for (k = 0; k < i->nr_segs; k++, skip = 0) { 1104cfa320f7SKeith Busch size_t len = i->iov[k].iov_len - skip; 1105cfa320f7SKeith Busch 1106cfa320f7SKeith Busch if (len > size) 1107cfa320f7SKeith Busch len = size; 1108cfa320f7SKeith Busch if (len & len_mask) 1109cfa320f7SKeith Busch return false; 1110cfa320f7SKeith Busch if ((unsigned long)(i->iov[k].iov_base + skip) & addr_mask) 1111cfa320f7SKeith Busch return false; 1112cfa320f7SKeith Busch 1113cfa320f7SKeith Busch size -= len; 1114cfa320f7SKeith Busch if (!size) 1115cfa320f7SKeith Busch break; 1116cfa320f7SKeith Busch } 1117cfa320f7SKeith Busch return true; 1118cfa320f7SKeith Busch } 1119cfa320f7SKeith Busch 1120cfa320f7SKeith Busch static bool iov_iter_aligned_bvec(const struct iov_iter *i, unsigned addr_mask, 1121cfa320f7SKeith Busch unsigned len_mask) 1122cfa320f7SKeith Busch { 1123cfa320f7SKeith Busch size_t size = i->count; 1124cfa320f7SKeith Busch unsigned skip = i->iov_offset; 1125cfa320f7SKeith Busch unsigned k; 1126cfa320f7SKeith Busch 1127cfa320f7SKeith Busch for (k = 0; k < i->nr_segs; k++, skip = 0) { 1128cfa320f7SKeith Busch size_t len = i->bvec[k].bv_len - skip; 1129cfa320f7SKeith Busch 1130cfa320f7SKeith Busch if (len > size) 1131cfa320f7SKeith Busch len = size; 1132cfa320f7SKeith Busch if (len & len_mask) 1133cfa320f7SKeith Busch return false; 1134cfa320f7SKeith Busch if ((unsigned long)(i->bvec[k].bv_offset + skip) & addr_mask) 1135cfa320f7SKeith Busch return false; 1136cfa320f7SKeith Busch 1137cfa320f7SKeith Busch size -= len; 1138cfa320f7SKeith Busch if (!size) 1139cfa320f7SKeith Busch break; 1140cfa320f7SKeith Busch } 1141cfa320f7SKeith Busch return true; 1142cfa320f7SKeith Busch } 1143cfa320f7SKeith Busch 1144cfa320f7SKeith Busch /** 1145cfa320f7SKeith Busch * iov_iter_is_aligned() - Check if the addresses and lengths of each segments 1146cfa320f7SKeith Busch * are aligned to the parameters. 1147cfa320f7SKeith Busch * 1148cfa320f7SKeith Busch * @i: &struct iov_iter to restore 1149cfa320f7SKeith Busch * @addr_mask: bit mask to check against the iov element's addresses 1150cfa320f7SKeith Busch * @len_mask: bit mask to check against the iov element's lengths 1151cfa320f7SKeith Busch * 1152cfa320f7SKeith Busch * Return: false if any addresses or lengths intersect with the provided masks 1153cfa320f7SKeith Busch */ 1154cfa320f7SKeith Busch bool iov_iter_is_aligned(const struct iov_iter *i, unsigned addr_mask, 1155cfa320f7SKeith Busch unsigned len_mask) 1156cfa320f7SKeith Busch { 1157fcb14cb1SAl Viro if (likely(iter_is_ubuf(i))) { 1158fcb14cb1SAl Viro if (i->count & len_mask) 1159fcb14cb1SAl Viro return false; 1160fcb14cb1SAl Viro if ((unsigned long)(i->ubuf + i->iov_offset) & addr_mask) 1161fcb14cb1SAl Viro return false; 1162fcb14cb1SAl Viro return true; 1163fcb14cb1SAl Viro } 1164fcb14cb1SAl Viro 1165cfa320f7SKeith Busch if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i))) 1166cfa320f7SKeith Busch return iov_iter_aligned_iovec(i, addr_mask, len_mask); 1167cfa320f7SKeith Busch 1168cfa320f7SKeith Busch if (iov_iter_is_bvec(i)) 1169cfa320f7SKeith Busch return iov_iter_aligned_bvec(i, addr_mask, len_mask); 1170cfa320f7SKeith Busch 1171cfa320f7SKeith Busch if (iov_iter_is_pipe(i)) { 1172cfa320f7SKeith Busch size_t size = i->count; 1173cfa320f7SKeith Busch 1174cfa320f7SKeith Busch if (size & len_mask) 1175cfa320f7SKeith Busch return false; 117610f525a8SAl Viro if (size && i->last_offset > 0) { 117710f525a8SAl Viro if (i->last_offset & addr_mask) 1178cfa320f7SKeith Busch return false; 1179cfa320f7SKeith Busch } 1180cfa320f7SKeith Busch 1181cfa320f7SKeith Busch return true; 1182cfa320f7SKeith Busch } 1183cfa320f7SKeith Busch 1184cfa320f7SKeith Busch if (iov_iter_is_xarray(i)) { 1185cfa320f7SKeith Busch if (i->count & len_mask) 1186cfa320f7SKeith Busch return false; 1187cfa320f7SKeith Busch if ((i->xarray_start + i->iov_offset) & addr_mask) 1188cfa320f7SKeith Busch return false; 1189cfa320f7SKeith Busch } 1190cfa320f7SKeith Busch 1191cfa320f7SKeith Busch return true; 1192cfa320f7SKeith Busch } 1193cfa320f7SKeith Busch EXPORT_SYMBOL_GPL(iov_iter_is_aligned); 1194cfa320f7SKeith Busch 11959221d2e3SAl Viro static unsigned long iov_iter_alignment_iovec(const struct iov_iter *i) 1196d879cb83SAl Viro { 1197d879cb83SAl Viro unsigned long res = 0; 1198d879cb83SAl Viro size_t size = i->count; 11999221d2e3SAl Viro size_t skip = i->iov_offset; 12009221d2e3SAl Viro unsigned k; 1201d879cb83SAl Viro 12029221d2e3SAl Viro for (k = 0; k < i->nr_segs; k++, skip = 0) { 12039221d2e3SAl Viro size_t len = i->iov[k].iov_len - skip; 12049221d2e3SAl Viro if (len) { 12059221d2e3SAl Viro res |= (unsigned long)i->iov[k].iov_base + skip; 12069221d2e3SAl Viro if (len > size) 12079221d2e3SAl Viro len = size; 12089221d2e3SAl Viro res |= len; 12099221d2e3SAl Viro size -= len; 12109221d2e3SAl Viro if (!size) 12119221d2e3SAl Viro break; 12129221d2e3SAl Viro } 12139221d2e3SAl Viro } 12149221d2e3SAl Viro return res; 12159221d2e3SAl Viro } 12169221d2e3SAl Viro 12179221d2e3SAl Viro static unsigned long iov_iter_alignment_bvec(const struct iov_iter *i) 12189221d2e3SAl Viro { 12199221d2e3SAl Viro unsigned res = 0; 12209221d2e3SAl Viro size_t size = i->count; 12219221d2e3SAl Viro unsigned skip = i->iov_offset; 12229221d2e3SAl Viro unsigned k; 12239221d2e3SAl Viro 12249221d2e3SAl Viro for (k = 0; k < i->nr_segs; k++, skip = 0) { 12259221d2e3SAl Viro size_t len = i->bvec[k].bv_len - skip; 12269221d2e3SAl Viro res |= (unsigned long)i->bvec[k].bv_offset + skip; 12279221d2e3SAl Viro if (len > size) 12289221d2e3SAl Viro len = size; 12299221d2e3SAl Viro res |= len; 12309221d2e3SAl Viro size -= len; 12319221d2e3SAl Viro if (!size) 12329221d2e3SAl Viro break; 12339221d2e3SAl Viro } 12349221d2e3SAl Viro return res; 12359221d2e3SAl Viro } 12369221d2e3SAl Viro 12379221d2e3SAl Viro unsigned long iov_iter_alignment(const struct iov_iter *i) 12389221d2e3SAl Viro { 1239fcb14cb1SAl Viro if (likely(iter_is_ubuf(i))) { 1240fcb14cb1SAl Viro size_t size = i->count; 1241fcb14cb1SAl Viro if (size) 1242fcb14cb1SAl Viro return ((unsigned long)i->ubuf + i->iov_offset) | size; 1243fcb14cb1SAl Viro return 0; 1244fcb14cb1SAl Viro } 1245fcb14cb1SAl Viro 12469221d2e3SAl Viro /* iovec and kvec have identical layouts */ 12479221d2e3SAl Viro if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i))) 12489221d2e3SAl Viro return iov_iter_alignment_iovec(i); 12499221d2e3SAl Viro 12509221d2e3SAl Viro if (iov_iter_is_bvec(i)) 12519221d2e3SAl Viro return iov_iter_alignment_bvec(i); 12529221d2e3SAl Viro 12539221d2e3SAl Viro if (iov_iter_is_pipe(i)) { 12549221d2e3SAl Viro size_t size = i->count; 1255e0ff126eSJan Kara 125610f525a8SAl Viro if (size && i->last_offset > 0) 125710f525a8SAl Viro return size | i->last_offset; 1258241699cdSAl Viro return size; 1259241699cdSAl Viro } 12609221d2e3SAl Viro 12619221d2e3SAl Viro if (iov_iter_is_xarray(i)) 12623d14ec1fSDavid Howells return (i->xarray_start + i->iov_offset) | i->count; 12639221d2e3SAl Viro 12649221d2e3SAl Viro return 0; 1265d879cb83SAl Viro } 1266d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_alignment); 1267d879cb83SAl Viro 1268357f435dSAl Viro unsigned long iov_iter_gap_alignment(const struct iov_iter *i) 1269357f435dSAl Viro { 1270357f435dSAl Viro unsigned long res = 0; 1271610c7a71SAl Viro unsigned long v = 0; 1272357f435dSAl Viro size_t size = i->count; 1273610c7a71SAl Viro unsigned k; 1274357f435dSAl Viro 1275fcb14cb1SAl Viro if (iter_is_ubuf(i)) 1276fcb14cb1SAl Viro return 0; 1277fcb14cb1SAl Viro 1278610c7a71SAl Viro if (WARN_ON(!iter_is_iovec(i))) 1279241699cdSAl Viro return ~0U; 1280241699cdSAl Viro 1281610c7a71SAl Viro for (k = 0; k < i->nr_segs; k++) { 1282610c7a71SAl Viro if (i->iov[k].iov_len) { 1283610c7a71SAl Viro unsigned long base = (unsigned long)i->iov[k].iov_base; 1284610c7a71SAl Viro if (v) // if not the first one 1285610c7a71SAl Viro res |= base | v; // this start | previous end 1286610c7a71SAl Viro v = base + i->iov[k].iov_len; 1287610c7a71SAl Viro if (size <= i->iov[k].iov_len) 1288610c7a71SAl Viro break; 1289610c7a71SAl Viro size -= i->iov[k].iov_len; 1290610c7a71SAl Viro } 1291610c7a71SAl Viro } 1292357f435dSAl Viro return res; 1293357f435dSAl Viro } 1294357f435dSAl Viro EXPORT_SYMBOL(iov_iter_gap_alignment); 1295357f435dSAl Viro 12963cf42da3SAl Viro static int want_pages_array(struct page ***res, size_t size, 12973cf42da3SAl Viro size_t start, unsigned int maxpages) 1298acbdeb83SAl Viro { 12993cf42da3SAl Viro unsigned int count = DIV_ROUND_UP(size + start, PAGE_SIZE); 13003cf42da3SAl Viro 13013cf42da3SAl Viro if (count > maxpages) 13023cf42da3SAl Viro count = maxpages; 13033cf42da3SAl Viro WARN_ON(!count); // caller should've prevented that 13043cf42da3SAl Viro if (!*res) { 13053cf42da3SAl Viro *res = kvmalloc_array(count, sizeof(struct page *), GFP_KERNEL); 13063cf42da3SAl Viro if (!*res) 13073cf42da3SAl Viro return 0; 13083cf42da3SAl Viro } 13093cf42da3SAl Viro return count; 1310acbdeb83SAl Viro } 1311acbdeb83SAl Viro 131285200084SAl Viro static ssize_t pipe_get_pages(struct iov_iter *i, 131385200084SAl Viro struct page ***pages, size_t maxsize, unsigned maxpages, 131485200084SAl Viro size_t *start) 1315241699cdSAl Viro { 1316746de1f8SAl Viro unsigned int npages, count, off, chunk; 131785200084SAl Viro struct page **p; 1318746de1f8SAl Viro size_t left; 1319241699cdSAl Viro 132085200084SAl Viro if (!sanity(i)) 132185200084SAl Viro return -EFAULT; 132285200084SAl Viro 132385200084SAl Viro *start = off = pipe_npages(i, &npages); 13243cf42da3SAl Viro if (!npages) 13253cf42da3SAl Viro return -EFAULT; 13263cf42da3SAl Viro count = want_pages_array(pages, maxsize, off, min(npages, maxpages)); 13273cf42da3SAl Viro if (!count) 132885200084SAl Viro return -ENOMEM; 13293cf42da3SAl Viro p = *pages; 1330746de1f8SAl Viro for (npages = 0, left = maxsize ; npages < count; npages++, left -= chunk) { 1331746de1f8SAl Viro struct page *page = append_pipe(i, left, &off); 1332e3b42964SAl Viro if (!page) 1333e3b42964SAl Viro break; 1334746de1f8SAl Viro chunk = min_t(size_t, left, PAGE_SIZE - off); 133585200084SAl Viro get_page(*p++ = page); 1336e3b42964SAl Viro } 133785200084SAl Viro if (!npages) 1338241699cdSAl Viro return -EFAULT; 1339746de1f8SAl Viro return maxsize - left; 1340241699cdSAl Viro } 1341241699cdSAl Viro 13427ff50620SDavid Howells static ssize_t iter_xarray_populate_pages(struct page **pages, struct xarray *xa, 13437ff50620SDavid Howells pgoff_t index, unsigned int nr_pages) 13447ff50620SDavid Howells { 13457ff50620SDavid Howells XA_STATE(xas, xa, index); 13467ff50620SDavid Howells struct page *page; 13477ff50620SDavid Howells unsigned int ret = 0; 13487ff50620SDavid Howells 13497ff50620SDavid Howells rcu_read_lock(); 13507ff50620SDavid Howells for (page = xas_load(&xas); page; page = xas_next(&xas)) { 13517ff50620SDavid Howells if (xas_retry(&xas, page)) 13527ff50620SDavid Howells continue; 13537ff50620SDavid Howells 13547ff50620SDavid Howells /* Has the page moved or been split? */ 13557ff50620SDavid Howells if (unlikely(page != xas_reload(&xas))) { 13567ff50620SDavid Howells xas_reset(&xas); 13577ff50620SDavid Howells continue; 13587ff50620SDavid Howells } 13597ff50620SDavid Howells 13607ff50620SDavid Howells pages[ret] = find_subpage(page, xas.xa_index); 13617ff50620SDavid Howells get_page(pages[ret]); 13627ff50620SDavid Howells if (++ret == nr_pages) 13637ff50620SDavid Howells break; 13647ff50620SDavid Howells } 13657ff50620SDavid Howells rcu_read_unlock(); 13667ff50620SDavid Howells return ret; 13677ff50620SDavid Howells } 13687ff50620SDavid Howells 13697ff50620SDavid Howells static ssize_t iter_xarray_get_pages(struct iov_iter *i, 137068fe506fSAl Viro struct page ***pages, size_t maxsize, 13717ff50620SDavid Howells unsigned maxpages, size_t *_start_offset) 13727ff50620SDavid Howells { 13733cf42da3SAl Viro unsigned nr, offset, count; 13743cf42da3SAl Viro pgoff_t index; 13757ff50620SDavid Howells loff_t pos; 13767ff50620SDavid Howells 13777ff50620SDavid Howells pos = i->xarray_start + i->iov_offset; 13787ff50620SDavid Howells index = pos >> PAGE_SHIFT; 13797ff50620SDavid Howells offset = pos & ~PAGE_MASK; 13807ff50620SDavid Howells *_start_offset = offset; 13817ff50620SDavid Howells 13823cf42da3SAl Viro count = want_pages_array(pages, maxsize, offset, maxpages); 13833cf42da3SAl Viro if (!count) 138468fe506fSAl Viro return -ENOMEM; 138568fe506fSAl Viro nr = iter_xarray_populate_pages(*pages, i->xarray, index, count); 13867ff50620SDavid Howells if (nr == 0) 13877ff50620SDavid Howells return 0; 13887ff50620SDavid Howells 1389eba2d3d7SAl Viro maxsize = min_t(size_t, nr * PAGE_SIZE - offset, maxsize); 1390310d9d5aSAl Viro i->iov_offset += maxsize; 1391310d9d5aSAl Viro i->count -= maxsize; 1392eba2d3d7SAl Viro return maxsize; 13937ff50620SDavid Howells } 13947ff50620SDavid Howells 1395fcb14cb1SAl Viro /* must be done on non-empty ITER_UBUF or ITER_IOVEC one */ 1396dd45ab9dSAl Viro static unsigned long first_iovec_segment(const struct iov_iter *i, size_t *size) 13973d671ca6SAl Viro { 13983d671ca6SAl Viro size_t skip; 13993d671ca6SAl Viro long k; 14003d671ca6SAl Viro 1401fcb14cb1SAl Viro if (iter_is_ubuf(i)) 1402fcb14cb1SAl Viro return (unsigned long)i->ubuf + i->iov_offset; 1403fcb14cb1SAl Viro 14043d671ca6SAl Viro for (k = 0, skip = i->iov_offset; k < i->nr_segs; k++, skip = 0) { 14053d671ca6SAl Viro size_t len = i->iov[k].iov_len - skip; 14063d671ca6SAl Viro 14073d671ca6SAl Viro if (unlikely(!len)) 14083d671ca6SAl Viro continue; 140959dbd7d0SAl Viro if (*size > len) 14103d671ca6SAl Viro *size = len; 1411dd45ab9dSAl Viro return (unsigned long)i->iov[k].iov_base + skip; 14123d671ca6SAl Viro } 14133d671ca6SAl Viro BUG(); // if it had been empty, we wouldn't get called 14143d671ca6SAl Viro } 14153d671ca6SAl Viro 14163d671ca6SAl Viro /* must be done on non-empty ITER_BVEC one */ 14173d671ca6SAl Viro static struct page *first_bvec_segment(const struct iov_iter *i, 141859dbd7d0SAl Viro size_t *size, size_t *start) 14193d671ca6SAl Viro { 14203d671ca6SAl Viro struct page *page; 14213d671ca6SAl Viro size_t skip = i->iov_offset, len; 14223d671ca6SAl Viro 14233d671ca6SAl Viro len = i->bvec->bv_len - skip; 142459dbd7d0SAl Viro if (*size > len) 142559dbd7d0SAl Viro *size = len; 14263d671ca6SAl Viro skip += i->bvec->bv_offset; 14273d671ca6SAl Viro page = i->bvec->bv_page + skip / PAGE_SIZE; 1428dda8e5d1SAl Viro *start = skip % PAGE_SIZE; 14293d671ca6SAl Viro return page; 14303d671ca6SAl Viro } 14313d671ca6SAl Viro 143291329559SAl Viro static ssize_t __iov_iter_get_pages_alloc(struct iov_iter *i, 1433d879cb83SAl Viro struct page ***pages, size_t maxsize, 1434d8207640SLogan Gunthorpe unsigned int maxpages, size_t *start, 1435d8207640SLogan Gunthorpe unsigned int gup_flags) 1436d879cb83SAl Viro { 14373cf42da3SAl Viro unsigned int n; 1438d879cb83SAl Viro 1439d879cb83SAl Viro if (maxsize > i->count) 1440d879cb83SAl Viro maxsize = i->count; 14413d671ca6SAl Viro if (!maxsize) 14423d671ca6SAl Viro return 0; 14437392ed17SAl Viro if (maxsize > MAX_RW_COUNT) 14447392ed17SAl Viro maxsize = MAX_RW_COUNT; 1445d879cb83SAl Viro 1446fcb14cb1SAl Viro if (likely(user_backed_iter(i))) { 14473d671ca6SAl Viro unsigned long addr; 14483cf42da3SAl Viro int res; 14499ea9ce04SDavid Howells 14503337ab08SAndreas Gruenbacher if (iov_iter_rw(i) != WRITE) 14513337ab08SAndreas Gruenbacher gup_flags |= FOLL_WRITE; 14523337ab08SAndreas Gruenbacher if (i->nofault) 14533337ab08SAndreas Gruenbacher gup_flags |= FOLL_NOFAULT; 14543337ab08SAndreas Gruenbacher 1455dd45ab9dSAl Viro addr = first_iovec_segment(i, &maxsize); 1456dd45ab9dSAl Viro *start = addr % PAGE_SIZE; 1457dd45ab9dSAl Viro addr &= PAGE_MASK; 14583cf42da3SAl Viro n = want_pages_array(pages, maxsize, *start, maxpages); 14593cf42da3SAl Viro if (!n) 1460d879cb83SAl Viro return -ENOMEM; 1461451c0ba9SAl Viro res = get_user_pages_fast(addr, n, gup_flags, *pages); 146291329559SAl Viro if (unlikely(res <= 0)) 1463d879cb83SAl Viro return res; 1464eba2d3d7SAl Viro maxsize = min_t(size_t, maxsize, res * PAGE_SIZE - *start); 1465eba2d3d7SAl Viro iov_iter_advance(i, maxsize); 1466eba2d3d7SAl Viro return maxsize; 14673d671ca6SAl Viro } 14683d671ca6SAl Viro if (iov_iter_is_bvec(i)) { 1469451c0ba9SAl Viro struct page **p; 14703d671ca6SAl Viro struct page *page; 14713d671ca6SAl Viro 147259dbd7d0SAl Viro page = first_bvec_segment(i, &maxsize, start); 14733cf42da3SAl Viro n = want_pages_array(pages, maxsize, *start, maxpages); 14743cf42da3SAl Viro if (!n) 1475d879cb83SAl Viro return -ENOMEM; 14763cf42da3SAl Viro p = *pages; 1477dda8e5d1SAl Viro for (int k = 0; k < n; k++) 1478eba2d3d7SAl Viro get_page(p[k] = page + k); 1479eba2d3d7SAl Viro maxsize = min_t(size_t, maxsize, n * PAGE_SIZE - *start); 1480310d9d5aSAl Viro i->count -= maxsize; 1481310d9d5aSAl Viro i->iov_offset += maxsize; 1482310d9d5aSAl Viro if (i->iov_offset == i->bvec->bv_len) { 1483310d9d5aSAl Viro i->iov_offset = 0; 1484310d9d5aSAl Viro i->bvec++; 1485310d9d5aSAl Viro i->nr_segs--; 1486310d9d5aSAl Viro } 1487eba2d3d7SAl Viro return maxsize; 14883d671ca6SAl Viro } 14893d671ca6SAl Viro if (iov_iter_is_pipe(i)) 1490451c0ba9SAl Viro return pipe_get_pages(i, pages, maxsize, maxpages, start); 14913d671ca6SAl Viro if (iov_iter_is_xarray(i)) 1492451c0ba9SAl Viro return iter_xarray_get_pages(i, pages, maxsize, maxpages, start); 1493d879cb83SAl Viro return -EFAULT; 1494d879cb83SAl Viro } 149591329559SAl Viro 1496d8207640SLogan Gunthorpe ssize_t iov_iter_get_pages(struct iov_iter *i, 1497451c0ba9SAl Viro struct page **pages, size_t maxsize, unsigned maxpages, 1498d8207640SLogan Gunthorpe size_t *start, unsigned gup_flags) 1499451c0ba9SAl Viro { 1500451c0ba9SAl Viro if (!maxpages) 1501451c0ba9SAl Viro return 0; 1502451c0ba9SAl Viro BUG_ON(!pages); 1503451c0ba9SAl Viro 1504d8207640SLogan Gunthorpe return __iov_iter_get_pages_alloc(i, &pages, maxsize, maxpages, 1505d8207640SLogan Gunthorpe start, gup_flags); 1506d8207640SLogan Gunthorpe } 1507d8207640SLogan Gunthorpe EXPORT_SYMBOL_GPL(iov_iter_get_pages); 1508d8207640SLogan Gunthorpe 1509d8207640SLogan Gunthorpe ssize_t iov_iter_get_pages2(struct iov_iter *i, struct page **pages, 1510d8207640SLogan Gunthorpe size_t maxsize, unsigned maxpages, size_t *start) 1511d8207640SLogan Gunthorpe { 1512d8207640SLogan Gunthorpe return iov_iter_get_pages(i, pages, maxsize, maxpages, start, 0); 1513451c0ba9SAl Viro } 1514eba2d3d7SAl Viro EXPORT_SYMBOL(iov_iter_get_pages2); 1515451c0ba9SAl Viro 1516d8207640SLogan Gunthorpe ssize_t iov_iter_get_pages_alloc(struct iov_iter *i, 151791329559SAl Viro struct page ***pages, size_t maxsize, 1518d8207640SLogan Gunthorpe size_t *start, unsigned gup_flags) 151991329559SAl Viro { 152091329559SAl Viro ssize_t len; 152191329559SAl Viro 152291329559SAl Viro *pages = NULL; 152391329559SAl Viro 1524d8207640SLogan Gunthorpe len = __iov_iter_get_pages_alloc(i, pages, maxsize, ~0U, start, 1525d8207640SLogan Gunthorpe gup_flags); 152691329559SAl Viro if (len <= 0) { 152791329559SAl Viro kvfree(*pages); 152891329559SAl Viro *pages = NULL; 152991329559SAl Viro } 153091329559SAl Viro return len; 153191329559SAl Viro } 1532d8207640SLogan Gunthorpe EXPORT_SYMBOL_GPL(iov_iter_get_pages_alloc); 1533d8207640SLogan Gunthorpe 1534d8207640SLogan Gunthorpe ssize_t iov_iter_get_pages_alloc2(struct iov_iter *i, 1535d8207640SLogan Gunthorpe struct page ***pages, size_t maxsize, size_t *start) 1536d8207640SLogan Gunthorpe { 1537d8207640SLogan Gunthorpe return iov_iter_get_pages_alloc(i, pages, maxsize, start, 0); 1538d8207640SLogan Gunthorpe } 1539eba2d3d7SAl Viro EXPORT_SYMBOL(iov_iter_get_pages_alloc2); 1540d879cb83SAl Viro 1541d879cb83SAl Viro size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum, 1542d879cb83SAl Viro struct iov_iter *i) 1543d879cb83SAl Viro { 1544d879cb83SAl Viro __wsum sum, next; 1545d879cb83SAl Viro sum = *csum; 1546a41dad90SAl Viro if (WARN_ON_ONCE(!i->data_source)) 1547241699cdSAl Viro return 0; 1548a41dad90SAl Viro 15497baa5099SAl Viro iterate_and_advance(i, bytes, base, len, off, ({ 15507baa5099SAl Viro next = csum_and_copy_from_user(base, addr + off, len); 1551d879cb83SAl Viro sum = csum_block_add(sum, next, off); 15527baa5099SAl Viro next ? 0 : len; 1553d879cb83SAl Viro }), ({ 15547baa5099SAl Viro sum = csum_and_memcpy(addr + off, base, len, sum, off); 1555d879cb83SAl Viro }) 1556d879cb83SAl Viro ) 1557d879cb83SAl Viro *csum = sum; 1558d879cb83SAl Viro return bytes; 1559d879cb83SAl Viro } 1560d879cb83SAl Viro EXPORT_SYMBOL(csum_and_copy_from_iter); 1561d879cb83SAl Viro 156252cbd23aSWillem de Bruijn size_t csum_and_copy_to_iter(const void *addr, size_t bytes, void *_csstate, 1563d879cb83SAl Viro struct iov_iter *i) 1564d879cb83SAl Viro { 156552cbd23aSWillem de Bruijn struct csum_state *csstate = _csstate; 1566d879cb83SAl Viro __wsum sum, next; 156778e1f386SAl Viro 1568a41dad90SAl Viro if (WARN_ON_ONCE(i->data_source)) 1569a41dad90SAl Viro return 0; 157078e1f386SAl Viro if (unlikely(iov_iter_is_discard(i))) { 1571c67f1fd2SAl Viro // can't use csum_memcpy() for that one - data is not copied 1572c67f1fd2SAl Viro csstate->csum = csum_block_add(csstate->csum, 1573c67f1fd2SAl Viro csum_partial(addr, bytes, 0), 1574c67f1fd2SAl Viro csstate->off); 1575c67f1fd2SAl Viro csstate->off += bytes; 1576c67f1fd2SAl Viro return bytes; 1577241699cdSAl Viro } 15786852df12SAl Viro 15796852df12SAl Viro sum = csum_shift(csstate->csum, csstate->off); 15806852df12SAl Viro if (unlikely(iov_iter_is_pipe(i))) 15816852df12SAl Viro bytes = csum_and_copy_to_pipe_iter(addr, bytes, i, &sum); 15826852df12SAl Viro else iterate_and_advance(i, bytes, base, len, off, ({ 15837baa5099SAl Viro next = csum_and_copy_to_user(addr + off, base, len); 1584d879cb83SAl Viro sum = csum_block_add(sum, next, off); 15857baa5099SAl Viro next ? 0 : len; 1586d879cb83SAl Viro }), ({ 15877baa5099SAl Viro sum = csum_and_memcpy(base, addr + off, len, sum, off); 1588d879cb83SAl Viro }) 1589d879cb83SAl Viro ) 1590594e450bSAl Viro csstate->csum = csum_shift(sum, csstate->off); 1591594e450bSAl Viro csstate->off += bytes; 1592d879cb83SAl Viro return bytes; 1593d879cb83SAl Viro } 1594d879cb83SAl Viro EXPORT_SYMBOL(csum_and_copy_to_iter); 1595d879cb83SAl Viro 1596d05f4435SSagi Grimberg size_t hash_and_copy_to_iter(const void *addr, size_t bytes, void *hashp, 1597d05f4435SSagi Grimberg struct iov_iter *i) 1598d05f4435SSagi Grimberg { 15997999096fSHerbert Xu #ifdef CONFIG_CRYPTO_HASH 1600d05f4435SSagi Grimberg struct ahash_request *hash = hashp; 1601d05f4435SSagi Grimberg struct scatterlist sg; 1602d05f4435SSagi Grimberg size_t copied; 1603d05f4435SSagi Grimberg 1604d05f4435SSagi Grimberg copied = copy_to_iter(addr, bytes, i); 1605d05f4435SSagi Grimberg sg_init_one(&sg, addr, copied); 1606d05f4435SSagi Grimberg ahash_request_set_crypt(hash, &sg, NULL, copied); 1607d05f4435SSagi Grimberg crypto_ahash_update(hash); 1608d05f4435SSagi Grimberg return copied; 160927fad74aSYueHaibing #else 161027fad74aSYueHaibing return 0; 161127fad74aSYueHaibing #endif 1612d05f4435SSagi Grimberg } 1613d05f4435SSagi Grimberg EXPORT_SYMBOL(hash_and_copy_to_iter); 1614d05f4435SSagi Grimberg 161566531c65SAl Viro static int iov_npages(const struct iov_iter *i, int maxpages) 1616d879cb83SAl Viro { 161766531c65SAl Viro size_t skip = i->iov_offset, size = i->count; 161866531c65SAl Viro const struct iovec *p; 1619d879cb83SAl Viro int npages = 0; 1620d879cb83SAl Viro 162166531c65SAl Viro for (p = i->iov; size; skip = 0, p++) { 162266531c65SAl Viro unsigned offs = offset_in_page(p->iov_base + skip); 162366531c65SAl Viro size_t len = min(p->iov_len - skip, size); 1624d879cb83SAl Viro 162566531c65SAl Viro if (len) { 162666531c65SAl Viro size -= len; 162766531c65SAl Viro npages += DIV_ROUND_UP(offs + len, PAGE_SIZE); 162866531c65SAl Viro if (unlikely(npages > maxpages)) 162966531c65SAl Viro return maxpages; 163066531c65SAl Viro } 163166531c65SAl Viro } 163266531c65SAl Viro return npages; 163366531c65SAl Viro } 163466531c65SAl Viro 163566531c65SAl Viro static int bvec_npages(const struct iov_iter *i, int maxpages) 163666531c65SAl Viro { 163766531c65SAl Viro size_t skip = i->iov_offset, size = i->count; 163866531c65SAl Viro const struct bio_vec *p; 163966531c65SAl Viro int npages = 0; 164066531c65SAl Viro 164166531c65SAl Viro for (p = i->bvec; size; skip = 0, p++) { 164266531c65SAl Viro unsigned offs = (p->bv_offset + skip) % PAGE_SIZE; 164366531c65SAl Viro size_t len = min(p->bv_len - skip, size); 164466531c65SAl Viro 164566531c65SAl Viro size -= len; 164666531c65SAl Viro npages += DIV_ROUND_UP(offs + len, PAGE_SIZE); 164766531c65SAl Viro if (unlikely(npages > maxpages)) 164866531c65SAl Viro return maxpages; 164966531c65SAl Viro } 165066531c65SAl Viro return npages; 165166531c65SAl Viro } 165266531c65SAl Viro 165366531c65SAl Viro int iov_iter_npages(const struct iov_iter *i, int maxpages) 165466531c65SAl Viro { 165566531c65SAl Viro if (unlikely(!i->count)) 165666531c65SAl Viro return 0; 1657fcb14cb1SAl Viro if (likely(iter_is_ubuf(i))) { 1658fcb14cb1SAl Viro unsigned offs = offset_in_page(i->ubuf + i->iov_offset); 1659fcb14cb1SAl Viro int npages = DIV_ROUND_UP(offs + i->count, PAGE_SIZE); 1660fcb14cb1SAl Viro return min(npages, maxpages); 1661fcb14cb1SAl Viro } 166266531c65SAl Viro /* iovec and kvec have identical layouts */ 166366531c65SAl Viro if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i))) 166466531c65SAl Viro return iov_npages(i, maxpages); 166566531c65SAl Viro if (iov_iter_is_bvec(i)) 166666531c65SAl Viro return bvec_npages(i, maxpages); 166766531c65SAl Viro if (iov_iter_is_pipe(i)) { 166866531c65SAl Viro int npages; 1669241699cdSAl Viro 1670241699cdSAl Viro if (!sanity(i)) 1671241699cdSAl Viro return 0; 1672241699cdSAl Viro 167312d426abSAl Viro pipe_npages(i, &npages); 167466531c65SAl Viro return min(npages, maxpages); 167566531c65SAl Viro } 167666531c65SAl Viro if (iov_iter_is_xarray(i)) { 1677e4f8df86SAl Viro unsigned offset = (i->xarray_start + i->iov_offset) % PAGE_SIZE; 1678e4f8df86SAl Viro int npages = DIV_ROUND_UP(offset + i->count, PAGE_SIZE); 167966531c65SAl Viro return min(npages, maxpages); 168066531c65SAl Viro } 168166531c65SAl Viro return 0; 1682d879cb83SAl Viro } 1683d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_npages); 1684d879cb83SAl Viro 1685d879cb83SAl Viro const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags) 1686d879cb83SAl Viro { 1687d879cb83SAl Viro *new = *old; 168800e23707SDavid Howells if (unlikely(iov_iter_is_pipe(new))) { 1689241699cdSAl Viro WARN_ON(1); 1690241699cdSAl Viro return NULL; 1691241699cdSAl Viro } 169200e23707SDavid Howells if (iov_iter_is_bvec(new)) 1693d879cb83SAl Viro return new->bvec = kmemdup(new->bvec, 1694d879cb83SAl Viro new->nr_segs * sizeof(struct bio_vec), 1695d879cb83SAl Viro flags); 1696fcb14cb1SAl Viro else if (iov_iter_is_kvec(new) || iter_is_iovec(new)) 1697d879cb83SAl Viro /* iovec and kvec have identical layout */ 1698d879cb83SAl Viro return new->iov = kmemdup(new->iov, 1699d879cb83SAl Viro new->nr_segs * sizeof(struct iovec), 1700d879cb83SAl Viro flags); 1701fcb14cb1SAl Viro return NULL; 1702d879cb83SAl Viro } 1703d879cb83SAl Viro EXPORT_SYMBOL(dup_iter); 1704bc917be8SAl Viro 1705bfdc5970SChristoph Hellwig static int copy_compat_iovec_from_user(struct iovec *iov, 1706bfdc5970SChristoph Hellwig const struct iovec __user *uvec, unsigned long nr_segs) 1707bfdc5970SChristoph Hellwig { 1708bfdc5970SChristoph Hellwig const struct compat_iovec __user *uiov = 1709bfdc5970SChristoph Hellwig (const struct compat_iovec __user *)uvec; 1710bfdc5970SChristoph Hellwig int ret = -EFAULT, i; 1711bfdc5970SChristoph Hellwig 1712a959a978SChristoph Hellwig if (!user_access_begin(uiov, nr_segs * sizeof(*uiov))) 1713bfdc5970SChristoph Hellwig return -EFAULT; 1714bfdc5970SChristoph Hellwig 1715bfdc5970SChristoph Hellwig for (i = 0; i < nr_segs; i++) { 1716bfdc5970SChristoph Hellwig compat_uptr_t buf; 1717bfdc5970SChristoph Hellwig compat_ssize_t len; 1718bfdc5970SChristoph Hellwig 1719bfdc5970SChristoph Hellwig unsafe_get_user(len, &uiov[i].iov_len, uaccess_end); 1720bfdc5970SChristoph Hellwig unsafe_get_user(buf, &uiov[i].iov_base, uaccess_end); 1721bfdc5970SChristoph Hellwig 1722bfdc5970SChristoph Hellwig /* check for compat_size_t not fitting in compat_ssize_t .. */ 1723bfdc5970SChristoph Hellwig if (len < 0) { 1724bfdc5970SChristoph Hellwig ret = -EINVAL; 1725bfdc5970SChristoph Hellwig goto uaccess_end; 1726bfdc5970SChristoph Hellwig } 1727bfdc5970SChristoph Hellwig iov[i].iov_base = compat_ptr(buf); 1728bfdc5970SChristoph Hellwig iov[i].iov_len = len; 1729bfdc5970SChristoph Hellwig } 1730bfdc5970SChristoph Hellwig 1731bfdc5970SChristoph Hellwig ret = 0; 1732bfdc5970SChristoph Hellwig uaccess_end: 1733bfdc5970SChristoph Hellwig user_access_end(); 1734bfdc5970SChristoph Hellwig return ret; 1735bfdc5970SChristoph Hellwig } 1736bfdc5970SChristoph Hellwig 1737bfdc5970SChristoph Hellwig static int copy_iovec_from_user(struct iovec *iov, 1738bfdc5970SChristoph Hellwig const struct iovec __user *uvec, unsigned long nr_segs) 1739fb041b59SDavid Laight { 1740fb041b59SDavid Laight unsigned long seg; 1741bfdc5970SChristoph Hellwig 1742bfdc5970SChristoph Hellwig if (copy_from_user(iov, uvec, nr_segs * sizeof(*uvec))) 1743bfdc5970SChristoph Hellwig return -EFAULT; 1744bfdc5970SChristoph Hellwig for (seg = 0; seg < nr_segs; seg++) { 1745bfdc5970SChristoph Hellwig if ((ssize_t)iov[seg].iov_len < 0) 1746bfdc5970SChristoph Hellwig return -EINVAL; 1747bfdc5970SChristoph Hellwig } 1748bfdc5970SChristoph Hellwig 1749bfdc5970SChristoph Hellwig return 0; 1750bfdc5970SChristoph Hellwig } 1751bfdc5970SChristoph Hellwig 1752bfdc5970SChristoph Hellwig struct iovec *iovec_from_user(const struct iovec __user *uvec, 1753bfdc5970SChristoph Hellwig unsigned long nr_segs, unsigned long fast_segs, 1754bfdc5970SChristoph Hellwig struct iovec *fast_iov, bool compat) 1755bfdc5970SChristoph Hellwig { 1756bfdc5970SChristoph Hellwig struct iovec *iov = fast_iov; 1757bfdc5970SChristoph Hellwig int ret; 1758fb041b59SDavid Laight 1759fb041b59SDavid Laight /* 1760bfdc5970SChristoph Hellwig * SuS says "The readv() function *may* fail if the iovcnt argument was 1761bfdc5970SChristoph Hellwig * less than or equal to 0, or greater than {IOV_MAX}. Linux has 1762fb041b59SDavid Laight * traditionally returned zero for zero segments, so... 1763fb041b59SDavid Laight */ 1764bfdc5970SChristoph Hellwig if (nr_segs == 0) 1765bfdc5970SChristoph Hellwig return iov; 1766bfdc5970SChristoph Hellwig if (nr_segs > UIO_MAXIOV) 1767bfdc5970SChristoph Hellwig return ERR_PTR(-EINVAL); 1768fb041b59SDavid Laight if (nr_segs > fast_segs) { 1769fb041b59SDavid Laight iov = kmalloc_array(nr_segs, sizeof(struct iovec), GFP_KERNEL); 1770bfdc5970SChristoph Hellwig if (!iov) 1771bfdc5970SChristoph Hellwig return ERR_PTR(-ENOMEM); 1772fb041b59SDavid Laight } 1773bfdc5970SChristoph Hellwig 1774bfdc5970SChristoph Hellwig if (compat) 1775bfdc5970SChristoph Hellwig ret = copy_compat_iovec_from_user(iov, uvec, nr_segs); 1776bfdc5970SChristoph Hellwig else 1777bfdc5970SChristoph Hellwig ret = copy_iovec_from_user(iov, uvec, nr_segs); 1778bfdc5970SChristoph Hellwig if (ret) { 1779bfdc5970SChristoph Hellwig if (iov != fast_iov) 1780bfdc5970SChristoph Hellwig kfree(iov); 1781bfdc5970SChristoph Hellwig return ERR_PTR(ret); 1782fb041b59SDavid Laight } 1783bfdc5970SChristoph Hellwig 1784bfdc5970SChristoph Hellwig return iov; 1785bfdc5970SChristoph Hellwig } 1786bfdc5970SChristoph Hellwig 1787bfdc5970SChristoph Hellwig ssize_t __import_iovec(int type, const struct iovec __user *uvec, 1788bfdc5970SChristoph Hellwig unsigned nr_segs, unsigned fast_segs, struct iovec **iovp, 1789bfdc5970SChristoph Hellwig struct iov_iter *i, bool compat) 1790bfdc5970SChristoph Hellwig { 1791bfdc5970SChristoph Hellwig ssize_t total_len = 0; 1792bfdc5970SChristoph Hellwig unsigned long seg; 1793bfdc5970SChristoph Hellwig struct iovec *iov; 1794bfdc5970SChristoph Hellwig 1795bfdc5970SChristoph Hellwig iov = iovec_from_user(uvec, nr_segs, fast_segs, *iovp, compat); 1796bfdc5970SChristoph Hellwig if (IS_ERR(iov)) { 1797bfdc5970SChristoph Hellwig *iovp = NULL; 1798bfdc5970SChristoph Hellwig return PTR_ERR(iov); 1799fb041b59SDavid Laight } 1800fb041b59SDavid Laight 1801fb041b59SDavid Laight /* 1802bfdc5970SChristoph Hellwig * According to the Single Unix Specification we should return EINVAL if 1803bfdc5970SChristoph Hellwig * an element length is < 0 when cast to ssize_t or if the total length 1804bfdc5970SChristoph Hellwig * would overflow the ssize_t return value of the system call. 1805fb041b59SDavid Laight * 1806fb041b59SDavid Laight * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the 1807fb041b59SDavid Laight * overflow case. 1808fb041b59SDavid Laight */ 1809fb041b59SDavid Laight for (seg = 0; seg < nr_segs; seg++) { 1810fb041b59SDavid Laight ssize_t len = (ssize_t)iov[seg].iov_len; 1811fb041b59SDavid Laight 1812bfdc5970SChristoph Hellwig if (!access_ok(iov[seg].iov_base, len)) { 1813bfdc5970SChristoph Hellwig if (iov != *iovp) 1814bfdc5970SChristoph Hellwig kfree(iov); 1815bfdc5970SChristoph Hellwig *iovp = NULL; 1816bfdc5970SChristoph Hellwig return -EFAULT; 1817fb041b59SDavid Laight } 1818bfdc5970SChristoph Hellwig 1819bfdc5970SChristoph Hellwig if (len > MAX_RW_COUNT - total_len) { 1820bfdc5970SChristoph Hellwig len = MAX_RW_COUNT - total_len; 1821fb041b59SDavid Laight iov[seg].iov_len = len; 1822fb041b59SDavid Laight } 1823bfdc5970SChristoph Hellwig total_len += len; 1824fb041b59SDavid Laight } 1825bfdc5970SChristoph Hellwig 1826bfdc5970SChristoph Hellwig iov_iter_init(i, type, iov, nr_segs, total_len); 1827bfdc5970SChristoph Hellwig if (iov == *iovp) 1828bfdc5970SChristoph Hellwig *iovp = NULL; 1829bfdc5970SChristoph Hellwig else 1830bfdc5970SChristoph Hellwig *iovp = iov; 1831bfdc5970SChristoph Hellwig return total_len; 1832fb041b59SDavid Laight } 1833fb041b59SDavid Laight 1834ffecee4fSVegard Nossum /** 1835ffecee4fSVegard Nossum * import_iovec() - Copy an array of &struct iovec from userspace 1836ffecee4fSVegard Nossum * into the kernel, check that it is valid, and initialize a new 1837ffecee4fSVegard Nossum * &struct iov_iter iterator to access it. 1838ffecee4fSVegard Nossum * 1839ffecee4fSVegard Nossum * @type: One of %READ or %WRITE. 1840bfdc5970SChristoph Hellwig * @uvec: Pointer to the userspace array. 1841ffecee4fSVegard Nossum * @nr_segs: Number of elements in userspace array. 1842ffecee4fSVegard Nossum * @fast_segs: Number of elements in @iov. 1843bfdc5970SChristoph Hellwig * @iovp: (input and output parameter) Pointer to pointer to (usually small 1844ffecee4fSVegard Nossum * on-stack) kernel array. 1845ffecee4fSVegard Nossum * @i: Pointer to iterator that will be initialized on success. 1846ffecee4fSVegard Nossum * 1847ffecee4fSVegard Nossum * If the array pointed to by *@iov is large enough to hold all @nr_segs, 1848ffecee4fSVegard Nossum * then this function places %NULL in *@iov on return. Otherwise, a new 1849ffecee4fSVegard Nossum * array will be allocated and the result placed in *@iov. This means that 1850ffecee4fSVegard Nossum * the caller may call kfree() on *@iov regardless of whether the small 1851ffecee4fSVegard Nossum * on-stack array was used or not (and regardless of whether this function 1852ffecee4fSVegard Nossum * returns an error or not). 1853ffecee4fSVegard Nossum * 185487e5e6daSJens Axboe * Return: Negative error code on error, bytes imported on success 1855ffecee4fSVegard Nossum */ 1856bfdc5970SChristoph Hellwig ssize_t import_iovec(int type, const struct iovec __user *uvec, 1857bc917be8SAl Viro unsigned nr_segs, unsigned fast_segs, 1858bfdc5970SChristoph Hellwig struct iovec **iovp, struct iov_iter *i) 1859bc917be8SAl Viro { 186089cd35c5SChristoph Hellwig return __import_iovec(type, uvec, nr_segs, fast_segs, iovp, i, 186189cd35c5SChristoph Hellwig in_compat_syscall()); 1862bc917be8SAl Viro } 1863bc917be8SAl Viro EXPORT_SYMBOL(import_iovec); 1864bc917be8SAl Viro 1865bc917be8SAl Viro int import_single_range(int rw, void __user *buf, size_t len, 1866bc917be8SAl Viro struct iovec *iov, struct iov_iter *i) 1867bc917be8SAl Viro { 1868bc917be8SAl Viro if (len > MAX_RW_COUNT) 1869bc917be8SAl Viro len = MAX_RW_COUNT; 187096d4f267SLinus Torvalds if (unlikely(!access_ok(buf, len))) 1871bc917be8SAl Viro return -EFAULT; 1872bc917be8SAl Viro 1873bc917be8SAl Viro iov->iov_base = buf; 1874bc917be8SAl Viro iov->iov_len = len; 1875bc917be8SAl Viro iov_iter_init(i, rw, iov, 1, len); 1876bc917be8SAl Viro return 0; 1877bc917be8SAl Viro } 1878e1267585SAl Viro EXPORT_SYMBOL(import_single_range); 18798fb0f47aSJens Axboe 1880*2ad9bd83SJens Axboe int import_ubuf(int rw, void __user *buf, size_t len, struct iov_iter *i) 1881*2ad9bd83SJens Axboe { 1882*2ad9bd83SJens Axboe if (len > MAX_RW_COUNT) 1883*2ad9bd83SJens Axboe len = MAX_RW_COUNT; 1884*2ad9bd83SJens Axboe if (unlikely(!access_ok(buf, len))) 1885*2ad9bd83SJens Axboe return -EFAULT; 1886*2ad9bd83SJens Axboe 1887*2ad9bd83SJens Axboe iov_iter_ubuf(i, rw, buf, len); 1888*2ad9bd83SJens Axboe return 0; 1889*2ad9bd83SJens Axboe } 1890*2ad9bd83SJens Axboe 18918fb0f47aSJens Axboe /** 18928fb0f47aSJens Axboe * iov_iter_restore() - Restore a &struct iov_iter to the same state as when 18938fb0f47aSJens Axboe * iov_iter_save_state() was called. 18948fb0f47aSJens Axboe * 18958fb0f47aSJens Axboe * @i: &struct iov_iter to restore 18968fb0f47aSJens Axboe * @state: state to restore from 18978fb0f47aSJens Axboe * 18988fb0f47aSJens Axboe * Used after iov_iter_save_state() to bring restore @i, if operations may 18998fb0f47aSJens Axboe * have advanced it. 19008fb0f47aSJens Axboe * 19018fb0f47aSJens Axboe * Note: only works on ITER_IOVEC, ITER_BVEC, and ITER_KVEC 19028fb0f47aSJens Axboe */ 19038fb0f47aSJens Axboe void iov_iter_restore(struct iov_iter *i, struct iov_iter_state *state) 19048fb0f47aSJens Axboe { 19058fb0f47aSJens Axboe if (WARN_ON_ONCE(!iov_iter_is_bvec(i) && !iter_is_iovec(i)) && 1906fcb14cb1SAl Viro !iov_iter_is_kvec(i) && !iter_is_ubuf(i)) 19078fb0f47aSJens Axboe return; 19088fb0f47aSJens Axboe i->iov_offset = state->iov_offset; 19098fb0f47aSJens Axboe i->count = state->count; 1910fcb14cb1SAl Viro if (iter_is_ubuf(i)) 1911fcb14cb1SAl Viro return; 19128fb0f47aSJens Axboe /* 19138fb0f47aSJens Axboe * For the *vec iters, nr_segs + iov is constant - if we increment 19148fb0f47aSJens Axboe * the vec, then we also decrement the nr_segs count. Hence we don't 19158fb0f47aSJens Axboe * need to track both of these, just one is enough and we can deduct 19168fb0f47aSJens Axboe * the other from that. ITER_KVEC and ITER_IOVEC are the same struct 19178fb0f47aSJens Axboe * size, so we can just increment the iov pointer as they are unionzed. 19188fb0f47aSJens Axboe * ITER_BVEC _may_ be the same size on some archs, but on others it is 19198fb0f47aSJens Axboe * not. Be safe and handle it separately. 19208fb0f47aSJens Axboe */ 19218fb0f47aSJens Axboe BUILD_BUG_ON(sizeof(struct iovec) != sizeof(struct kvec)); 19228fb0f47aSJens Axboe if (iov_iter_is_bvec(i)) 19238fb0f47aSJens Axboe i->bvec -= state->nr_segs - i->nr_segs; 19248fb0f47aSJens Axboe else 19258fb0f47aSJens Axboe i->iov -= state->nr_segs - i->nr_segs; 19268fb0f47aSJens Axboe i->nr_segs = state->nr_segs; 19278fb0f47aSJens Axboe } 1928