This is a note to let you know that I've just added the patch titled iov_iter: add helper to save iov_iter state to the 5.10-stable tree which can be found at: http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary The filename of the patch is: iov_iter-add-helper-to-save-iov_iter-state.patch and it can be found in the queue-5.10 subdirectory. If you, or anyone else, feels it should not be added to the stable tree, please let <stable@xxxxxxxxxxxxxxx> know about it. >From 4d95aa07b6f1059990366f88112c48f217fbbc5d Mon Sep 17 00:00:00 2001 From: Jens Axboe <axboe@xxxxxxxxx> Date: Fri, 10 Sep 2021 11:18:36 -0600 Subject: iov_iter: add helper to save iov_iter state From: Jens Axboe <axboe@xxxxxxxxx> [ Upstream commit 8fb0f47a9d7acf620d0fd97831b69da9bc5e22ed ] In an ideal world, when someone is passed an iov_iter and returns X bytes, then X bytes would have been consumed/advanced from the iov_iter. But we have use cases that always consume the entire iterator, a few examples of that are iomap and bdev O_DIRECT. This means we cannot rely on the state of the iov_iter once we've called ->read_iter() or ->write_iter(). This would be easier if we didn't always have to deal with truncate of the iov_iter, as rewinding would be trivial without that. We recently added a commit to track the truncate state, but that grew the iov_iter by 8 bytes and wasn't the best solution. Implement a helper to save enough of the iov_iter state to sanely restore it after we've called the read/write iterator helpers. This currently only works for IOVEC/BVEC/KVEC as that's all we need, support for other iterator types are left as an exercise for the reader. Link: https://lore.kernel.org/linux-fsdevel/CAHk-=wiacKV4Gh-MYjteU0LwNBSGpWrK-Ov25HdqB1ewinrFPg@xxxxxxxxxxxxxx/ Signed-off-by: Jens Axboe <axboe@xxxxxxxxx> Signed-off-by: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx> --- include/linux/uio.h | 15 +++++++++++++++ lib/iov_iter.c | 52 +++++++++++++++++++++++++++++++++------------------- 2 files changed, 48 insertions(+), 19 deletions(-) --- a/include/linux/uio.h +++ b/include/linux/uio.h @@ -26,6 +26,12 @@ enum iter_type { ITER_DISCARD = 64, }; +struct iov_iter_state { + size_t iov_offset; + size_t count; + unsigned long nr_segs; +}; + struct iov_iter { /* * Bit 0 is the read/write bit, set if we're writing. @@ -55,6 +61,14 @@ static inline enum iter_type iov_iter_ty return i->type & ~(READ | WRITE); } +static inline void iov_iter_save_state(struct iov_iter *iter, + struct iov_iter_state *state) +{ + state->iov_offset = iter->iov_offset; + state->count = iter->count; + state->nr_segs = iter->nr_segs; +} + static inline bool iter_is_iovec(const struct iov_iter *i) { return iov_iter_type(i) == ITER_IOVEC; @@ -226,6 +240,7 @@ ssize_t iov_iter_get_pages(struct iov_it ssize_t iov_iter_get_pages_alloc(struct iov_iter *i, struct page ***pages, size_t maxsize, size_t *start); int iov_iter_npages(const struct iov_iter *i, int maxpages); +void iov_iter_restore(struct iov_iter *i, struct iov_iter_state *state); const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags); --- a/lib/iov_iter.c +++ b/lib/iov_iter.c @@ -1836,24 +1836,38 @@ int import_single_range(int rw, void __u } EXPORT_SYMBOL(import_single_range); -int iov_iter_for_each_range(struct iov_iter *i, size_t bytes, - int (*f)(struct kvec *vec, void *context), - void *context) +/** + * iov_iter_restore() - Restore a &struct iov_iter to the same state as when + * iov_iter_save_state() was called. + * + * @i: &struct iov_iter to restore + * @state: state to restore from + * + * Used after iov_iter_save_state() to bring restore @i, if operations may + * have advanced it. + * + * Note: only works on ITER_IOVEC, ITER_BVEC, and ITER_KVEC + */ +void iov_iter_restore(struct iov_iter *i, struct iov_iter_state *state) { - struct kvec w; - int err = -EINVAL; - if (!bytes) - return 0; - - iterate_all_kinds(i, bytes, v, -EINVAL, ({ - w.iov_base = kmap(v.bv_page) + v.bv_offset; - w.iov_len = v.bv_len; - err = f(&w, context); - kunmap(v.bv_page); - err;}), ({ - w = v; - err = f(&w, context);}) - ) - return err; + if (WARN_ON_ONCE(!iov_iter_is_bvec(i) && !iter_is_iovec(i)) && + !iov_iter_is_kvec(i)) + return; + i->iov_offset = state->iov_offset; + i->count = state->count; + /* + * For the *vec iters, nr_segs + iov is constant - if we increment + * the vec, then we also decrement the nr_segs count. Hence we don't + * need to track both of these, just one is enough and we can deduct + * the other from that. ITER_KVEC and ITER_IOVEC are the same struct + * size, so we can just increment the iov pointer as they are unionzed. + * ITER_BVEC _may_ be the same size on some archs, but on others it is + * not. Be safe and handle it separately. + */ + BUILD_BUG_ON(sizeof(struct iovec) != sizeof(struct kvec)); + if (iov_iter_is_bvec(i)) + i->bvec -= state->nr_segs - i->nr_segs; + else + i->iov -= state->nr_segs - i->nr_segs; + i->nr_segs = state->nr_segs; } -EXPORT_SYMBOL(iov_iter_for_each_range); Patches currently in stable-queue which might be from axboe@xxxxxxxxx are queue-5.10/x86-process-setup-io_threads-more-like-normal-user-space-threads.patch queue-5.10/powerpc-add-support-for-tif_notify_signal.patch queue-5.10/eventfd-provide-a-eventfd_signal_mask-helper.patch queue-5.10/fs-provide-locked-helper-variant-of-close_fd_get_file.patch queue-5.10/relay-fix-type-mismatch-when-allocating-memory-in-re.patch queue-5.10/eventfd-change-int-to-__u64-in-eventfd_signal-ifndef.patch queue-5.10/io_uring-pass-in-epoll_uring_wake-for-eventfd-signaling-and-wakeups.patch queue-5.10/blk-mq-fix-possible-memleak-when-register-hctx-faile.patch queue-5.10/fix-handling-of-nd-depth-on-lookup_cached-failures-in-try_to_unlazy.patch queue-5.10/net-provide-__sys_shutdown_sock-that-takes-a-socket.patch queue-5.10/task_work-unconditionally-run-task_work-from-get_signal.patch queue-5.10/openrisc-add-support-for-tif_notify_signal.patch queue-5.10/signal-add-task_sigpending-helper.patch queue-5.10/net-remove-cmsg-restriction-from-io_uring-based-send-recvmsg-calls.patch queue-5.10/alpha-add-support-for-tif_notify_signal.patch queue-5.10/nios32-add-support-for-tif_notify_signal.patch queue-5.10/ia64-don-t-call-handle_signal-unless-there-s-actually-a-signal-queued.patch queue-5.10/task_work-remove-legacy-twa_signal-path.patch queue-5.10/revert-proc-don-t-allow-async-path-resolution-of-proc-self-components.patch queue-5.10/m68k-add-support-for-tif_notify_signal.patch queue-5.10/s390-add-support-for-tif_notify_signal.patch queue-5.10/um-add-support-for-tif_notify_signal.patch queue-5.10/tools-headers-uapi-sync-openat2.h-with-the-kernel-sources.patch queue-5.10/kernel-provide-create_io_thread-helper.patch queue-5.10/iov_iter-add-helper-to-save-iov_iter-state.patch queue-5.10/arc-unbork-5.11-bootup-fix-snafu-in-_tif_notify_signal-handling.patch queue-5.10/arch-ensure-parisc-powerpc-handle-pf_io_worker-in-copy_thread.patch queue-5.10/csky-add-support-for-tif_notify_signal.patch queue-5.10/arm-add-support-for-tif_notify_signal.patch queue-5.10/kernel-stop-masking-signals-in-create_io_thread.patch queue-5.10/fs-expose-lookup_cached-through-openat2-resolve_cached.patch queue-5.10/task_work-add-helper-for-more-targeted-task_work-canceling.patch queue-5.10/nds32-add-support-for-tif_notify_signal.patch queue-5.10/signal-kill-jobctl_task_work.patch queue-5.10/hexagon-add-support-for-tif_notify_signal.patch queue-5.10/sh-add-support-for-tif_notify_signal.patch queue-5.10/riscv-add-support-for-tif_notify_signal.patch queue-5.10/h8300-add-support-for-tif_notify_signal.patch queue-5.10/io_uring-import-5.15-stable-io_uring.patch queue-5.10/sparc-add-support-for-tif_notify_signal.patch queue-5.10/blktrace-fix-output-non-blktrace-event-when-blk_clas.patch queue-5.10/eventpoll-add-epoll_uring_wake-poll-wakeup-flag.patch queue-5.10/parisc-add-support-for-tif_notify_signal.patch queue-5.10/entry-add-support-for-tif_notify_signal.patch queue-5.10/x86-wire-up-tif_notify_signal.patch queue-5.10/task_work-use-tif_notify_signal-if-available.patch queue-5.10/drbd-fix-an-invalid-memory-access-caused-by-incorrec.patch queue-5.10/kernel-don-t-call-do_exit-for-pf_io_worker-threads.patch queue-5.10/kernel-allow-fork-with-tif_notify_signal-pending.patch queue-5.10/pata_ipx4xx_cf-fix-unsigned-comparison-with-less-tha.patch queue-5.10/mips-add-support-for-tif_notify_signal.patch queue-5.10/xtensa-add-support-for-tif_notify_signal.patch queue-5.10/c6x-add-support-for-tif_notify_signal.patch queue-5.10/microblaze-add-support-for-tif_notify_signal.patch queue-5.10/net-add-accept-helper-not-installing-fd.patch queue-5.10/ia64-add-support-for-tif_notify_signal.patch queue-5.10/arm64-add-support-for-tif_notify_signal.patch queue-5.10/arc-add-support-for-tif_notify_signal.patch queue-5.10/revert-proc-don-t-allow-async-path-resolution-of-proc-thread-self-components.patch queue-5.10/fs-make-do_renameat2-take-struct-filename.patch queue-5.10/kernel-remove-checking-for-tif_notify_signal.patch queue-5.10/arch-setup-pf_io_worker-threads-like-pf_kthread.patch queue-5.10/nvme-pci-fix-mempool-alloc-size.patch queue-5.10/fs-add-support-for-lookup_cached.patch