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 17fcb14cb1SAl Viro /* covers ubuf and kbuf alike */ 18fcb14cb1SAl Viro #define iterate_buf(i, n, base, len, off, __p, STEP) { \ 19fcb14cb1SAl Viro size_t __maybe_unused off = 0; \ 20fcb14cb1SAl Viro len = n; \ 21fcb14cb1SAl Viro base = __p + i->iov_offset; \ 22fcb14cb1SAl Viro len -= (STEP); \ 23fcb14cb1SAl Viro i->iov_offset += len; \ 24fcb14cb1SAl Viro n = len; \ 25fcb14cb1SAl Viro } 26fcb14cb1SAl Viro 275c67aa90SAl Viro /* covers iovec and kvec alike */ 28a6e4ec7bSAl Viro #define iterate_iovec(i, n, base, len, off, __p, STEP) { \ 297baa5099SAl Viro size_t off = 0; \ 30a6e4ec7bSAl Viro size_t skip = i->iov_offset; \ 317a1bcb5dSAl Viro do { \ 327baa5099SAl Viro len = min(n, __p->iov_len - skip); \ 337baa5099SAl Viro if (likely(len)) { \ 347baa5099SAl Viro base = __p->iov_base + skip; \ 357baa5099SAl Viro len -= (STEP); \ 367baa5099SAl Viro off += len; \ 377baa5099SAl Viro skip += len; \ 387baa5099SAl Viro n -= len; \ 397a1bcb5dSAl Viro if (skip < __p->iov_len) \ 407a1bcb5dSAl Viro break; \ 41d879cb83SAl Viro } \ 42d879cb83SAl Viro __p++; \ 437a1bcb5dSAl Viro skip = 0; \ 447a1bcb5dSAl Viro } while (n); \ 45a6e4ec7bSAl Viro i->iov_offset = skip; \ 467baa5099SAl Viro n = off; \ 47d879cb83SAl Viro } 48d879cb83SAl Viro 49a6e4ec7bSAl Viro #define iterate_bvec(i, n, base, len, off, p, STEP) { \ 507baa5099SAl Viro size_t off = 0; \ 51a6e4ec7bSAl Viro unsigned skip = i->iov_offset; \ 527491a2bfSAl Viro while (n) { \ 537491a2bfSAl Viro unsigned offset = p->bv_offset + skip; \ 541b4fb5ffSAl Viro unsigned left; \ 5521b56c84SAl Viro void *kaddr = kmap_local_page(p->bv_page + \ 5621b56c84SAl Viro offset / PAGE_SIZE); \ 577baa5099SAl Viro base = kaddr + offset % PAGE_SIZE; \ 58a6e4ec7bSAl Viro len = min(min(n, (size_t)(p->bv_len - skip)), \ 597491a2bfSAl Viro (size_t)(PAGE_SIZE - offset % PAGE_SIZE)); \ 601b4fb5ffSAl Viro left = (STEP); \ 6121b56c84SAl Viro kunmap_local(kaddr); \ 627baa5099SAl Viro len -= left; \ 637baa5099SAl Viro off += len; \ 647baa5099SAl Viro skip += len; \ 657491a2bfSAl Viro if (skip == p->bv_len) { \ 667491a2bfSAl Viro skip = 0; \ 677491a2bfSAl Viro p++; \ 68d879cb83SAl Viro } \ 697baa5099SAl Viro n -= len; \ 701b4fb5ffSAl Viro if (left) \ 711b4fb5ffSAl Viro break; \ 727491a2bfSAl Viro } \ 73a6e4ec7bSAl Viro i->iov_offset = skip; \ 747baa5099SAl Viro n = off; \ 75d879cb83SAl Viro } 76d879cb83SAl Viro 77a6e4ec7bSAl Viro #define iterate_xarray(i, n, base, len, __off, STEP) { \ 781b4fb5ffSAl Viro __label__ __out; \ 79622838f3SAl Viro size_t __off = 0; \ 80821979f5SMatthew Wilcox (Oracle) struct folio *folio; \ 81a6e4ec7bSAl Viro loff_t start = i->xarray_start + i->iov_offset; \ 824b179e9aSAl Viro pgoff_t index = start / PAGE_SIZE; \ 837ff50620SDavid Howells XA_STATE(xas, i->xarray, index); \ 847ff50620SDavid Howells \ 85821979f5SMatthew Wilcox (Oracle) len = PAGE_SIZE - offset_in_page(start); \ 867ff50620SDavid Howells rcu_read_lock(); \ 87821979f5SMatthew Wilcox (Oracle) xas_for_each(&xas, folio, ULONG_MAX) { \ 881b4fb5ffSAl Viro unsigned left; \ 89821979f5SMatthew Wilcox (Oracle) size_t offset; \ 90821979f5SMatthew Wilcox (Oracle) if (xas_retry(&xas, folio)) \ 917ff50620SDavid Howells continue; \ 92821979f5SMatthew Wilcox (Oracle) if (WARN_ON(xa_is_value(folio))) \ 937ff50620SDavid Howells break; \ 94821979f5SMatthew Wilcox (Oracle) if (WARN_ON(folio_test_hugetlb(folio))) \ 957ff50620SDavid Howells break; \ 96821979f5SMatthew Wilcox (Oracle) offset = offset_in_folio(folio, start + __off); \ 97821979f5SMatthew Wilcox (Oracle) while (offset < folio_size(folio)) { \ 98821979f5SMatthew Wilcox (Oracle) base = kmap_local_folio(folio, offset); \ 997baa5099SAl Viro len = min(n, len); \ 1001b4fb5ffSAl Viro left = (STEP); \ 101821979f5SMatthew Wilcox (Oracle) kunmap_local(base); \ 1027baa5099SAl Viro len -= left; \ 1037baa5099SAl Viro __off += len; \ 1047baa5099SAl Viro n -= len; \ 1051b4fb5ffSAl Viro if (left || n == 0) \ 1061b4fb5ffSAl Viro goto __out; \ 107821979f5SMatthew Wilcox (Oracle) offset += len; \ 108821979f5SMatthew Wilcox (Oracle) len = PAGE_SIZE; \ 1097ff50620SDavid Howells } \ 1107ff50620SDavid Howells } \ 1111b4fb5ffSAl Viro __out: \ 1127ff50620SDavid Howells rcu_read_unlock(); \ 113a6e4ec7bSAl Viro i->iov_offset += __off; \ 114622838f3SAl Viro n = __off; \ 1157ff50620SDavid Howells } 1167ff50620SDavid Howells 1177baa5099SAl Viro #define __iterate_and_advance(i, n, base, len, off, I, K) { \ 118dd254f5aSAl Viro if (unlikely(i->count < n)) \ 119dd254f5aSAl Viro n = i->count; \ 120f5da8354SAl Viro if (likely(n)) { \ 121fcb14cb1SAl Viro if (likely(iter_is_ubuf(i))) { \ 122fcb14cb1SAl Viro void __user *base; \ 123fcb14cb1SAl Viro size_t len; \ 124fcb14cb1SAl Viro iterate_buf(i, n, base, len, off, \ 125fcb14cb1SAl Viro i->ubuf, (I)) \ 126fcb14cb1SAl Viro } else if (likely(iter_is_iovec(i))) { \ 127de4f5fedSJens Axboe const struct iovec *iov = iter_iov(i); \ 1287baa5099SAl Viro void __user *base; \ 1297baa5099SAl Viro size_t len; \ 1307baa5099SAl Viro iterate_iovec(i, n, base, len, off, \ 131a6e4ec7bSAl Viro iov, (I)) \ 132de4f5fedSJens Axboe i->nr_segs -= iov - iter_iov(i); \ 133de4f5fedSJens Axboe i->__iov = iov; \ 13428f38db7SAl Viro } else if (iov_iter_is_bvec(i)) { \ 13528f38db7SAl Viro const struct bio_vec *bvec = i->bvec; \ 1367baa5099SAl Viro void *base; \ 1377baa5099SAl Viro size_t len; \ 1387baa5099SAl Viro iterate_bvec(i, n, base, len, off, \ 139a6e4ec7bSAl Viro bvec, (K)) \ 1407491a2bfSAl Viro i->nr_segs -= bvec - i->bvec; \ 1417491a2bfSAl Viro i->bvec = bvec; \ 14228f38db7SAl Viro } else if (iov_iter_is_kvec(i)) { \ 1435c67aa90SAl Viro const struct kvec *kvec = i->kvec; \ 1447baa5099SAl Viro void *base; \ 1457baa5099SAl Viro size_t len; \ 1467baa5099SAl Viro iterate_iovec(i, n, base, len, off, \ 147a6e4ec7bSAl Viro kvec, (K)) \ 14828f38db7SAl Viro i->nr_segs -= kvec - i->kvec; \ 14928f38db7SAl Viro i->kvec = kvec; \ 15028f38db7SAl Viro } else if (iov_iter_is_xarray(i)) { \ 1517baa5099SAl Viro void *base; \ 1527baa5099SAl Viro size_t len; \ 1537baa5099SAl Viro iterate_xarray(i, n, base, len, off, \ 154a6e4ec7bSAl Viro (K)) \ 155d879cb83SAl Viro } \ 156d879cb83SAl Viro i->count -= n; \ 157dd254f5aSAl Viro } \ 158d879cb83SAl Viro } 1597baa5099SAl Viro #define iterate_and_advance(i, n, base, len, off, I, K) \ 1607baa5099SAl Viro __iterate_and_advance(i, n, base, len, off, I, ((void)(K),0)) 161d879cb83SAl Viro 16209fc68dcSAl Viro static int copyout(void __user *to, const void *from, size_t n) 16309fc68dcSAl Viro { 1644d0e9df5SAlbert van der Linde if (should_fail_usercopy()) 1654d0e9df5SAlbert van der Linde return n; 16696d4f267SLinus Torvalds if (access_ok(to, n)) { 167d0ef4c36SMarco Elver instrument_copy_to_user(to, from, n); 16809fc68dcSAl Viro n = raw_copy_to_user(to, from, n); 16909fc68dcSAl Viro } 17009fc68dcSAl Viro return n; 17109fc68dcSAl Viro } 17209fc68dcSAl Viro 1734f80818bSLorenzo Stoakes static int copyout_nofault(void __user *to, const void *from, size_t n) 1744f80818bSLorenzo Stoakes { 1754f80818bSLorenzo Stoakes long res; 1764f80818bSLorenzo Stoakes 1774f80818bSLorenzo Stoakes if (should_fail_usercopy()) 1784f80818bSLorenzo Stoakes return n; 1794f80818bSLorenzo Stoakes 1804f80818bSLorenzo Stoakes res = copy_to_user_nofault(to, from, n); 1814f80818bSLorenzo Stoakes 1824f80818bSLorenzo Stoakes return res < 0 ? n : res; 1834f80818bSLorenzo Stoakes } 1844f80818bSLorenzo Stoakes 18509fc68dcSAl Viro static int copyin(void *to, const void __user *from, size_t n) 18609fc68dcSAl Viro { 18733b75c1dSAlexander Potapenko size_t res = n; 18833b75c1dSAlexander Potapenko 1894d0e9df5SAlbert van der Linde if (should_fail_usercopy()) 1904d0e9df5SAlbert van der Linde return n; 19196d4f267SLinus Torvalds if (access_ok(from, n)) { 19233b75c1dSAlexander Potapenko instrument_copy_from_user_before(to, from, n); 19333b75c1dSAlexander Potapenko res = raw_copy_from_user(to, from, n); 19433b75c1dSAlexander Potapenko instrument_copy_from_user_after(to, from, n, res); 19509fc68dcSAl Viro } 19633b75c1dSAlexander Potapenko return res; 19709fc68dcSAl Viro } 19809fc68dcSAl Viro 199d879cb83SAl Viro /* 200a6294593SAndreas Gruenbacher * fault_in_iov_iter_readable - fault in iov iterator for reading 201a6294593SAndreas Gruenbacher * @i: iterator 202a6294593SAndreas Gruenbacher * @size: maximum length 203171a0203SAnton Altaparmakov * 204a6294593SAndreas Gruenbacher * Fault in one or more iovecs of the given iov_iter, to a maximum length of 205a6294593SAndreas Gruenbacher * @size. For each iovec, fault in each page that constitutes the iovec. 206a6294593SAndreas Gruenbacher * 207a6294593SAndreas Gruenbacher * Returns the number of bytes not faulted in (like copy_to_user() and 208a6294593SAndreas Gruenbacher * copy_from_user()). 209a6294593SAndreas Gruenbacher * 210a6294593SAndreas Gruenbacher * Always returns 0 for non-userspace iterators. 211171a0203SAnton Altaparmakov */ 212a6294593SAndreas Gruenbacher size_t fault_in_iov_iter_readable(const struct iov_iter *i, size_t size) 213171a0203SAnton Altaparmakov { 214fcb14cb1SAl Viro if (iter_is_ubuf(i)) { 215fcb14cb1SAl Viro size_t n = min(size, iov_iter_count(i)); 216fcb14cb1SAl Viro n -= fault_in_readable(i->ubuf + i->iov_offset, n); 217fcb14cb1SAl Viro return size - n; 218fcb14cb1SAl Viro } else if (iter_is_iovec(i)) { 219a6294593SAndreas Gruenbacher size_t count = min(size, iov_iter_count(i)); 2208409a0d2SAl Viro const struct iovec *p; 2218409a0d2SAl Viro size_t skip; 2228409a0d2SAl Viro 223a6294593SAndreas Gruenbacher size -= count; 224de4f5fedSJens Axboe for (p = iter_iov(i), skip = i->iov_offset; count; p++, skip = 0) { 225a6294593SAndreas Gruenbacher size_t len = min(count, p->iov_len - skip); 226a6294593SAndreas Gruenbacher size_t ret; 2278409a0d2SAl Viro 2288409a0d2SAl Viro if (unlikely(!len)) 2298409a0d2SAl Viro continue; 230a6294593SAndreas Gruenbacher ret = fault_in_readable(p->iov_base + skip, len); 231a6294593SAndreas Gruenbacher count -= len - ret; 232a6294593SAndreas Gruenbacher if (ret) 233a6294593SAndreas Gruenbacher break; 2348409a0d2SAl Viro } 235a6294593SAndreas Gruenbacher return count + size; 236171a0203SAnton Altaparmakov } 237171a0203SAnton Altaparmakov return 0; 238171a0203SAnton Altaparmakov } 239a6294593SAndreas Gruenbacher EXPORT_SYMBOL(fault_in_iov_iter_readable); 240171a0203SAnton Altaparmakov 241cdd591fcSAndreas Gruenbacher /* 242cdd591fcSAndreas Gruenbacher * fault_in_iov_iter_writeable - fault in iov iterator for writing 243cdd591fcSAndreas Gruenbacher * @i: iterator 244cdd591fcSAndreas Gruenbacher * @size: maximum length 245cdd591fcSAndreas Gruenbacher * 246cdd591fcSAndreas Gruenbacher * Faults in the iterator using get_user_pages(), i.e., without triggering 247cdd591fcSAndreas Gruenbacher * hardware page faults. This is primarily useful when we already know that 248cdd591fcSAndreas Gruenbacher * some or all of the pages in @i aren't in memory. 249cdd591fcSAndreas Gruenbacher * 250cdd591fcSAndreas Gruenbacher * Returns the number of bytes not faulted in, like copy_to_user() and 251cdd591fcSAndreas Gruenbacher * copy_from_user(). 252cdd591fcSAndreas Gruenbacher * 253cdd591fcSAndreas Gruenbacher * Always returns 0 for non-user-space iterators. 254cdd591fcSAndreas Gruenbacher */ 255cdd591fcSAndreas Gruenbacher size_t fault_in_iov_iter_writeable(const struct iov_iter *i, size_t size) 256cdd591fcSAndreas Gruenbacher { 257fcb14cb1SAl Viro if (iter_is_ubuf(i)) { 258fcb14cb1SAl Viro size_t n = min(size, iov_iter_count(i)); 259fcb14cb1SAl Viro n -= fault_in_safe_writeable(i->ubuf + i->iov_offset, n); 260fcb14cb1SAl Viro return size - n; 261fcb14cb1SAl Viro } else if (iter_is_iovec(i)) { 262cdd591fcSAndreas Gruenbacher size_t count = min(size, iov_iter_count(i)); 263cdd591fcSAndreas Gruenbacher const struct iovec *p; 264cdd591fcSAndreas Gruenbacher size_t skip; 265cdd591fcSAndreas Gruenbacher 266cdd591fcSAndreas Gruenbacher size -= count; 267de4f5fedSJens Axboe for (p = iter_iov(i), skip = i->iov_offset; count; p++, skip = 0) { 268cdd591fcSAndreas Gruenbacher size_t len = min(count, p->iov_len - skip); 269cdd591fcSAndreas Gruenbacher size_t ret; 270cdd591fcSAndreas Gruenbacher 271cdd591fcSAndreas Gruenbacher if (unlikely(!len)) 272cdd591fcSAndreas Gruenbacher continue; 273cdd591fcSAndreas Gruenbacher ret = fault_in_safe_writeable(p->iov_base + skip, len); 274cdd591fcSAndreas Gruenbacher count -= len - ret; 275cdd591fcSAndreas Gruenbacher if (ret) 276cdd591fcSAndreas Gruenbacher break; 277cdd591fcSAndreas Gruenbacher } 278cdd591fcSAndreas Gruenbacher return count + size; 279cdd591fcSAndreas Gruenbacher } 280cdd591fcSAndreas Gruenbacher return 0; 281cdd591fcSAndreas Gruenbacher } 282cdd591fcSAndreas Gruenbacher EXPORT_SYMBOL(fault_in_iov_iter_writeable); 283cdd591fcSAndreas Gruenbacher 284aa563d7bSDavid Howells void iov_iter_init(struct iov_iter *i, unsigned int direction, 285d879cb83SAl Viro const struct iovec *iov, unsigned long nr_segs, 286d879cb83SAl Viro size_t count) 287d879cb83SAl Viro { 288aa563d7bSDavid Howells WARN_ON(direction & ~(READ | WRITE)); 2898cd54c1cSAl Viro *i = (struct iov_iter) { 2908cd54c1cSAl Viro .iter_type = ITER_IOVEC, 291245f0922SKefeng Wang .copy_mc = false, 2923337ab08SAndreas Gruenbacher .nofault = false, 293fcb14cb1SAl Viro .user_backed = true, 2948cd54c1cSAl Viro .data_source = direction, 295de4f5fedSJens Axboe .__iov = iov, 2968cd54c1cSAl Viro .nr_segs = nr_segs, 2978cd54c1cSAl Viro .iov_offset = 0, 2988cd54c1cSAl Viro .count = count 2998cd54c1cSAl Viro }; 300d879cb83SAl Viro } 301d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_init); 302d879cb83SAl Viro 303f9152895SAl Viro static __wsum csum_and_memcpy(void *to, const void *from, size_t len, 304f9152895SAl Viro __wsum sum, size_t off) 305f9152895SAl Viro { 306cc44c17bSAl Viro __wsum next = csum_partial_copy_nocheck(from, to, len); 307f9152895SAl Viro return csum_block_add(sum, next, off); 308f9152895SAl Viro } 309f9152895SAl Viro 310aa28de27SAl Viro size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i) 311d879cb83SAl Viro { 312a41dad90SAl Viro if (WARN_ON_ONCE(i->data_source)) 313a41dad90SAl Viro return 0; 314fcb14cb1SAl Viro if (user_backed_iter(i)) 31509fc68dcSAl Viro might_fault(); 3167baa5099SAl Viro iterate_and_advance(i, bytes, base, len, off, 3177baa5099SAl Viro copyout(base, addr + off, len), 3187baa5099SAl Viro memcpy(base, addr + off, len) 319d879cb83SAl Viro ) 320d879cb83SAl Viro 321d879cb83SAl Viro return bytes; 322d879cb83SAl Viro } 323aa28de27SAl Viro EXPORT_SYMBOL(_copy_to_iter); 324d879cb83SAl Viro 325ec6347bbSDan Williams #ifdef CONFIG_ARCH_HAS_COPY_MC 326ec6347bbSDan Williams static int copyout_mc(void __user *to, const void *from, size_t n) 3278780356eSDan Williams { 32896d4f267SLinus Torvalds if (access_ok(to, n)) { 329d0ef4c36SMarco Elver instrument_copy_to_user(to, from, n); 330ec6347bbSDan Williams n = copy_mc_to_user((__force void *) to, from, n); 3318780356eSDan Williams } 3328780356eSDan Williams return n; 3338780356eSDan Williams } 3348780356eSDan Williams 335bf3eeb9bSDan Williams /** 336ec6347bbSDan Williams * _copy_mc_to_iter - copy to iter with source memory error exception handling 337bf3eeb9bSDan Williams * @addr: source kernel address 338bf3eeb9bSDan Williams * @bytes: total transfer length 33944e55997SRandy Dunlap * @i: destination iterator 340bf3eeb9bSDan Williams * 341ec6347bbSDan Williams * The pmem driver deploys this for the dax operation 342ec6347bbSDan Williams * (dax_copy_to_iter()) for dax reads (bypass page-cache and the 343ec6347bbSDan Williams * block-layer). Upon #MC read(2) aborts and returns EIO or the bytes 344ec6347bbSDan Williams * successfully copied. 345bf3eeb9bSDan Williams * 346ec6347bbSDan Williams * The main differences between this and typical _copy_to_iter(). 347bf3eeb9bSDan Williams * 348bf3eeb9bSDan Williams * * Typical tail/residue handling after a fault retries the copy 349bf3eeb9bSDan Williams * byte-by-byte until the fault happens again. Re-triggering machine 350bf3eeb9bSDan Williams * checks is potentially fatal so the implementation uses source 351bf3eeb9bSDan Williams * alignment and poison alignment assumptions to avoid re-triggering 352bf3eeb9bSDan Williams * hardware exceptions. 353bf3eeb9bSDan Williams * 354*3fc40265SDavid Howells * * ITER_KVEC and ITER_BVEC can return short copies. Compare to 355*3fc40265SDavid Howells * copy_to_iter() where only ITER_IOVEC attempts might return a short copy. 35644e55997SRandy Dunlap * 35744e55997SRandy Dunlap * Return: number of bytes copied (may be %0) 358bf3eeb9bSDan Williams */ 359ec6347bbSDan Williams size_t _copy_mc_to_iter(const void *addr, size_t bytes, struct iov_iter *i) 3608780356eSDan Williams { 361a41dad90SAl Viro if (WARN_ON_ONCE(i->data_source)) 362a41dad90SAl Viro return 0; 363fcb14cb1SAl Viro if (user_backed_iter(i)) 3648780356eSDan Williams might_fault(); 3657baa5099SAl Viro __iterate_and_advance(i, bytes, base, len, off, 3667baa5099SAl Viro copyout_mc(base, addr + off, len), 3677baa5099SAl Viro copy_mc_to_kernel(base, addr + off, len) 3688780356eSDan Williams ) 3698780356eSDan Williams 3708780356eSDan Williams return bytes; 3718780356eSDan Williams } 372ec6347bbSDan Williams EXPORT_SYMBOL_GPL(_copy_mc_to_iter); 373ec6347bbSDan Williams #endif /* CONFIG_ARCH_HAS_COPY_MC */ 3748780356eSDan Williams 375245f0922SKefeng Wang static void *memcpy_from_iter(struct iov_iter *i, void *to, const void *from, 376245f0922SKefeng Wang size_t size) 377245f0922SKefeng Wang { 378245f0922SKefeng Wang if (iov_iter_is_copy_mc(i)) 379245f0922SKefeng Wang return (void *)copy_mc_to_kernel(to, from, size); 380245f0922SKefeng Wang return memcpy(to, from, size); 381245f0922SKefeng Wang } 382245f0922SKefeng Wang 383aa28de27SAl Viro size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i) 384d879cb83SAl Viro { 385a41dad90SAl Viro if (WARN_ON_ONCE(!i->data_source)) 386241699cdSAl Viro return 0; 387a41dad90SAl Viro 388fcb14cb1SAl Viro if (user_backed_iter(i)) 38909fc68dcSAl Viro might_fault(); 3907baa5099SAl Viro iterate_and_advance(i, bytes, base, len, off, 3917baa5099SAl Viro copyin(addr + off, base, len), 392245f0922SKefeng Wang memcpy_from_iter(i, addr + off, base, len) 393d879cb83SAl Viro ) 394d879cb83SAl Viro 395d879cb83SAl Viro return bytes; 396d879cb83SAl Viro } 397aa28de27SAl Viro EXPORT_SYMBOL(_copy_from_iter); 398d879cb83SAl Viro 399aa28de27SAl Viro size_t _copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i) 400d879cb83SAl Viro { 401a41dad90SAl Viro if (WARN_ON_ONCE(!i->data_source)) 402241699cdSAl Viro return 0; 403a41dad90SAl Viro 4047baa5099SAl Viro iterate_and_advance(i, bytes, base, len, off, 4057baa5099SAl Viro __copy_from_user_inatomic_nocache(addr + off, base, len), 4067baa5099SAl Viro memcpy(addr + off, base, len) 407d879cb83SAl Viro ) 408d879cb83SAl Viro 409d879cb83SAl Viro return bytes; 410d879cb83SAl Viro } 411aa28de27SAl Viro EXPORT_SYMBOL(_copy_from_iter_nocache); 412d879cb83SAl Viro 4130aed55afSDan Williams #ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE 414abd08d7dSDan Williams /** 415abd08d7dSDan Williams * _copy_from_iter_flushcache - write destination through cpu cache 416abd08d7dSDan Williams * @addr: destination kernel address 417abd08d7dSDan Williams * @bytes: total transfer length 41844e55997SRandy Dunlap * @i: source iterator 419abd08d7dSDan Williams * 420abd08d7dSDan Williams * The pmem driver arranges for filesystem-dax to use this facility via 421abd08d7dSDan Williams * dax_copy_from_iter() for ensuring that writes to persistent memory 422abd08d7dSDan Williams * are flushed through the CPU cache. It is differentiated from 423abd08d7dSDan Williams * _copy_from_iter_nocache() in that guarantees all data is flushed for 424abd08d7dSDan Williams * all iterator types. The _copy_from_iter_nocache() only attempts to 425abd08d7dSDan Williams * bypass the cache for the ITER_IOVEC case, and on some archs may use 426abd08d7dSDan Williams * instructions that strand dirty-data in the cache. 42744e55997SRandy Dunlap * 42844e55997SRandy Dunlap * Return: number of bytes copied (may be %0) 429abd08d7dSDan Williams */ 4306a37e940SLinus Torvalds size_t _copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i) 4310aed55afSDan Williams { 432a41dad90SAl Viro if (WARN_ON_ONCE(!i->data_source)) 4330aed55afSDan Williams return 0; 434a41dad90SAl Viro 4357baa5099SAl Viro iterate_and_advance(i, bytes, base, len, off, 4367baa5099SAl Viro __copy_from_user_flushcache(addr + off, base, len), 4377baa5099SAl Viro memcpy_flushcache(addr + off, base, len) 4380aed55afSDan Williams ) 4390aed55afSDan Williams 4400aed55afSDan Williams return bytes; 4410aed55afSDan Williams } 4426a37e940SLinus Torvalds EXPORT_SYMBOL_GPL(_copy_from_iter_flushcache); 4430aed55afSDan Williams #endif 4440aed55afSDan Williams 44572e809edSAl Viro static inline bool page_copy_sane(struct page *page, size_t offset, size_t n) 44672e809edSAl Viro { 4476daef95bSEric Dumazet struct page *head; 4486daef95bSEric Dumazet size_t v = n + offset; 4496daef95bSEric Dumazet 4506daef95bSEric Dumazet /* 4516daef95bSEric Dumazet * The general case needs to access the page order in order 4526daef95bSEric Dumazet * to compute the page size. 4536daef95bSEric Dumazet * However, we mostly deal with order-0 pages and thus can 4546daef95bSEric Dumazet * avoid a possible cache line miss for requests that fit all 4556daef95bSEric Dumazet * page orders. 4566daef95bSEric Dumazet */ 4576daef95bSEric Dumazet if (n <= v && v <= PAGE_SIZE) 4586daef95bSEric Dumazet return true; 4596daef95bSEric Dumazet 4606daef95bSEric Dumazet head = compound_head(page); 4616daef95bSEric Dumazet v += (page - head) << PAGE_SHIFT; 462a90bcb86SPetar Penkov 46340a86061SAl Viro if (WARN_ON(n > v || v > page_size(head))) 46472e809edSAl Viro return false; 46540a86061SAl Viro return true; 46672e809edSAl Viro } 467cbbd26b8SAl Viro 46808aa6479SAl Viro size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes, 46908aa6479SAl Viro struct iov_iter *i) 47008aa6479SAl Viro { 47108aa6479SAl Viro size_t res = 0; 47240a86061SAl Viro if (!page_copy_sane(page, offset, bytes)) 47308aa6479SAl Viro return 0; 474a41dad90SAl Viro if (WARN_ON_ONCE(i->data_source)) 475a41dad90SAl Viro return 0; 47608aa6479SAl Viro page += offset / PAGE_SIZE; // first subpage 47708aa6479SAl Viro offset %= PAGE_SIZE; 47808aa6479SAl Viro while (1) { 479f0f6b614SAl Viro void *kaddr = kmap_local_page(page); 480f0f6b614SAl Viro size_t n = min(bytes, (size_t)PAGE_SIZE - offset); 481f0f6b614SAl Viro n = _copy_to_iter(kaddr + offset, n, i); 482f0f6b614SAl Viro kunmap_local(kaddr); 48308aa6479SAl Viro res += n; 48408aa6479SAl Viro bytes -= n; 48508aa6479SAl Viro if (!bytes || !n) 48608aa6479SAl Viro break; 48708aa6479SAl Viro offset += n; 48808aa6479SAl Viro if (offset == PAGE_SIZE) { 48908aa6479SAl Viro page++; 49008aa6479SAl Viro offset = 0; 49108aa6479SAl Viro } 49208aa6479SAl Viro } 49308aa6479SAl Viro return res; 49408aa6479SAl Viro } 495d879cb83SAl Viro EXPORT_SYMBOL(copy_page_to_iter); 496d879cb83SAl Viro 4974f80818bSLorenzo Stoakes size_t copy_page_to_iter_nofault(struct page *page, unsigned offset, size_t bytes, 4984f80818bSLorenzo Stoakes struct iov_iter *i) 4994f80818bSLorenzo Stoakes { 5004f80818bSLorenzo Stoakes size_t res = 0; 5014f80818bSLorenzo Stoakes 5024f80818bSLorenzo Stoakes if (!page_copy_sane(page, offset, bytes)) 5034f80818bSLorenzo Stoakes return 0; 5044f80818bSLorenzo Stoakes if (WARN_ON_ONCE(i->data_source)) 5054f80818bSLorenzo Stoakes return 0; 5064f80818bSLorenzo Stoakes page += offset / PAGE_SIZE; // first subpage 5074f80818bSLorenzo Stoakes offset %= PAGE_SIZE; 5084f80818bSLorenzo Stoakes while (1) { 5094f80818bSLorenzo Stoakes void *kaddr = kmap_local_page(page); 5104f80818bSLorenzo Stoakes size_t n = min(bytes, (size_t)PAGE_SIZE - offset); 5114f80818bSLorenzo Stoakes 5124f80818bSLorenzo Stoakes iterate_and_advance(i, n, base, len, off, 5134f80818bSLorenzo Stoakes copyout_nofault(base, kaddr + offset + off, len), 5144f80818bSLorenzo Stoakes memcpy(base, kaddr + offset + off, len) 5154f80818bSLorenzo Stoakes ) 5164f80818bSLorenzo Stoakes kunmap_local(kaddr); 5174f80818bSLorenzo Stoakes res += n; 5184f80818bSLorenzo Stoakes bytes -= n; 5194f80818bSLorenzo Stoakes if (!bytes || !n) 5204f80818bSLorenzo Stoakes break; 5214f80818bSLorenzo Stoakes offset += n; 5224f80818bSLorenzo Stoakes if (offset == PAGE_SIZE) { 5234f80818bSLorenzo Stoakes page++; 5244f80818bSLorenzo Stoakes offset = 0; 5254f80818bSLorenzo Stoakes } 5264f80818bSLorenzo Stoakes } 5274f80818bSLorenzo Stoakes return res; 5284f80818bSLorenzo Stoakes } 5294f80818bSLorenzo Stoakes EXPORT_SYMBOL(copy_page_to_iter_nofault); 5304f80818bSLorenzo Stoakes 531d879cb83SAl Viro size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes, 532d879cb83SAl Viro struct iov_iter *i) 533d879cb83SAl Viro { 534c03f05f1SAl Viro size_t res = 0; 535c03f05f1SAl Viro if (!page_copy_sane(page, offset, bytes)) 53628f38db7SAl Viro return 0; 537c03f05f1SAl Viro page += offset / PAGE_SIZE; // first subpage 538c03f05f1SAl Viro offset %= PAGE_SIZE; 539c03f05f1SAl Viro while (1) { 540c03f05f1SAl Viro void *kaddr = kmap_local_page(page); 541c03f05f1SAl Viro size_t n = min(bytes, (size_t)PAGE_SIZE - offset); 542c03f05f1SAl Viro n = _copy_from_iter(kaddr + offset, n, i); 543c03f05f1SAl Viro kunmap_local(kaddr); 544c03f05f1SAl Viro res += n; 545c03f05f1SAl Viro bytes -= n; 546c03f05f1SAl Viro if (!bytes || !n) 547c03f05f1SAl Viro break; 548c03f05f1SAl Viro offset += n; 549c03f05f1SAl Viro if (offset == PAGE_SIZE) { 550c03f05f1SAl Viro page++; 551c03f05f1SAl Viro offset = 0; 552c03f05f1SAl Viro } 553c03f05f1SAl Viro } 554c03f05f1SAl Viro return res; 555d879cb83SAl Viro } 556d879cb83SAl Viro EXPORT_SYMBOL(copy_page_from_iter); 557d879cb83SAl Viro 558d879cb83SAl Viro size_t iov_iter_zero(size_t bytes, struct iov_iter *i) 559d879cb83SAl Viro { 5607baa5099SAl Viro iterate_and_advance(i, bytes, base, len, count, 5617baa5099SAl Viro clear_user(base, len), 5627baa5099SAl Viro memset(base, 0, len) 563d879cb83SAl Viro ) 564d879cb83SAl Viro 565d879cb83SAl Viro return bytes; 566d879cb83SAl Viro } 567d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_zero); 568d879cb83SAl Viro 569f0b65f39SAl Viro size_t copy_page_from_iter_atomic(struct page *page, unsigned offset, size_t bytes, 570f0b65f39SAl Viro struct iov_iter *i) 571d879cb83SAl Viro { 572d879cb83SAl Viro char *kaddr = kmap_atomic(page), *p = kaddr + offset; 57340a86061SAl Viro if (!page_copy_sane(page, offset, bytes)) { 57472e809edSAl Viro kunmap_atomic(kaddr); 57572e809edSAl Viro return 0; 57672e809edSAl Viro } 577a41dad90SAl Viro if (WARN_ON_ONCE(!i->data_source)) { 578241699cdSAl Viro kunmap_atomic(kaddr); 579241699cdSAl Viro return 0; 580241699cdSAl Viro } 5817baa5099SAl Viro iterate_and_advance(i, bytes, base, len, off, 5827baa5099SAl Viro copyin(p + off, base, len), 583245f0922SKefeng Wang memcpy_from_iter(i, p + off, base, len) 584d879cb83SAl Viro ) 585d879cb83SAl Viro kunmap_atomic(kaddr); 586d879cb83SAl Viro return bytes; 587d879cb83SAl Viro } 588f0b65f39SAl Viro EXPORT_SYMBOL(copy_page_from_iter_atomic); 589d879cb83SAl Viro 59054c8195bSPavel Begunkov static void iov_iter_bvec_advance(struct iov_iter *i, size_t size) 59154c8195bSPavel Begunkov { 59218fa9af7SAl Viro const struct bio_vec *bvec, *end; 59354c8195bSPavel Begunkov 59418fa9af7SAl Viro if (!i->count) 59518fa9af7SAl Viro return; 59618fa9af7SAl Viro i->count -= size; 59754c8195bSPavel Begunkov 59818fa9af7SAl Viro size += i->iov_offset; 59918fa9af7SAl Viro 60018fa9af7SAl Viro for (bvec = i->bvec, end = bvec + i->nr_segs; bvec < end; bvec++) { 60118fa9af7SAl Viro if (likely(size < bvec->bv_len)) 60218fa9af7SAl Viro break; 60318fa9af7SAl Viro size -= bvec->bv_len; 60418fa9af7SAl Viro } 60518fa9af7SAl Viro i->iov_offset = size; 60618fa9af7SAl Viro i->nr_segs -= bvec - i->bvec; 60718fa9af7SAl Viro i->bvec = bvec; 60854c8195bSPavel Begunkov } 60954c8195bSPavel Begunkov 610185ac4d4SAl Viro static void iov_iter_iovec_advance(struct iov_iter *i, size_t size) 611185ac4d4SAl Viro { 612185ac4d4SAl Viro const struct iovec *iov, *end; 613185ac4d4SAl Viro 614185ac4d4SAl Viro if (!i->count) 615185ac4d4SAl Viro return; 616185ac4d4SAl Viro i->count -= size; 617185ac4d4SAl Viro 618185ac4d4SAl Viro size += i->iov_offset; // from beginning of current segment 619de4f5fedSJens Axboe for (iov = iter_iov(i), end = iov + i->nr_segs; iov < end; iov++) { 620185ac4d4SAl Viro if (likely(size < iov->iov_len)) 621185ac4d4SAl Viro break; 622185ac4d4SAl Viro size -= iov->iov_len; 623185ac4d4SAl Viro } 624185ac4d4SAl Viro i->iov_offset = size; 625de4f5fedSJens Axboe i->nr_segs -= iov - iter_iov(i); 626de4f5fedSJens Axboe i->__iov = iov; 627185ac4d4SAl Viro } 628185ac4d4SAl Viro 629d879cb83SAl Viro void iov_iter_advance(struct iov_iter *i, size_t size) 630d879cb83SAl Viro { 6313b3fc051SAl Viro if (unlikely(i->count < size)) 6323b3fc051SAl Viro size = i->count; 633fcb14cb1SAl Viro if (likely(iter_is_ubuf(i)) || unlikely(iov_iter_is_xarray(i))) { 634fcb14cb1SAl Viro i->iov_offset += size; 635fcb14cb1SAl Viro i->count -= size; 636fcb14cb1SAl Viro } else if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i))) { 637185ac4d4SAl Viro /* iovec and kvec have identical layouts */ 638185ac4d4SAl Viro iov_iter_iovec_advance(i, size); 639185ac4d4SAl Viro } else if (iov_iter_is_bvec(i)) { 640185ac4d4SAl Viro iov_iter_bvec_advance(i, size); 641185ac4d4SAl Viro } else if (iov_iter_is_discard(i)) { 642185ac4d4SAl Viro i->count -= size; 6437ff50620SDavid Howells } 644d879cb83SAl Viro } 645d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_advance); 646d879cb83SAl Viro 64727c0e374SAl Viro void iov_iter_revert(struct iov_iter *i, size_t unroll) 64827c0e374SAl Viro { 64927c0e374SAl Viro if (!unroll) 65027c0e374SAl Viro return; 6515b47d59aSAl Viro if (WARN_ON(unroll > MAX_RW_COUNT)) 6525b47d59aSAl Viro return; 65327c0e374SAl Viro i->count += unroll; 6549ea9ce04SDavid Howells if (unlikely(iov_iter_is_discard(i))) 6559ea9ce04SDavid Howells return; 65627c0e374SAl Viro if (unroll <= i->iov_offset) { 65727c0e374SAl Viro i->iov_offset -= unroll; 65827c0e374SAl Viro return; 65927c0e374SAl Viro } 66027c0e374SAl Viro unroll -= i->iov_offset; 661fcb14cb1SAl Viro if (iov_iter_is_xarray(i) || iter_is_ubuf(i)) { 6627ff50620SDavid Howells BUG(); /* We should never go beyond the start of the specified 6637ff50620SDavid Howells * range since we might then be straying into pages that 6647ff50620SDavid Howells * aren't pinned. 6657ff50620SDavid Howells */ 6667ff50620SDavid Howells } else if (iov_iter_is_bvec(i)) { 66727c0e374SAl Viro const struct bio_vec *bvec = i->bvec; 66827c0e374SAl Viro while (1) { 66927c0e374SAl Viro size_t n = (--bvec)->bv_len; 67027c0e374SAl Viro i->nr_segs++; 67127c0e374SAl Viro if (unroll <= n) { 67227c0e374SAl Viro i->bvec = bvec; 67327c0e374SAl Viro i->iov_offset = n - unroll; 67427c0e374SAl Viro return; 67527c0e374SAl Viro } 67627c0e374SAl Viro unroll -= n; 67727c0e374SAl Viro } 67827c0e374SAl Viro } else { /* same logics for iovec and kvec */ 679de4f5fedSJens Axboe const struct iovec *iov = iter_iov(i); 68027c0e374SAl Viro while (1) { 68127c0e374SAl Viro size_t n = (--iov)->iov_len; 68227c0e374SAl Viro i->nr_segs++; 68327c0e374SAl Viro if (unroll <= n) { 684de4f5fedSJens Axboe i->__iov = iov; 68527c0e374SAl Viro i->iov_offset = n - unroll; 68627c0e374SAl Viro return; 68727c0e374SAl Viro } 68827c0e374SAl Viro unroll -= n; 68927c0e374SAl Viro } 69027c0e374SAl Viro } 69127c0e374SAl Viro } 69227c0e374SAl Viro EXPORT_SYMBOL(iov_iter_revert); 69327c0e374SAl Viro 694d879cb83SAl Viro /* 695d879cb83SAl Viro * Return the count of just the current iov_iter segment. 696d879cb83SAl Viro */ 697d879cb83SAl Viro size_t iov_iter_single_seg_count(const struct iov_iter *i) 698d879cb83SAl Viro { 69928f38db7SAl Viro if (i->nr_segs > 1) { 70028f38db7SAl Viro if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i))) 701de4f5fedSJens Axboe return min(i->count, iter_iov(i)->iov_len - i->iov_offset); 7027ff50620SDavid Howells if (iov_iter_is_bvec(i)) 703d879cb83SAl Viro return min(i->count, i->bvec->bv_len - i->iov_offset); 70428f38db7SAl Viro } 70528f38db7SAl Viro return i->count; 706d879cb83SAl Viro } 707d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_single_seg_count); 708d879cb83SAl Viro 709aa563d7bSDavid Howells void iov_iter_kvec(struct iov_iter *i, unsigned int direction, 710d879cb83SAl Viro const struct kvec *kvec, unsigned long nr_segs, 711d879cb83SAl Viro size_t count) 712d879cb83SAl Viro { 713aa563d7bSDavid Howells WARN_ON(direction & ~(READ | WRITE)); 7148cd54c1cSAl Viro *i = (struct iov_iter){ 7158cd54c1cSAl Viro .iter_type = ITER_KVEC, 716245f0922SKefeng Wang .copy_mc = false, 7178cd54c1cSAl Viro .data_source = direction, 7188cd54c1cSAl Viro .kvec = kvec, 7198cd54c1cSAl Viro .nr_segs = nr_segs, 7208cd54c1cSAl Viro .iov_offset = 0, 7218cd54c1cSAl Viro .count = count 7228cd54c1cSAl Viro }; 723d879cb83SAl Viro } 724d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_kvec); 725d879cb83SAl Viro 726aa563d7bSDavid Howells void iov_iter_bvec(struct iov_iter *i, unsigned int direction, 727d879cb83SAl Viro const struct bio_vec *bvec, unsigned long nr_segs, 728d879cb83SAl Viro size_t count) 729d879cb83SAl Viro { 730aa563d7bSDavid Howells WARN_ON(direction & ~(READ | WRITE)); 7318cd54c1cSAl Viro *i = (struct iov_iter){ 7328cd54c1cSAl Viro .iter_type = ITER_BVEC, 733245f0922SKefeng Wang .copy_mc = false, 7348cd54c1cSAl Viro .data_source = direction, 7358cd54c1cSAl Viro .bvec = bvec, 7368cd54c1cSAl Viro .nr_segs = nr_segs, 7378cd54c1cSAl Viro .iov_offset = 0, 7388cd54c1cSAl Viro .count = count 7398cd54c1cSAl Viro }; 740d879cb83SAl Viro } 741d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_bvec); 742d879cb83SAl Viro 7439ea9ce04SDavid Howells /** 7447ff50620SDavid Howells * iov_iter_xarray - Initialise an I/O iterator to use the pages in an xarray 7457ff50620SDavid Howells * @i: The iterator to initialise. 7467ff50620SDavid Howells * @direction: The direction of the transfer. 7477ff50620SDavid Howells * @xarray: The xarray to access. 7487ff50620SDavid Howells * @start: The start file position. 7497ff50620SDavid Howells * @count: The size of the I/O buffer in bytes. 7507ff50620SDavid Howells * 7517ff50620SDavid Howells * Set up an I/O iterator to either draw data out of the pages attached to an 7527ff50620SDavid Howells * inode or to inject data into those pages. The pages *must* be prevented 7537ff50620SDavid Howells * from evaporation, either by taking a ref on them or locking them by the 7547ff50620SDavid Howells * caller. 7557ff50620SDavid Howells */ 7567ff50620SDavid Howells void iov_iter_xarray(struct iov_iter *i, unsigned int direction, 7577ff50620SDavid Howells struct xarray *xarray, loff_t start, size_t count) 7587ff50620SDavid Howells { 7597ff50620SDavid Howells BUG_ON(direction & ~1); 7608cd54c1cSAl Viro *i = (struct iov_iter) { 7618cd54c1cSAl Viro .iter_type = ITER_XARRAY, 762245f0922SKefeng Wang .copy_mc = false, 7638cd54c1cSAl Viro .data_source = direction, 7648cd54c1cSAl Viro .xarray = xarray, 7658cd54c1cSAl Viro .xarray_start = start, 7668cd54c1cSAl Viro .count = count, 7678cd54c1cSAl Viro .iov_offset = 0 7688cd54c1cSAl Viro }; 7697ff50620SDavid Howells } 7707ff50620SDavid Howells EXPORT_SYMBOL(iov_iter_xarray); 7717ff50620SDavid Howells 7727ff50620SDavid Howells /** 7739ea9ce04SDavid Howells * iov_iter_discard - Initialise an I/O iterator that discards data 7749ea9ce04SDavid Howells * @i: The iterator to initialise. 7759ea9ce04SDavid Howells * @direction: The direction of the transfer. 7769ea9ce04SDavid Howells * @count: The size of the I/O buffer in bytes. 7779ea9ce04SDavid Howells * 7789ea9ce04SDavid Howells * Set up an I/O iterator that just discards everything that's written to it. 7799ea9ce04SDavid Howells * It's only available as a READ iterator. 7809ea9ce04SDavid Howells */ 7819ea9ce04SDavid Howells void iov_iter_discard(struct iov_iter *i, unsigned int direction, size_t count) 7829ea9ce04SDavid Howells { 7839ea9ce04SDavid Howells BUG_ON(direction != READ); 7848cd54c1cSAl Viro *i = (struct iov_iter){ 7858cd54c1cSAl Viro .iter_type = ITER_DISCARD, 786245f0922SKefeng Wang .copy_mc = false, 7878cd54c1cSAl Viro .data_source = false, 7888cd54c1cSAl Viro .count = count, 7898cd54c1cSAl Viro .iov_offset = 0 7908cd54c1cSAl Viro }; 7919ea9ce04SDavid Howells } 7929ea9ce04SDavid Howells EXPORT_SYMBOL(iov_iter_discard); 7939ea9ce04SDavid Howells 794cfa320f7SKeith Busch static bool iov_iter_aligned_iovec(const struct iov_iter *i, unsigned addr_mask, 795cfa320f7SKeith Busch unsigned len_mask) 796cfa320f7SKeith Busch { 797cfa320f7SKeith Busch size_t size = i->count; 798cfa320f7SKeith Busch size_t skip = i->iov_offset; 799cfa320f7SKeith Busch unsigned k; 800cfa320f7SKeith Busch 801cfa320f7SKeith Busch for (k = 0; k < i->nr_segs; k++, skip = 0) { 802de4f5fedSJens Axboe const struct iovec *iov = iter_iov(i) + k; 803de4f5fedSJens Axboe size_t len = iov->iov_len - skip; 804cfa320f7SKeith Busch 805cfa320f7SKeith Busch if (len > size) 806cfa320f7SKeith Busch len = size; 807cfa320f7SKeith Busch if (len & len_mask) 808cfa320f7SKeith Busch return false; 809de4f5fedSJens Axboe if ((unsigned long)(iov->iov_base + skip) & addr_mask) 810cfa320f7SKeith Busch return false; 811cfa320f7SKeith Busch 812cfa320f7SKeith Busch size -= len; 813cfa320f7SKeith Busch if (!size) 814cfa320f7SKeith Busch break; 815cfa320f7SKeith Busch } 816cfa320f7SKeith Busch return true; 817cfa320f7SKeith Busch } 818cfa320f7SKeith Busch 819cfa320f7SKeith Busch static bool iov_iter_aligned_bvec(const struct iov_iter *i, unsigned addr_mask, 820cfa320f7SKeith Busch unsigned len_mask) 821cfa320f7SKeith Busch { 822cfa320f7SKeith Busch size_t size = i->count; 823cfa320f7SKeith Busch unsigned skip = i->iov_offset; 824cfa320f7SKeith Busch unsigned k; 825cfa320f7SKeith Busch 826cfa320f7SKeith Busch for (k = 0; k < i->nr_segs; k++, skip = 0) { 827cfa320f7SKeith Busch size_t len = i->bvec[k].bv_len - skip; 828cfa320f7SKeith Busch 829cfa320f7SKeith Busch if (len > size) 830cfa320f7SKeith Busch len = size; 831cfa320f7SKeith Busch if (len & len_mask) 832cfa320f7SKeith Busch return false; 833cfa320f7SKeith Busch if ((unsigned long)(i->bvec[k].bv_offset + skip) & addr_mask) 834cfa320f7SKeith Busch return false; 835cfa320f7SKeith Busch 836cfa320f7SKeith Busch size -= len; 837cfa320f7SKeith Busch if (!size) 838cfa320f7SKeith Busch break; 839cfa320f7SKeith Busch } 840cfa320f7SKeith Busch return true; 841cfa320f7SKeith Busch } 842cfa320f7SKeith Busch 843cfa320f7SKeith Busch /** 844cfa320f7SKeith Busch * iov_iter_is_aligned() - Check if the addresses and lengths of each segments 845cfa320f7SKeith Busch * are aligned to the parameters. 846cfa320f7SKeith Busch * 847cfa320f7SKeith Busch * @i: &struct iov_iter to restore 848cfa320f7SKeith Busch * @addr_mask: bit mask to check against the iov element's addresses 849cfa320f7SKeith Busch * @len_mask: bit mask to check against the iov element's lengths 850cfa320f7SKeith Busch * 851cfa320f7SKeith Busch * Return: false if any addresses or lengths intersect with the provided masks 852cfa320f7SKeith Busch */ 853cfa320f7SKeith Busch bool iov_iter_is_aligned(const struct iov_iter *i, unsigned addr_mask, 854cfa320f7SKeith Busch unsigned len_mask) 855cfa320f7SKeith Busch { 856fcb14cb1SAl Viro if (likely(iter_is_ubuf(i))) { 857fcb14cb1SAl Viro if (i->count & len_mask) 858fcb14cb1SAl Viro return false; 859fcb14cb1SAl Viro if ((unsigned long)(i->ubuf + i->iov_offset) & addr_mask) 860fcb14cb1SAl Viro return false; 861fcb14cb1SAl Viro return true; 862fcb14cb1SAl Viro } 863fcb14cb1SAl Viro 864cfa320f7SKeith Busch if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i))) 865cfa320f7SKeith Busch return iov_iter_aligned_iovec(i, addr_mask, len_mask); 866cfa320f7SKeith Busch 867cfa320f7SKeith Busch if (iov_iter_is_bvec(i)) 868cfa320f7SKeith Busch return iov_iter_aligned_bvec(i, addr_mask, len_mask); 869cfa320f7SKeith Busch 870cfa320f7SKeith Busch if (iov_iter_is_xarray(i)) { 871cfa320f7SKeith Busch if (i->count & len_mask) 872cfa320f7SKeith Busch return false; 873cfa320f7SKeith Busch if ((i->xarray_start + i->iov_offset) & addr_mask) 874cfa320f7SKeith Busch return false; 875cfa320f7SKeith Busch } 876cfa320f7SKeith Busch 877cfa320f7SKeith Busch return true; 878cfa320f7SKeith Busch } 879cfa320f7SKeith Busch EXPORT_SYMBOL_GPL(iov_iter_is_aligned); 880cfa320f7SKeith Busch 8819221d2e3SAl Viro static unsigned long iov_iter_alignment_iovec(const struct iov_iter *i) 882d879cb83SAl Viro { 883d879cb83SAl Viro unsigned long res = 0; 884d879cb83SAl Viro size_t size = i->count; 8859221d2e3SAl Viro size_t skip = i->iov_offset; 8869221d2e3SAl Viro unsigned k; 887d879cb83SAl Viro 8889221d2e3SAl Viro for (k = 0; k < i->nr_segs; k++, skip = 0) { 889de4f5fedSJens Axboe const struct iovec *iov = iter_iov(i) + k; 890de4f5fedSJens Axboe size_t len = iov->iov_len - skip; 8919221d2e3SAl Viro if (len) { 892de4f5fedSJens Axboe res |= (unsigned long)iov->iov_base + skip; 8939221d2e3SAl Viro if (len > size) 8949221d2e3SAl Viro len = size; 8959221d2e3SAl Viro res |= len; 8969221d2e3SAl Viro size -= len; 8979221d2e3SAl Viro if (!size) 8989221d2e3SAl Viro break; 8999221d2e3SAl Viro } 9009221d2e3SAl Viro } 9019221d2e3SAl Viro return res; 9029221d2e3SAl Viro } 9039221d2e3SAl Viro 9049221d2e3SAl Viro static unsigned long iov_iter_alignment_bvec(const struct iov_iter *i) 9059221d2e3SAl Viro { 9069221d2e3SAl Viro unsigned res = 0; 9079221d2e3SAl Viro size_t size = i->count; 9089221d2e3SAl Viro unsigned skip = i->iov_offset; 9099221d2e3SAl Viro unsigned k; 9109221d2e3SAl Viro 9119221d2e3SAl Viro for (k = 0; k < i->nr_segs; k++, skip = 0) { 9129221d2e3SAl Viro size_t len = i->bvec[k].bv_len - skip; 9139221d2e3SAl Viro res |= (unsigned long)i->bvec[k].bv_offset + skip; 9149221d2e3SAl Viro if (len > size) 9159221d2e3SAl Viro len = size; 9169221d2e3SAl Viro res |= len; 9179221d2e3SAl Viro size -= len; 9189221d2e3SAl Viro if (!size) 9199221d2e3SAl Viro break; 9209221d2e3SAl Viro } 9219221d2e3SAl Viro return res; 9229221d2e3SAl Viro } 9239221d2e3SAl Viro 9249221d2e3SAl Viro unsigned long iov_iter_alignment(const struct iov_iter *i) 9259221d2e3SAl Viro { 926fcb14cb1SAl Viro if (likely(iter_is_ubuf(i))) { 927fcb14cb1SAl Viro size_t size = i->count; 928fcb14cb1SAl Viro if (size) 929fcb14cb1SAl Viro return ((unsigned long)i->ubuf + i->iov_offset) | size; 930fcb14cb1SAl Viro return 0; 931fcb14cb1SAl Viro } 932fcb14cb1SAl Viro 9339221d2e3SAl Viro /* iovec and kvec have identical layouts */ 9349221d2e3SAl Viro if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i))) 9359221d2e3SAl Viro return iov_iter_alignment_iovec(i); 9369221d2e3SAl Viro 9379221d2e3SAl Viro if (iov_iter_is_bvec(i)) 9389221d2e3SAl Viro return iov_iter_alignment_bvec(i); 9399221d2e3SAl Viro 9409221d2e3SAl Viro if (iov_iter_is_xarray(i)) 9413d14ec1fSDavid Howells return (i->xarray_start + i->iov_offset) | i->count; 9429221d2e3SAl Viro 9439221d2e3SAl Viro return 0; 944d879cb83SAl Viro } 945d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_alignment); 946d879cb83SAl Viro 947357f435dSAl Viro unsigned long iov_iter_gap_alignment(const struct iov_iter *i) 948357f435dSAl Viro { 949357f435dSAl Viro unsigned long res = 0; 950610c7a71SAl Viro unsigned long v = 0; 951357f435dSAl Viro size_t size = i->count; 952610c7a71SAl Viro unsigned k; 953357f435dSAl Viro 954fcb14cb1SAl Viro if (iter_is_ubuf(i)) 955fcb14cb1SAl Viro return 0; 956fcb14cb1SAl Viro 957610c7a71SAl Viro if (WARN_ON(!iter_is_iovec(i))) 958241699cdSAl Viro return ~0U; 959241699cdSAl Viro 960610c7a71SAl Viro for (k = 0; k < i->nr_segs; k++) { 961de4f5fedSJens Axboe const struct iovec *iov = iter_iov(i) + k; 962de4f5fedSJens Axboe if (iov->iov_len) { 963de4f5fedSJens Axboe unsigned long base = (unsigned long)iov->iov_base; 964610c7a71SAl Viro if (v) // if not the first one 965610c7a71SAl Viro res |= base | v; // this start | previous end 966de4f5fedSJens Axboe v = base + iov->iov_len; 967de4f5fedSJens Axboe if (size <= iov->iov_len) 968610c7a71SAl Viro break; 969de4f5fedSJens Axboe size -= iov->iov_len; 970610c7a71SAl Viro } 971610c7a71SAl Viro } 972357f435dSAl Viro return res; 973357f435dSAl Viro } 974357f435dSAl Viro EXPORT_SYMBOL(iov_iter_gap_alignment); 975357f435dSAl Viro 9763cf42da3SAl Viro static int want_pages_array(struct page ***res, size_t size, 9773cf42da3SAl Viro size_t start, unsigned int maxpages) 978acbdeb83SAl Viro { 9793cf42da3SAl Viro unsigned int count = DIV_ROUND_UP(size + start, PAGE_SIZE); 9803cf42da3SAl Viro 9813cf42da3SAl Viro if (count > maxpages) 9823cf42da3SAl Viro count = maxpages; 9833cf42da3SAl Viro WARN_ON(!count); // caller should've prevented that 9843cf42da3SAl Viro if (!*res) { 9853cf42da3SAl Viro *res = kvmalloc_array(count, sizeof(struct page *), GFP_KERNEL); 9863cf42da3SAl Viro if (!*res) 9873cf42da3SAl Viro return 0; 9883cf42da3SAl Viro } 9893cf42da3SAl Viro return count; 990acbdeb83SAl Viro } 991acbdeb83SAl Viro 9927ff50620SDavid Howells static ssize_t iter_xarray_populate_pages(struct page **pages, struct xarray *xa, 9937ff50620SDavid Howells pgoff_t index, unsigned int nr_pages) 9947ff50620SDavid Howells { 9957ff50620SDavid Howells XA_STATE(xas, xa, index); 9967ff50620SDavid Howells struct page *page; 9977ff50620SDavid Howells unsigned int ret = 0; 9987ff50620SDavid Howells 9997ff50620SDavid Howells rcu_read_lock(); 10007ff50620SDavid Howells for (page = xas_load(&xas); page; page = xas_next(&xas)) { 10017ff50620SDavid Howells if (xas_retry(&xas, page)) 10027ff50620SDavid Howells continue; 10037ff50620SDavid Howells 10047ff50620SDavid Howells /* Has the page moved or been split? */ 10057ff50620SDavid Howells if (unlikely(page != xas_reload(&xas))) { 10067ff50620SDavid Howells xas_reset(&xas); 10077ff50620SDavid Howells continue; 10087ff50620SDavid Howells } 10097ff50620SDavid Howells 10107ff50620SDavid Howells pages[ret] = find_subpage(page, xas.xa_index); 10117ff50620SDavid Howells get_page(pages[ret]); 10127ff50620SDavid Howells if (++ret == nr_pages) 10137ff50620SDavid Howells break; 10147ff50620SDavid Howells } 10157ff50620SDavid Howells rcu_read_unlock(); 10167ff50620SDavid Howells return ret; 10177ff50620SDavid Howells } 10187ff50620SDavid Howells 10197ff50620SDavid Howells static ssize_t iter_xarray_get_pages(struct iov_iter *i, 102068fe506fSAl Viro struct page ***pages, size_t maxsize, 10217ff50620SDavid Howells unsigned maxpages, size_t *_start_offset) 10227ff50620SDavid Howells { 10233cf42da3SAl Viro unsigned nr, offset, count; 10243cf42da3SAl Viro pgoff_t index; 10257ff50620SDavid Howells loff_t pos; 10267ff50620SDavid Howells 10277ff50620SDavid Howells pos = i->xarray_start + i->iov_offset; 10287ff50620SDavid Howells index = pos >> PAGE_SHIFT; 10297ff50620SDavid Howells offset = pos & ~PAGE_MASK; 10307ff50620SDavid Howells *_start_offset = offset; 10317ff50620SDavid Howells 10323cf42da3SAl Viro count = want_pages_array(pages, maxsize, offset, maxpages); 10333cf42da3SAl Viro if (!count) 103468fe506fSAl Viro return -ENOMEM; 103568fe506fSAl Viro nr = iter_xarray_populate_pages(*pages, i->xarray, index, count); 10367ff50620SDavid Howells if (nr == 0) 10377ff50620SDavid Howells return 0; 10387ff50620SDavid Howells 1039eba2d3d7SAl Viro maxsize = min_t(size_t, nr * PAGE_SIZE - offset, maxsize); 1040310d9d5aSAl Viro i->iov_offset += maxsize; 1041310d9d5aSAl Viro i->count -= maxsize; 1042eba2d3d7SAl Viro return maxsize; 10437ff50620SDavid Howells } 10447ff50620SDavid Howells 1045fcb14cb1SAl Viro /* must be done on non-empty ITER_UBUF or ITER_IOVEC one */ 1046dd45ab9dSAl Viro static unsigned long first_iovec_segment(const struct iov_iter *i, size_t *size) 10473d671ca6SAl Viro { 10483d671ca6SAl Viro size_t skip; 10493d671ca6SAl Viro long k; 10503d671ca6SAl Viro 1051fcb14cb1SAl Viro if (iter_is_ubuf(i)) 1052fcb14cb1SAl Viro return (unsigned long)i->ubuf + i->iov_offset; 1053fcb14cb1SAl Viro 10543d671ca6SAl Viro for (k = 0, skip = i->iov_offset; k < i->nr_segs; k++, skip = 0) { 1055de4f5fedSJens Axboe const struct iovec *iov = iter_iov(i) + k; 1056de4f5fedSJens Axboe size_t len = iov->iov_len - skip; 10573d671ca6SAl Viro 10583d671ca6SAl Viro if (unlikely(!len)) 10593d671ca6SAl Viro continue; 106059dbd7d0SAl Viro if (*size > len) 10613d671ca6SAl Viro *size = len; 1062de4f5fedSJens Axboe return (unsigned long)iov->iov_base + skip; 10633d671ca6SAl Viro } 10643d671ca6SAl Viro BUG(); // if it had been empty, we wouldn't get called 10653d671ca6SAl Viro } 10663d671ca6SAl Viro 10673d671ca6SAl Viro /* must be done on non-empty ITER_BVEC one */ 10683d671ca6SAl Viro static struct page *first_bvec_segment(const struct iov_iter *i, 106959dbd7d0SAl Viro size_t *size, size_t *start) 10703d671ca6SAl Viro { 10713d671ca6SAl Viro struct page *page; 10723d671ca6SAl Viro size_t skip = i->iov_offset, len; 10733d671ca6SAl Viro 10743d671ca6SAl Viro len = i->bvec->bv_len - skip; 107559dbd7d0SAl Viro if (*size > len) 107659dbd7d0SAl Viro *size = len; 10773d671ca6SAl Viro skip += i->bvec->bv_offset; 10783d671ca6SAl Viro page = i->bvec->bv_page + skip / PAGE_SIZE; 1079dda8e5d1SAl Viro *start = skip % PAGE_SIZE; 10803d671ca6SAl Viro return page; 10813d671ca6SAl Viro } 10823d671ca6SAl Viro 108391329559SAl Viro static ssize_t __iov_iter_get_pages_alloc(struct iov_iter *i, 1084d879cb83SAl Viro struct page ***pages, size_t maxsize, 1085d8207640SLogan Gunthorpe unsigned int maxpages, size_t *start, 1086f62e52d1SDavid Howells iov_iter_extraction_t extraction_flags) 1087d879cb83SAl Viro { 1088f62e52d1SDavid Howells unsigned int n, gup_flags = 0; 1089d879cb83SAl Viro 1090d879cb83SAl Viro if (maxsize > i->count) 1091d879cb83SAl Viro maxsize = i->count; 10923d671ca6SAl Viro if (!maxsize) 10933d671ca6SAl Viro return 0; 10947392ed17SAl Viro if (maxsize > MAX_RW_COUNT) 10957392ed17SAl Viro maxsize = MAX_RW_COUNT; 1096f62e52d1SDavid Howells if (extraction_flags & ITER_ALLOW_P2PDMA) 1097f62e52d1SDavid Howells gup_flags |= FOLL_PCI_P2PDMA; 1098d879cb83SAl Viro 1099fcb14cb1SAl Viro if (likely(user_backed_iter(i))) { 11003d671ca6SAl Viro unsigned long addr; 11013cf42da3SAl Viro int res; 11029ea9ce04SDavid Howells 11033337ab08SAndreas Gruenbacher if (iov_iter_rw(i) != WRITE) 11043337ab08SAndreas Gruenbacher gup_flags |= FOLL_WRITE; 11053337ab08SAndreas Gruenbacher if (i->nofault) 11063337ab08SAndreas Gruenbacher gup_flags |= FOLL_NOFAULT; 11073337ab08SAndreas Gruenbacher 1108dd45ab9dSAl Viro addr = first_iovec_segment(i, &maxsize); 1109dd45ab9dSAl Viro *start = addr % PAGE_SIZE; 1110dd45ab9dSAl Viro addr &= PAGE_MASK; 11113cf42da3SAl Viro n = want_pages_array(pages, maxsize, *start, maxpages); 11123cf42da3SAl Viro if (!n) 1113d879cb83SAl Viro return -ENOMEM; 1114451c0ba9SAl Viro res = get_user_pages_fast(addr, n, gup_flags, *pages); 111591329559SAl Viro if (unlikely(res <= 0)) 1116d879cb83SAl Viro return res; 1117eba2d3d7SAl Viro maxsize = min_t(size_t, maxsize, res * PAGE_SIZE - *start); 1118eba2d3d7SAl Viro iov_iter_advance(i, maxsize); 1119eba2d3d7SAl Viro return maxsize; 11203d671ca6SAl Viro } 11213d671ca6SAl Viro if (iov_iter_is_bvec(i)) { 1122451c0ba9SAl Viro struct page **p; 11233d671ca6SAl Viro struct page *page; 11243d671ca6SAl Viro 112559dbd7d0SAl Viro page = first_bvec_segment(i, &maxsize, start); 11263cf42da3SAl Viro n = want_pages_array(pages, maxsize, *start, maxpages); 11273cf42da3SAl Viro if (!n) 1128d879cb83SAl Viro return -ENOMEM; 11293cf42da3SAl Viro p = *pages; 1130dda8e5d1SAl Viro for (int k = 0; k < n; k++) 1131eba2d3d7SAl Viro get_page(p[k] = page + k); 1132eba2d3d7SAl Viro maxsize = min_t(size_t, maxsize, n * PAGE_SIZE - *start); 1133310d9d5aSAl Viro i->count -= maxsize; 1134310d9d5aSAl Viro i->iov_offset += maxsize; 1135310d9d5aSAl Viro if (i->iov_offset == i->bvec->bv_len) { 1136310d9d5aSAl Viro i->iov_offset = 0; 1137310d9d5aSAl Viro i->bvec++; 1138310d9d5aSAl Viro i->nr_segs--; 1139310d9d5aSAl Viro } 1140eba2d3d7SAl Viro return maxsize; 11413d671ca6SAl Viro } 11423d671ca6SAl Viro if (iov_iter_is_xarray(i)) 1143451c0ba9SAl Viro return iter_xarray_get_pages(i, pages, maxsize, maxpages, start); 1144d879cb83SAl Viro return -EFAULT; 1145d879cb83SAl Viro } 114691329559SAl Viro 1147d8207640SLogan Gunthorpe ssize_t iov_iter_get_pages(struct iov_iter *i, 1148451c0ba9SAl Viro struct page **pages, size_t maxsize, unsigned maxpages, 1149f62e52d1SDavid Howells size_t *start, iov_iter_extraction_t extraction_flags) 1150451c0ba9SAl Viro { 1151451c0ba9SAl Viro if (!maxpages) 1152451c0ba9SAl Viro return 0; 1153451c0ba9SAl Viro BUG_ON(!pages); 1154451c0ba9SAl Viro 1155d8207640SLogan Gunthorpe return __iov_iter_get_pages_alloc(i, &pages, maxsize, maxpages, 1156f62e52d1SDavid Howells start, extraction_flags); 1157d8207640SLogan Gunthorpe } 1158d8207640SLogan Gunthorpe EXPORT_SYMBOL_GPL(iov_iter_get_pages); 1159d8207640SLogan Gunthorpe 1160d8207640SLogan Gunthorpe ssize_t iov_iter_get_pages2(struct iov_iter *i, struct page **pages, 1161d8207640SLogan Gunthorpe size_t maxsize, unsigned maxpages, size_t *start) 1162d8207640SLogan Gunthorpe { 1163d8207640SLogan Gunthorpe return iov_iter_get_pages(i, pages, maxsize, maxpages, start, 0); 1164451c0ba9SAl Viro } 1165eba2d3d7SAl Viro EXPORT_SYMBOL(iov_iter_get_pages2); 1166451c0ba9SAl Viro 1167d8207640SLogan Gunthorpe ssize_t iov_iter_get_pages_alloc(struct iov_iter *i, 116891329559SAl Viro struct page ***pages, size_t maxsize, 1169f62e52d1SDavid Howells size_t *start, iov_iter_extraction_t extraction_flags) 117091329559SAl Viro { 117191329559SAl Viro ssize_t len; 117291329559SAl Viro 117391329559SAl Viro *pages = NULL; 117491329559SAl Viro 1175d8207640SLogan Gunthorpe len = __iov_iter_get_pages_alloc(i, pages, maxsize, ~0U, start, 1176f62e52d1SDavid Howells extraction_flags); 117791329559SAl Viro if (len <= 0) { 117891329559SAl Viro kvfree(*pages); 117991329559SAl Viro *pages = NULL; 118091329559SAl Viro } 118191329559SAl Viro return len; 118291329559SAl Viro } 1183d8207640SLogan Gunthorpe EXPORT_SYMBOL_GPL(iov_iter_get_pages_alloc); 1184d8207640SLogan Gunthorpe 1185d8207640SLogan Gunthorpe ssize_t iov_iter_get_pages_alloc2(struct iov_iter *i, 1186d8207640SLogan Gunthorpe struct page ***pages, size_t maxsize, size_t *start) 1187d8207640SLogan Gunthorpe { 1188d8207640SLogan Gunthorpe return iov_iter_get_pages_alloc(i, pages, maxsize, start, 0); 1189d8207640SLogan Gunthorpe } 1190eba2d3d7SAl Viro EXPORT_SYMBOL(iov_iter_get_pages_alloc2); 1191d879cb83SAl Viro 1192d879cb83SAl Viro size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum, 1193d879cb83SAl Viro struct iov_iter *i) 1194d879cb83SAl Viro { 1195d879cb83SAl Viro __wsum sum, next; 1196d879cb83SAl Viro sum = *csum; 1197a41dad90SAl Viro if (WARN_ON_ONCE(!i->data_source)) 1198241699cdSAl Viro return 0; 1199a41dad90SAl Viro 12007baa5099SAl Viro iterate_and_advance(i, bytes, base, len, off, ({ 12017baa5099SAl Viro next = csum_and_copy_from_user(base, addr + off, len); 1202d879cb83SAl Viro sum = csum_block_add(sum, next, off); 12037baa5099SAl Viro next ? 0 : len; 1204d879cb83SAl Viro }), ({ 12057baa5099SAl Viro sum = csum_and_memcpy(addr + off, base, len, sum, off); 1206d879cb83SAl Viro }) 1207d879cb83SAl Viro ) 1208d879cb83SAl Viro *csum = sum; 1209d879cb83SAl Viro return bytes; 1210d879cb83SAl Viro } 1211d879cb83SAl Viro EXPORT_SYMBOL(csum_and_copy_from_iter); 1212d879cb83SAl Viro 121352cbd23aSWillem de Bruijn size_t csum_and_copy_to_iter(const void *addr, size_t bytes, void *_csstate, 1214d879cb83SAl Viro struct iov_iter *i) 1215d879cb83SAl Viro { 121652cbd23aSWillem de Bruijn struct csum_state *csstate = _csstate; 1217d879cb83SAl Viro __wsum sum, next; 121878e1f386SAl Viro 1219a41dad90SAl Viro if (WARN_ON_ONCE(i->data_source)) 1220a41dad90SAl Viro return 0; 122178e1f386SAl Viro if (unlikely(iov_iter_is_discard(i))) { 1222c67f1fd2SAl Viro // can't use csum_memcpy() for that one - data is not copied 1223c67f1fd2SAl Viro csstate->csum = csum_block_add(csstate->csum, 1224c67f1fd2SAl Viro csum_partial(addr, bytes, 0), 1225c67f1fd2SAl Viro csstate->off); 1226c67f1fd2SAl Viro csstate->off += bytes; 1227c67f1fd2SAl Viro return bytes; 1228241699cdSAl Viro } 12296852df12SAl Viro 12306852df12SAl Viro sum = csum_shift(csstate->csum, csstate->off); 1231*3fc40265SDavid Howells iterate_and_advance(i, bytes, base, len, off, ({ 12327baa5099SAl Viro next = csum_and_copy_to_user(addr + off, base, len); 1233d879cb83SAl Viro sum = csum_block_add(sum, next, off); 12347baa5099SAl Viro next ? 0 : len; 1235d879cb83SAl Viro }), ({ 12367baa5099SAl Viro sum = csum_and_memcpy(base, addr + off, len, sum, off); 1237d879cb83SAl Viro }) 1238d879cb83SAl Viro ) 1239594e450bSAl Viro csstate->csum = csum_shift(sum, csstate->off); 1240594e450bSAl Viro csstate->off += bytes; 1241d879cb83SAl Viro return bytes; 1242d879cb83SAl Viro } 1243d879cb83SAl Viro EXPORT_SYMBOL(csum_and_copy_to_iter); 1244d879cb83SAl Viro 1245d05f4435SSagi Grimberg size_t hash_and_copy_to_iter(const void *addr, size_t bytes, void *hashp, 1246d05f4435SSagi Grimberg struct iov_iter *i) 1247d05f4435SSagi Grimberg { 12487999096fSHerbert Xu #ifdef CONFIG_CRYPTO_HASH 1249d05f4435SSagi Grimberg struct ahash_request *hash = hashp; 1250d05f4435SSagi Grimberg struct scatterlist sg; 1251d05f4435SSagi Grimberg size_t copied; 1252d05f4435SSagi Grimberg 1253d05f4435SSagi Grimberg copied = copy_to_iter(addr, bytes, i); 1254d05f4435SSagi Grimberg sg_init_one(&sg, addr, copied); 1255d05f4435SSagi Grimberg ahash_request_set_crypt(hash, &sg, NULL, copied); 1256d05f4435SSagi Grimberg crypto_ahash_update(hash); 1257d05f4435SSagi Grimberg return copied; 125827fad74aSYueHaibing #else 125927fad74aSYueHaibing return 0; 126027fad74aSYueHaibing #endif 1261d05f4435SSagi Grimberg } 1262d05f4435SSagi Grimberg EXPORT_SYMBOL(hash_and_copy_to_iter); 1263d05f4435SSagi Grimberg 126466531c65SAl Viro static int iov_npages(const struct iov_iter *i, int maxpages) 1265d879cb83SAl Viro { 126666531c65SAl Viro size_t skip = i->iov_offset, size = i->count; 126766531c65SAl Viro const struct iovec *p; 1268d879cb83SAl Viro int npages = 0; 1269d879cb83SAl Viro 1270de4f5fedSJens Axboe for (p = iter_iov(i); size; skip = 0, p++) { 127166531c65SAl Viro unsigned offs = offset_in_page(p->iov_base + skip); 127266531c65SAl Viro size_t len = min(p->iov_len - skip, size); 1273d879cb83SAl Viro 127466531c65SAl Viro if (len) { 127566531c65SAl Viro size -= len; 127666531c65SAl Viro npages += DIV_ROUND_UP(offs + len, PAGE_SIZE); 127766531c65SAl Viro if (unlikely(npages > maxpages)) 127866531c65SAl Viro return maxpages; 127966531c65SAl Viro } 128066531c65SAl Viro } 128166531c65SAl Viro return npages; 128266531c65SAl Viro } 128366531c65SAl Viro 128466531c65SAl Viro static int bvec_npages(const struct iov_iter *i, int maxpages) 128566531c65SAl Viro { 128666531c65SAl Viro size_t skip = i->iov_offset, size = i->count; 128766531c65SAl Viro const struct bio_vec *p; 128866531c65SAl Viro int npages = 0; 128966531c65SAl Viro 129066531c65SAl Viro for (p = i->bvec; size; skip = 0, p++) { 129166531c65SAl Viro unsigned offs = (p->bv_offset + skip) % PAGE_SIZE; 129266531c65SAl Viro size_t len = min(p->bv_len - skip, size); 129366531c65SAl Viro 129466531c65SAl Viro size -= len; 129566531c65SAl Viro npages += DIV_ROUND_UP(offs + len, PAGE_SIZE); 129666531c65SAl Viro if (unlikely(npages > maxpages)) 129766531c65SAl Viro return maxpages; 129866531c65SAl Viro } 129966531c65SAl Viro return npages; 130066531c65SAl Viro } 130166531c65SAl Viro 130266531c65SAl Viro int iov_iter_npages(const struct iov_iter *i, int maxpages) 130366531c65SAl Viro { 130466531c65SAl Viro if (unlikely(!i->count)) 130566531c65SAl Viro return 0; 1306fcb14cb1SAl Viro if (likely(iter_is_ubuf(i))) { 1307fcb14cb1SAl Viro unsigned offs = offset_in_page(i->ubuf + i->iov_offset); 1308fcb14cb1SAl Viro int npages = DIV_ROUND_UP(offs + i->count, PAGE_SIZE); 1309fcb14cb1SAl Viro return min(npages, maxpages); 1310fcb14cb1SAl Viro } 131166531c65SAl Viro /* iovec and kvec have identical layouts */ 131266531c65SAl Viro if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i))) 131366531c65SAl Viro return iov_npages(i, maxpages); 131466531c65SAl Viro if (iov_iter_is_bvec(i)) 131566531c65SAl Viro return bvec_npages(i, maxpages); 131666531c65SAl Viro if (iov_iter_is_xarray(i)) { 1317e4f8df86SAl Viro unsigned offset = (i->xarray_start + i->iov_offset) % PAGE_SIZE; 1318e4f8df86SAl Viro int npages = DIV_ROUND_UP(offset + i->count, PAGE_SIZE); 131966531c65SAl Viro return min(npages, maxpages); 132066531c65SAl Viro } 132166531c65SAl Viro return 0; 1322d879cb83SAl Viro } 1323d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_npages); 1324d879cb83SAl Viro 1325d879cb83SAl Viro const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags) 1326d879cb83SAl Viro { 1327d879cb83SAl Viro *new = *old; 132800e23707SDavid Howells if (iov_iter_is_bvec(new)) 1329d879cb83SAl Viro return new->bvec = kmemdup(new->bvec, 1330d879cb83SAl Viro new->nr_segs * sizeof(struct bio_vec), 1331d879cb83SAl Viro flags); 1332fcb14cb1SAl Viro else if (iov_iter_is_kvec(new) || iter_is_iovec(new)) 1333d879cb83SAl Viro /* iovec and kvec have identical layout */ 1334de4f5fedSJens Axboe return new->__iov = kmemdup(new->__iov, 1335d879cb83SAl Viro new->nr_segs * sizeof(struct iovec), 1336d879cb83SAl Viro flags); 1337fcb14cb1SAl Viro return NULL; 1338d879cb83SAl Viro } 1339d879cb83SAl Viro EXPORT_SYMBOL(dup_iter); 1340bc917be8SAl Viro 134150f9a76eSJosh Poimboeuf static __noclone int copy_compat_iovec_from_user(struct iovec *iov, 1342bfdc5970SChristoph Hellwig const struct iovec __user *uvec, unsigned long nr_segs) 1343bfdc5970SChristoph Hellwig { 1344bfdc5970SChristoph Hellwig const struct compat_iovec __user *uiov = 1345bfdc5970SChristoph Hellwig (const struct compat_iovec __user *)uvec; 1346bfdc5970SChristoph Hellwig int ret = -EFAULT, i; 1347bfdc5970SChristoph Hellwig 1348a959a978SChristoph Hellwig if (!user_access_begin(uiov, nr_segs * sizeof(*uiov))) 1349bfdc5970SChristoph Hellwig return -EFAULT; 1350bfdc5970SChristoph Hellwig 1351bfdc5970SChristoph Hellwig for (i = 0; i < nr_segs; i++) { 1352bfdc5970SChristoph Hellwig compat_uptr_t buf; 1353bfdc5970SChristoph Hellwig compat_ssize_t len; 1354bfdc5970SChristoph Hellwig 1355bfdc5970SChristoph Hellwig unsafe_get_user(len, &uiov[i].iov_len, uaccess_end); 1356bfdc5970SChristoph Hellwig unsafe_get_user(buf, &uiov[i].iov_base, uaccess_end); 1357bfdc5970SChristoph Hellwig 1358bfdc5970SChristoph Hellwig /* check for compat_size_t not fitting in compat_ssize_t .. */ 1359bfdc5970SChristoph Hellwig if (len < 0) { 1360bfdc5970SChristoph Hellwig ret = -EINVAL; 1361bfdc5970SChristoph Hellwig goto uaccess_end; 1362bfdc5970SChristoph Hellwig } 1363bfdc5970SChristoph Hellwig iov[i].iov_base = compat_ptr(buf); 1364bfdc5970SChristoph Hellwig iov[i].iov_len = len; 1365bfdc5970SChristoph Hellwig } 1366bfdc5970SChristoph Hellwig 1367bfdc5970SChristoph Hellwig ret = 0; 1368bfdc5970SChristoph Hellwig uaccess_end: 1369bfdc5970SChristoph Hellwig user_access_end(); 1370bfdc5970SChristoph Hellwig return ret; 1371bfdc5970SChristoph Hellwig } 1372bfdc5970SChristoph Hellwig 1373bfdc5970SChristoph Hellwig static int copy_iovec_from_user(struct iovec *iov, 1374487c20b0SLinus Torvalds const struct iovec __user *uiov, unsigned long nr_segs) 1375fb041b59SDavid Laight { 1376487c20b0SLinus Torvalds int ret = -EFAULT; 1377bfdc5970SChristoph Hellwig 1378487c20b0SLinus Torvalds if (!user_access_begin(uiov, nr_segs * sizeof(*uiov))) 1379bfdc5970SChristoph Hellwig return -EFAULT; 1380bfdc5970SChristoph Hellwig 1381487c20b0SLinus Torvalds do { 1382487c20b0SLinus Torvalds void __user *buf; 1383487c20b0SLinus Torvalds ssize_t len; 1384487c20b0SLinus Torvalds 1385487c20b0SLinus Torvalds unsafe_get_user(len, &uiov->iov_len, uaccess_end); 1386487c20b0SLinus Torvalds unsafe_get_user(buf, &uiov->iov_base, uaccess_end); 1387487c20b0SLinus Torvalds 1388487c20b0SLinus Torvalds /* check for size_t not fitting in ssize_t .. */ 1389487c20b0SLinus Torvalds if (unlikely(len < 0)) { 1390487c20b0SLinus Torvalds ret = -EINVAL; 1391487c20b0SLinus Torvalds goto uaccess_end; 1392487c20b0SLinus Torvalds } 1393487c20b0SLinus Torvalds iov->iov_base = buf; 1394487c20b0SLinus Torvalds iov->iov_len = len; 1395487c20b0SLinus Torvalds 1396487c20b0SLinus Torvalds uiov++; iov++; 1397487c20b0SLinus Torvalds } while (--nr_segs); 1398487c20b0SLinus Torvalds 1399487c20b0SLinus Torvalds ret = 0; 1400487c20b0SLinus Torvalds uaccess_end: 1401487c20b0SLinus Torvalds user_access_end(); 1402487c20b0SLinus Torvalds return ret; 1403bfdc5970SChristoph Hellwig } 1404bfdc5970SChristoph Hellwig 1405bfdc5970SChristoph Hellwig struct iovec *iovec_from_user(const struct iovec __user *uvec, 1406bfdc5970SChristoph Hellwig unsigned long nr_segs, unsigned long fast_segs, 1407bfdc5970SChristoph Hellwig struct iovec *fast_iov, bool compat) 1408bfdc5970SChristoph Hellwig { 1409bfdc5970SChristoph Hellwig struct iovec *iov = fast_iov; 1410bfdc5970SChristoph Hellwig int ret; 1411fb041b59SDavid Laight 1412fb041b59SDavid Laight /* 1413bfdc5970SChristoph Hellwig * SuS says "The readv() function *may* fail if the iovcnt argument was 1414bfdc5970SChristoph Hellwig * less than or equal to 0, or greater than {IOV_MAX}. Linux has 1415fb041b59SDavid Laight * traditionally returned zero for zero segments, so... 1416fb041b59SDavid Laight */ 1417bfdc5970SChristoph Hellwig if (nr_segs == 0) 1418bfdc5970SChristoph Hellwig return iov; 1419bfdc5970SChristoph Hellwig if (nr_segs > UIO_MAXIOV) 1420bfdc5970SChristoph Hellwig return ERR_PTR(-EINVAL); 1421fb041b59SDavid Laight if (nr_segs > fast_segs) { 1422fb041b59SDavid Laight iov = kmalloc_array(nr_segs, sizeof(struct iovec), GFP_KERNEL); 1423bfdc5970SChristoph Hellwig if (!iov) 1424bfdc5970SChristoph Hellwig return ERR_PTR(-ENOMEM); 1425fb041b59SDavid Laight } 1426bfdc5970SChristoph Hellwig 1427487c20b0SLinus Torvalds if (unlikely(compat)) 1428bfdc5970SChristoph Hellwig ret = copy_compat_iovec_from_user(iov, uvec, nr_segs); 1429bfdc5970SChristoph Hellwig else 1430bfdc5970SChristoph Hellwig ret = copy_iovec_from_user(iov, uvec, nr_segs); 1431bfdc5970SChristoph Hellwig if (ret) { 1432bfdc5970SChristoph Hellwig if (iov != fast_iov) 1433bfdc5970SChristoph Hellwig kfree(iov); 1434bfdc5970SChristoph Hellwig return ERR_PTR(ret); 1435fb041b59SDavid Laight } 1436bfdc5970SChristoph Hellwig 1437bfdc5970SChristoph Hellwig return iov; 1438bfdc5970SChristoph Hellwig } 1439bfdc5970SChristoph Hellwig 14403b2deb0eSJens Axboe /* 14413b2deb0eSJens Axboe * Single segment iovec supplied by the user, import it as ITER_UBUF. 14423b2deb0eSJens Axboe */ 14433b2deb0eSJens Axboe static ssize_t __import_iovec_ubuf(int type, const struct iovec __user *uvec, 14443b2deb0eSJens Axboe struct iovec **iovp, struct iov_iter *i, 14453b2deb0eSJens Axboe bool compat) 14463b2deb0eSJens Axboe { 14473b2deb0eSJens Axboe struct iovec *iov = *iovp; 14483b2deb0eSJens Axboe ssize_t ret; 14493b2deb0eSJens Axboe 14503b2deb0eSJens Axboe if (compat) 14513b2deb0eSJens Axboe ret = copy_compat_iovec_from_user(iov, uvec, 1); 14523b2deb0eSJens Axboe else 14533b2deb0eSJens Axboe ret = copy_iovec_from_user(iov, uvec, 1); 14543b2deb0eSJens Axboe if (unlikely(ret)) 14553b2deb0eSJens Axboe return ret; 14563b2deb0eSJens Axboe 14573b2deb0eSJens Axboe ret = import_ubuf(type, iov->iov_base, iov->iov_len, i); 14583b2deb0eSJens Axboe if (unlikely(ret)) 14593b2deb0eSJens Axboe return ret; 14603b2deb0eSJens Axboe *iovp = NULL; 14613b2deb0eSJens Axboe return i->count; 14623b2deb0eSJens Axboe } 14633b2deb0eSJens Axboe 1464bfdc5970SChristoph Hellwig ssize_t __import_iovec(int type, const struct iovec __user *uvec, 1465bfdc5970SChristoph Hellwig unsigned nr_segs, unsigned fast_segs, struct iovec **iovp, 1466bfdc5970SChristoph Hellwig struct iov_iter *i, bool compat) 1467bfdc5970SChristoph Hellwig { 1468bfdc5970SChristoph Hellwig ssize_t total_len = 0; 1469bfdc5970SChristoph Hellwig unsigned long seg; 1470bfdc5970SChristoph Hellwig struct iovec *iov; 1471bfdc5970SChristoph Hellwig 14723b2deb0eSJens Axboe if (nr_segs == 1) 14733b2deb0eSJens Axboe return __import_iovec_ubuf(type, uvec, iovp, i, compat); 14743b2deb0eSJens Axboe 1475bfdc5970SChristoph Hellwig iov = iovec_from_user(uvec, nr_segs, fast_segs, *iovp, compat); 1476bfdc5970SChristoph Hellwig if (IS_ERR(iov)) { 1477bfdc5970SChristoph Hellwig *iovp = NULL; 1478bfdc5970SChristoph Hellwig return PTR_ERR(iov); 1479fb041b59SDavid Laight } 1480fb041b59SDavid Laight 1481fb041b59SDavid Laight /* 1482bfdc5970SChristoph Hellwig * According to the Single Unix Specification we should return EINVAL if 1483bfdc5970SChristoph Hellwig * an element length is < 0 when cast to ssize_t or if the total length 1484bfdc5970SChristoph Hellwig * would overflow the ssize_t return value of the system call. 1485fb041b59SDavid Laight * 1486fb041b59SDavid Laight * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the 1487fb041b59SDavid Laight * overflow case. 1488fb041b59SDavid Laight */ 1489fb041b59SDavid Laight for (seg = 0; seg < nr_segs; seg++) { 1490fb041b59SDavid Laight ssize_t len = (ssize_t)iov[seg].iov_len; 1491fb041b59SDavid Laight 1492bfdc5970SChristoph Hellwig if (!access_ok(iov[seg].iov_base, len)) { 1493bfdc5970SChristoph Hellwig if (iov != *iovp) 1494bfdc5970SChristoph Hellwig kfree(iov); 1495bfdc5970SChristoph Hellwig *iovp = NULL; 1496bfdc5970SChristoph Hellwig return -EFAULT; 1497fb041b59SDavid Laight } 1498bfdc5970SChristoph Hellwig 1499bfdc5970SChristoph Hellwig if (len > MAX_RW_COUNT - total_len) { 1500bfdc5970SChristoph Hellwig len = MAX_RW_COUNT - total_len; 1501fb041b59SDavid Laight iov[seg].iov_len = len; 1502fb041b59SDavid Laight } 1503bfdc5970SChristoph Hellwig total_len += len; 1504fb041b59SDavid Laight } 1505bfdc5970SChristoph Hellwig 1506bfdc5970SChristoph Hellwig iov_iter_init(i, type, iov, nr_segs, total_len); 1507bfdc5970SChristoph Hellwig if (iov == *iovp) 1508bfdc5970SChristoph Hellwig *iovp = NULL; 1509bfdc5970SChristoph Hellwig else 1510bfdc5970SChristoph Hellwig *iovp = iov; 1511bfdc5970SChristoph Hellwig return total_len; 1512fb041b59SDavid Laight } 1513fb041b59SDavid Laight 1514ffecee4fSVegard Nossum /** 1515ffecee4fSVegard Nossum * import_iovec() - Copy an array of &struct iovec from userspace 1516ffecee4fSVegard Nossum * into the kernel, check that it is valid, and initialize a new 1517ffecee4fSVegard Nossum * &struct iov_iter iterator to access it. 1518ffecee4fSVegard Nossum * 1519ffecee4fSVegard Nossum * @type: One of %READ or %WRITE. 1520bfdc5970SChristoph Hellwig * @uvec: Pointer to the userspace array. 1521ffecee4fSVegard Nossum * @nr_segs: Number of elements in userspace array. 1522ffecee4fSVegard Nossum * @fast_segs: Number of elements in @iov. 1523bfdc5970SChristoph Hellwig * @iovp: (input and output parameter) Pointer to pointer to (usually small 1524ffecee4fSVegard Nossum * on-stack) kernel array. 1525ffecee4fSVegard Nossum * @i: Pointer to iterator that will be initialized on success. 1526ffecee4fSVegard Nossum * 1527ffecee4fSVegard Nossum * If the array pointed to by *@iov is large enough to hold all @nr_segs, 1528ffecee4fSVegard Nossum * then this function places %NULL in *@iov on return. Otherwise, a new 1529ffecee4fSVegard Nossum * array will be allocated and the result placed in *@iov. This means that 1530ffecee4fSVegard Nossum * the caller may call kfree() on *@iov regardless of whether the small 1531ffecee4fSVegard Nossum * on-stack array was used or not (and regardless of whether this function 1532ffecee4fSVegard Nossum * returns an error or not). 1533ffecee4fSVegard Nossum * 153487e5e6daSJens Axboe * Return: Negative error code on error, bytes imported on success 1535ffecee4fSVegard Nossum */ 1536bfdc5970SChristoph Hellwig ssize_t import_iovec(int type, const struct iovec __user *uvec, 1537bc917be8SAl Viro unsigned nr_segs, unsigned fast_segs, 1538bfdc5970SChristoph Hellwig struct iovec **iovp, struct iov_iter *i) 1539bc917be8SAl Viro { 154089cd35c5SChristoph Hellwig return __import_iovec(type, uvec, nr_segs, fast_segs, iovp, i, 154189cd35c5SChristoph Hellwig in_compat_syscall()); 1542bc917be8SAl Viro } 1543bc917be8SAl Viro EXPORT_SYMBOL(import_iovec); 1544bc917be8SAl Viro 1545bc917be8SAl Viro int import_single_range(int rw, void __user *buf, size_t len, 1546bc917be8SAl Viro struct iovec *iov, struct iov_iter *i) 1547bc917be8SAl Viro { 1548bc917be8SAl Viro if (len > MAX_RW_COUNT) 1549bc917be8SAl Viro len = MAX_RW_COUNT; 155096d4f267SLinus Torvalds if (unlikely(!access_ok(buf, len))) 1551bc917be8SAl Viro return -EFAULT; 1552bc917be8SAl Viro 1553e03ad4eeSJens Axboe iov_iter_ubuf(i, rw, buf, len); 1554bc917be8SAl Viro return 0; 1555bc917be8SAl Viro } 1556e1267585SAl Viro EXPORT_SYMBOL(import_single_range); 15578fb0f47aSJens Axboe 15582ad9bd83SJens Axboe int import_ubuf(int rw, void __user *buf, size_t len, struct iov_iter *i) 15592ad9bd83SJens Axboe { 15602ad9bd83SJens Axboe if (len > MAX_RW_COUNT) 15612ad9bd83SJens Axboe len = MAX_RW_COUNT; 15622ad9bd83SJens Axboe if (unlikely(!access_ok(buf, len))) 15632ad9bd83SJens Axboe return -EFAULT; 15642ad9bd83SJens Axboe 15652ad9bd83SJens Axboe iov_iter_ubuf(i, rw, buf, len); 15662ad9bd83SJens Axboe return 0; 15672ad9bd83SJens Axboe } 15682ad9bd83SJens Axboe 15698fb0f47aSJens Axboe /** 15708fb0f47aSJens Axboe * iov_iter_restore() - Restore a &struct iov_iter to the same state as when 15718fb0f47aSJens Axboe * iov_iter_save_state() was called. 15728fb0f47aSJens Axboe * 15738fb0f47aSJens Axboe * @i: &struct iov_iter to restore 15748fb0f47aSJens Axboe * @state: state to restore from 15758fb0f47aSJens Axboe * 15768fb0f47aSJens Axboe * Used after iov_iter_save_state() to bring restore @i, if operations may 15778fb0f47aSJens Axboe * have advanced it. 15788fb0f47aSJens Axboe * 15798fb0f47aSJens Axboe * Note: only works on ITER_IOVEC, ITER_BVEC, and ITER_KVEC 15808fb0f47aSJens Axboe */ 15818fb0f47aSJens Axboe void iov_iter_restore(struct iov_iter *i, struct iov_iter_state *state) 15828fb0f47aSJens Axboe { 15834397a17cSKeith Busch if (WARN_ON_ONCE(!iov_iter_is_bvec(i) && !iter_is_iovec(i) && 15844397a17cSKeith Busch !iter_is_ubuf(i)) && !iov_iter_is_kvec(i)) 15858fb0f47aSJens Axboe return; 15868fb0f47aSJens Axboe i->iov_offset = state->iov_offset; 15878fb0f47aSJens Axboe i->count = state->count; 1588fcb14cb1SAl Viro if (iter_is_ubuf(i)) 1589fcb14cb1SAl Viro return; 15908fb0f47aSJens Axboe /* 15918fb0f47aSJens Axboe * For the *vec iters, nr_segs + iov is constant - if we increment 15928fb0f47aSJens Axboe * the vec, then we also decrement the nr_segs count. Hence we don't 15938fb0f47aSJens Axboe * need to track both of these, just one is enough and we can deduct 15948fb0f47aSJens Axboe * the other from that. ITER_KVEC and ITER_IOVEC are the same struct 15958fb0f47aSJens Axboe * size, so we can just increment the iov pointer as they are unionzed. 15968fb0f47aSJens Axboe * ITER_BVEC _may_ be the same size on some archs, but on others it is 15978fb0f47aSJens Axboe * not. Be safe and handle it separately. 15988fb0f47aSJens Axboe */ 15998fb0f47aSJens Axboe BUILD_BUG_ON(sizeof(struct iovec) != sizeof(struct kvec)); 16008fb0f47aSJens Axboe if (iov_iter_is_bvec(i)) 16018fb0f47aSJens Axboe i->bvec -= state->nr_segs - i->nr_segs; 16028fb0f47aSJens Axboe else 1603de4f5fedSJens Axboe i->__iov -= state->nr_segs - i->nr_segs; 16048fb0f47aSJens Axboe i->nr_segs = state->nr_segs; 16058fb0f47aSJens Axboe } 16067d58fe73SDavid Howells 16077d58fe73SDavid Howells /* 16087d58fe73SDavid Howells * Extract a list of contiguous pages from an ITER_XARRAY iterator. This does not 16097d58fe73SDavid Howells * get references on the pages, nor does it get a pin on them. 16107d58fe73SDavid Howells */ 16117d58fe73SDavid Howells static ssize_t iov_iter_extract_xarray_pages(struct iov_iter *i, 16127d58fe73SDavid Howells struct page ***pages, size_t maxsize, 16137d58fe73SDavid Howells unsigned int maxpages, 16147d58fe73SDavid Howells iov_iter_extraction_t extraction_flags, 16157d58fe73SDavid Howells size_t *offset0) 16167d58fe73SDavid Howells { 16177d58fe73SDavid Howells struct page *page, **p; 16187d58fe73SDavid Howells unsigned int nr = 0, offset; 16197d58fe73SDavid Howells loff_t pos = i->xarray_start + i->iov_offset; 16207d58fe73SDavid Howells pgoff_t index = pos >> PAGE_SHIFT; 16217d58fe73SDavid Howells XA_STATE(xas, i->xarray, index); 16227d58fe73SDavid Howells 16237d58fe73SDavid Howells offset = pos & ~PAGE_MASK; 16247d58fe73SDavid Howells *offset0 = offset; 16257d58fe73SDavid Howells 16267d58fe73SDavid Howells maxpages = want_pages_array(pages, maxsize, offset, maxpages); 16277d58fe73SDavid Howells if (!maxpages) 16287d58fe73SDavid Howells return -ENOMEM; 16297d58fe73SDavid Howells p = *pages; 16307d58fe73SDavid Howells 16317d58fe73SDavid Howells rcu_read_lock(); 16327d58fe73SDavid Howells for (page = xas_load(&xas); page; page = xas_next(&xas)) { 16337d58fe73SDavid Howells if (xas_retry(&xas, page)) 16347d58fe73SDavid Howells continue; 16357d58fe73SDavid Howells 16367d58fe73SDavid Howells /* Has the page moved or been split? */ 16377d58fe73SDavid Howells if (unlikely(page != xas_reload(&xas))) { 16387d58fe73SDavid Howells xas_reset(&xas); 16397d58fe73SDavid Howells continue; 16407d58fe73SDavid Howells } 16417d58fe73SDavid Howells 16427d58fe73SDavid Howells p[nr++] = find_subpage(page, xas.xa_index); 16437d58fe73SDavid Howells if (nr == maxpages) 16447d58fe73SDavid Howells break; 16457d58fe73SDavid Howells } 16467d58fe73SDavid Howells rcu_read_unlock(); 16477d58fe73SDavid Howells 16487d58fe73SDavid Howells maxsize = min_t(size_t, nr * PAGE_SIZE - offset, maxsize); 16497d58fe73SDavid Howells iov_iter_advance(i, maxsize); 16507d58fe73SDavid Howells return maxsize; 16517d58fe73SDavid Howells } 16527d58fe73SDavid Howells 16537d58fe73SDavid Howells /* 16547d58fe73SDavid Howells * Extract a list of contiguous pages from an ITER_BVEC iterator. This does 16557d58fe73SDavid Howells * not get references on the pages, nor does it get a pin on them. 16567d58fe73SDavid Howells */ 16577d58fe73SDavid Howells static ssize_t iov_iter_extract_bvec_pages(struct iov_iter *i, 16587d58fe73SDavid Howells struct page ***pages, size_t maxsize, 16597d58fe73SDavid Howells unsigned int maxpages, 16607d58fe73SDavid Howells iov_iter_extraction_t extraction_flags, 16617d58fe73SDavid Howells size_t *offset0) 16627d58fe73SDavid Howells { 16637d58fe73SDavid Howells struct page **p, *page; 16647d58fe73SDavid Howells size_t skip = i->iov_offset, offset; 16657d58fe73SDavid Howells int k; 16667d58fe73SDavid Howells 16677d58fe73SDavid Howells for (;;) { 16687d58fe73SDavid Howells if (i->nr_segs == 0) 16697d58fe73SDavid Howells return 0; 16707d58fe73SDavid Howells maxsize = min(maxsize, i->bvec->bv_len - skip); 16717d58fe73SDavid Howells if (maxsize) 16727d58fe73SDavid Howells break; 16737d58fe73SDavid Howells i->iov_offset = 0; 16747d58fe73SDavid Howells i->nr_segs--; 16757d58fe73SDavid Howells i->bvec++; 16767d58fe73SDavid Howells skip = 0; 16777d58fe73SDavid Howells } 16787d58fe73SDavid Howells 16797d58fe73SDavid Howells skip += i->bvec->bv_offset; 16807d58fe73SDavid Howells page = i->bvec->bv_page + skip / PAGE_SIZE; 16817d58fe73SDavid Howells offset = skip % PAGE_SIZE; 16827d58fe73SDavid Howells *offset0 = offset; 16837d58fe73SDavid Howells 16847d58fe73SDavid Howells maxpages = want_pages_array(pages, maxsize, offset, maxpages); 16857d58fe73SDavid Howells if (!maxpages) 16867d58fe73SDavid Howells return -ENOMEM; 16877d58fe73SDavid Howells p = *pages; 16887d58fe73SDavid Howells for (k = 0; k < maxpages; k++) 16897d58fe73SDavid Howells p[k] = page + k; 16907d58fe73SDavid Howells 16917d58fe73SDavid Howells maxsize = min_t(size_t, maxsize, maxpages * PAGE_SIZE - offset); 16927d58fe73SDavid Howells iov_iter_advance(i, maxsize); 16937d58fe73SDavid Howells return maxsize; 16947d58fe73SDavid Howells } 16957d58fe73SDavid Howells 16967d58fe73SDavid Howells /* 16977d58fe73SDavid Howells * Extract a list of virtually contiguous pages from an ITER_KVEC iterator. 16987d58fe73SDavid Howells * This does not get references on the pages, nor does it get a pin on them. 16997d58fe73SDavid Howells */ 17007d58fe73SDavid Howells static ssize_t iov_iter_extract_kvec_pages(struct iov_iter *i, 17017d58fe73SDavid Howells struct page ***pages, size_t maxsize, 17027d58fe73SDavid Howells unsigned int maxpages, 17037d58fe73SDavid Howells iov_iter_extraction_t extraction_flags, 17047d58fe73SDavid Howells size_t *offset0) 17057d58fe73SDavid Howells { 17067d58fe73SDavid Howells struct page **p, *page; 17077d58fe73SDavid Howells const void *kaddr; 17087d58fe73SDavid Howells size_t skip = i->iov_offset, offset, len; 17097d58fe73SDavid Howells int k; 17107d58fe73SDavid Howells 17117d58fe73SDavid Howells for (;;) { 17127d58fe73SDavid Howells if (i->nr_segs == 0) 17137d58fe73SDavid Howells return 0; 17147d58fe73SDavid Howells maxsize = min(maxsize, i->kvec->iov_len - skip); 17157d58fe73SDavid Howells if (maxsize) 17167d58fe73SDavid Howells break; 17177d58fe73SDavid Howells i->iov_offset = 0; 17187d58fe73SDavid Howells i->nr_segs--; 17197d58fe73SDavid Howells i->kvec++; 17207d58fe73SDavid Howells skip = 0; 17217d58fe73SDavid Howells } 17227d58fe73SDavid Howells 17237d58fe73SDavid Howells kaddr = i->kvec->iov_base + skip; 17247d58fe73SDavid Howells offset = (unsigned long)kaddr & ~PAGE_MASK; 17257d58fe73SDavid Howells *offset0 = offset; 17267d58fe73SDavid Howells 17277d58fe73SDavid Howells maxpages = want_pages_array(pages, maxsize, offset, maxpages); 17287d58fe73SDavid Howells if (!maxpages) 17297d58fe73SDavid Howells return -ENOMEM; 17307d58fe73SDavid Howells p = *pages; 17317d58fe73SDavid Howells 17327d58fe73SDavid Howells kaddr -= offset; 17337d58fe73SDavid Howells len = offset + maxsize; 17347d58fe73SDavid Howells for (k = 0; k < maxpages; k++) { 17357d58fe73SDavid Howells size_t seg = min_t(size_t, len, PAGE_SIZE); 17367d58fe73SDavid Howells 17377d58fe73SDavid Howells if (is_vmalloc_or_module_addr(kaddr)) 17387d58fe73SDavid Howells page = vmalloc_to_page(kaddr); 17397d58fe73SDavid Howells else 17407d58fe73SDavid Howells page = virt_to_page(kaddr); 17417d58fe73SDavid Howells 17427d58fe73SDavid Howells p[k] = page; 17437d58fe73SDavid Howells len -= seg; 17447d58fe73SDavid Howells kaddr += PAGE_SIZE; 17457d58fe73SDavid Howells } 17467d58fe73SDavid Howells 17477d58fe73SDavid Howells maxsize = min_t(size_t, maxsize, maxpages * PAGE_SIZE - offset); 17487d58fe73SDavid Howells iov_iter_advance(i, maxsize); 17497d58fe73SDavid Howells return maxsize; 17507d58fe73SDavid Howells } 17517d58fe73SDavid Howells 17527d58fe73SDavid Howells /* 17537d58fe73SDavid Howells * Extract a list of contiguous pages from a user iterator and get a pin on 17547d58fe73SDavid Howells * each of them. This should only be used if the iterator is user-backed 17557d58fe73SDavid Howells * (IOBUF/UBUF). 17567d58fe73SDavid Howells * 17577d58fe73SDavid Howells * It does not get refs on the pages, but the pages must be unpinned by the 17587d58fe73SDavid Howells * caller once the transfer is complete. 17597d58fe73SDavid Howells * 17607d58fe73SDavid Howells * This is safe to be used where background IO/DMA *is* going to be modifying 17617d58fe73SDavid Howells * the buffer; using a pin rather than a ref makes forces fork() to give the 17627d58fe73SDavid Howells * child a copy of the page. 17637d58fe73SDavid Howells */ 17647d58fe73SDavid Howells static ssize_t iov_iter_extract_user_pages(struct iov_iter *i, 17657d58fe73SDavid Howells struct page ***pages, 17667d58fe73SDavid Howells size_t maxsize, 17677d58fe73SDavid Howells unsigned int maxpages, 17687d58fe73SDavid Howells iov_iter_extraction_t extraction_flags, 17697d58fe73SDavid Howells size_t *offset0) 17707d58fe73SDavid Howells { 17717d58fe73SDavid Howells unsigned long addr; 17727d58fe73SDavid Howells unsigned int gup_flags = 0; 17737d58fe73SDavid Howells size_t offset; 17747d58fe73SDavid Howells int res; 17757d58fe73SDavid Howells 17767d58fe73SDavid Howells if (i->data_source == ITER_DEST) 17777d58fe73SDavid Howells gup_flags |= FOLL_WRITE; 17787d58fe73SDavid Howells if (extraction_flags & ITER_ALLOW_P2PDMA) 17797d58fe73SDavid Howells gup_flags |= FOLL_PCI_P2PDMA; 17807d58fe73SDavid Howells if (i->nofault) 17817d58fe73SDavid Howells gup_flags |= FOLL_NOFAULT; 17827d58fe73SDavid Howells 17837d58fe73SDavid Howells addr = first_iovec_segment(i, &maxsize); 17847d58fe73SDavid Howells *offset0 = offset = addr % PAGE_SIZE; 17857d58fe73SDavid Howells addr &= PAGE_MASK; 17867d58fe73SDavid Howells maxpages = want_pages_array(pages, maxsize, offset, maxpages); 17877d58fe73SDavid Howells if (!maxpages) 17887d58fe73SDavid Howells return -ENOMEM; 17897d58fe73SDavid Howells res = pin_user_pages_fast(addr, maxpages, gup_flags, *pages); 17907d58fe73SDavid Howells if (unlikely(res <= 0)) 17917d58fe73SDavid Howells return res; 17927d58fe73SDavid Howells maxsize = min_t(size_t, maxsize, res * PAGE_SIZE - offset); 17937d58fe73SDavid Howells iov_iter_advance(i, maxsize); 17947d58fe73SDavid Howells return maxsize; 17957d58fe73SDavid Howells } 17967d58fe73SDavid Howells 17977d58fe73SDavid Howells /** 17987d58fe73SDavid Howells * iov_iter_extract_pages - Extract a list of contiguous pages from an iterator 17997d58fe73SDavid Howells * @i: The iterator to extract from 18007d58fe73SDavid Howells * @pages: Where to return the list of pages 18017d58fe73SDavid Howells * @maxsize: The maximum amount of iterator to extract 18027d58fe73SDavid Howells * @maxpages: The maximum size of the list of pages 18037d58fe73SDavid Howells * @extraction_flags: Flags to qualify request 18047d58fe73SDavid Howells * @offset0: Where to return the starting offset into (*@pages)[0] 18057d58fe73SDavid Howells * 18067d58fe73SDavid Howells * Extract a list of contiguous pages from the current point of the iterator, 18077d58fe73SDavid Howells * advancing the iterator. The maximum number of pages and the maximum amount 18087d58fe73SDavid Howells * of page contents can be set. 18097d58fe73SDavid Howells * 18107d58fe73SDavid Howells * If *@pages is NULL, a page list will be allocated to the required size and 18117d58fe73SDavid Howells * *@pages will be set to its base. If *@pages is not NULL, it will be assumed 18127d58fe73SDavid Howells * that the caller allocated a page list at least @maxpages in size and this 18137d58fe73SDavid Howells * will be filled in. 18147d58fe73SDavid Howells * 18157d58fe73SDavid Howells * @extraction_flags can have ITER_ALLOW_P2PDMA set to request peer-to-peer DMA 18167d58fe73SDavid Howells * be allowed on the pages extracted. 18177d58fe73SDavid Howells * 18187d58fe73SDavid Howells * The iov_iter_extract_will_pin() function can be used to query how cleanup 18197d58fe73SDavid Howells * should be performed. 18207d58fe73SDavid Howells * 18217d58fe73SDavid Howells * Extra refs or pins on the pages may be obtained as follows: 18227d58fe73SDavid Howells * 18237d58fe73SDavid Howells * (*) If the iterator is user-backed (ITER_IOVEC/ITER_UBUF), pins will be 18247d58fe73SDavid Howells * added to the pages, but refs will not be taken. 18257d58fe73SDavid Howells * iov_iter_extract_will_pin() will return true. 18267d58fe73SDavid Howells * 18277d58fe73SDavid Howells * (*) If the iterator is ITER_KVEC, ITER_BVEC or ITER_XARRAY, the pages are 18287d58fe73SDavid Howells * merely listed; no extra refs or pins are obtained. 18297d58fe73SDavid Howells * iov_iter_extract_will_pin() will return 0. 18307d58fe73SDavid Howells * 18317d58fe73SDavid Howells * Note also: 18327d58fe73SDavid Howells * 18337d58fe73SDavid Howells * (*) Use with ITER_DISCARD is not supported as that has no content. 18347d58fe73SDavid Howells * 18357d58fe73SDavid Howells * On success, the function sets *@pages to the new pagelist, if allocated, and 18367d58fe73SDavid Howells * sets *offset0 to the offset into the first page. 18377d58fe73SDavid Howells * 18387d58fe73SDavid Howells * It may also return -ENOMEM and -EFAULT. 18397d58fe73SDavid Howells */ 18407d58fe73SDavid Howells ssize_t iov_iter_extract_pages(struct iov_iter *i, 18417d58fe73SDavid Howells struct page ***pages, 18427d58fe73SDavid Howells size_t maxsize, 18437d58fe73SDavid Howells unsigned int maxpages, 18447d58fe73SDavid Howells iov_iter_extraction_t extraction_flags, 18457d58fe73SDavid Howells size_t *offset0) 18467d58fe73SDavid Howells { 18477d58fe73SDavid Howells maxsize = min_t(size_t, min_t(size_t, maxsize, i->count), MAX_RW_COUNT); 18487d58fe73SDavid Howells if (!maxsize) 18497d58fe73SDavid Howells return 0; 18507d58fe73SDavid Howells 18517d58fe73SDavid Howells if (likely(user_backed_iter(i))) 18527d58fe73SDavid Howells return iov_iter_extract_user_pages(i, pages, maxsize, 18537d58fe73SDavid Howells maxpages, extraction_flags, 18547d58fe73SDavid Howells offset0); 18557d58fe73SDavid Howells if (iov_iter_is_kvec(i)) 18567d58fe73SDavid Howells return iov_iter_extract_kvec_pages(i, pages, maxsize, 18577d58fe73SDavid Howells maxpages, extraction_flags, 18587d58fe73SDavid Howells offset0); 18597d58fe73SDavid Howells if (iov_iter_is_bvec(i)) 18607d58fe73SDavid Howells return iov_iter_extract_bvec_pages(i, pages, maxsize, 18617d58fe73SDavid Howells maxpages, extraction_flags, 18627d58fe73SDavid Howells offset0); 18637d58fe73SDavid Howells if (iov_iter_is_xarray(i)) 18647d58fe73SDavid Howells return iov_iter_extract_xarray_pages(i, pages, maxsize, 18657d58fe73SDavid Howells maxpages, extraction_flags, 18667d58fe73SDavid Howells offset0); 18677d58fe73SDavid Howells return -EFAULT; 18687d58fe73SDavid Howells } 18697d58fe73SDavid Howells EXPORT_SYMBOL_GPL(iov_iter_extract_pages); 1870