Merge tag 'mm-stable-2023-04-27-15-30' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-block.git] / lib / iov_iter.c
index 967fba189c5ff068e6da252a6d80e22defb26cc8..c3dbe994112c671783e703057c006846cd561333 100644 (file)
@@ -172,6 +172,18 @@ static int copyout(void __user *to, const void *from, size_t n)
        return n;
 }
 
+static int copyout_nofault(void __user *to, const void *from, size_t n)
+{
+       long res;
+
+       if (should_fail_usercopy())
+               return n;
+
+       res = copy_to_user_nofault(to, from, n);
+
+       return res < 0 ? n : res;
+}
+
 static int copyin(void *to, const void __user *from, size_t n)
 {
        size_t res = n;
@@ -734,6 +746,42 @@ size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
 }
 EXPORT_SYMBOL(copy_page_to_iter);
 
+size_t copy_page_to_iter_nofault(struct page *page, unsigned offset, size_t bytes,
+                                struct iov_iter *i)
+{
+       size_t res = 0;
+
+       if (!page_copy_sane(page, offset, bytes))
+               return 0;
+       if (WARN_ON_ONCE(i->data_source))
+               return 0;
+       if (unlikely(iov_iter_is_pipe(i)))
+               return copy_page_to_iter_pipe(page, offset, bytes, i);
+       page += offset / PAGE_SIZE; // first subpage
+       offset %= PAGE_SIZE;
+       while (1) {
+               void *kaddr = kmap_local_page(page);
+               size_t n = min(bytes, (size_t)PAGE_SIZE - offset);
+
+               iterate_and_advance(i, n, base, len, off,
+                       copyout_nofault(base, kaddr + offset + off, len),
+                       memcpy(base, kaddr + offset + off, len)
+               )
+               kunmap_local(kaddr);
+               res += n;
+               bytes -= n;
+               if (!bytes || !n)
+                       break;
+               offset += n;
+               if (offset == PAGE_SIZE) {
+                       page++;
+                       offset = 0;
+               }
+       }
+       return res;
+}
+EXPORT_SYMBOL(copy_page_to_iter_nofault);
+
 size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes,
                         struct iov_iter *i)
 {