All of lore.kernel.org
 help / color / mirror / Atom feed
From: David Howells <dhowells@redhat.com>
To: Steve French <smfrench@gmail.com>, Al Viro <viro@zeniv.linux.org.uk>
Cc: dhowells@redhat.com, Shyam Prasad N <nspmangalore@gmail.com>,
	Rohith Surabattula <rohiths.msft@gmail.com>,
	Jeff Layton <jlayton@kernel.org>,
	linux-cifs@vger.kernel.org, linux-fsdevel@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: [PATCH 2/7] iov_iter: Add a general purpose iteration function
Date: Tue, 23 Aug 2022 15:12:21 +0100	[thread overview]
Message-ID: <166126394098.708021.10931745751914856461.stgit@warthog.procyon.org.uk> (raw)
In-Reply-To: <166126392703.708021.14465850073772688008.stgit@warthog.procyon.org.uk>

Add a function, iov_iter_scan(), to iterate over the buffers described by
an I/O iterator, kmapping and passing each contiguous chunk the the
supplied scanner function in turn, up to the requested amount of data or
until the scanner function returns an error.

This can be used, for example, to hash all the data in an iterator by
having the scanner function call the appropriate crypto update function.

Signed-off-by: David Howells <dhowells@redhat.com>
---

 include/linux/uio.h |    4 +++
 lib/iov_iter.c      |   66 +++++++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 67 insertions(+), 3 deletions(-)

diff --git a/include/linux/uio.h b/include/linux/uio.h
index 88fd93508710..76a3aeca8703 100644
--- a/include/linux/uio.h
+++ b/include/linux/uio.h
@@ -259,6 +259,10 @@ 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);
+ssize_t iov_iter_scan(struct iov_iter *i, size_t bytes,
+		      ssize_t (*scanner)(struct iov_iter *i, const void *p,
+					 size_t len, size_t off, void *priv),
+		      void *priv);
 
 static inline size_t iov_iter_count(const struct iov_iter *i)
 {
diff --git a/lib/iov_iter.c b/lib/iov_iter.c
index c07bf978b935..3f22822a946c 100644
--- a/lib/iov_iter.c
+++ b/lib/iov_iter.c
@@ -21,9 +21,11 @@
 	size_t __maybe_unused off = 0;				\
 	len = n;						\
 	base = __p + i->iov_offset;				\
-	len -= (STEP);						\
-	i->iov_offset += len;					\
-	n = len;						\
+	do {							\
+		len -= (STEP);					\
+		i->iov_offset += len;				\
+		n = len;					\
+	} while (0);						\
 }
 
 /* covers iovec and kvec alike */
@@ -1611,6 +1613,64 @@ ssize_t extract_iter_to_iter(struct iov_iter *orig,
 }
 EXPORT_SYMBOL(extract_iter_to_iter);
 
+/**
+ * iov_iter_scan - Scan a source iter
+ * @i: The iterator to scan
+ * @bytes: The amount of buffer/data to scan
+ * @scanner: The function to call to process each segment
+ * @priv: Private data to pass to the scanner function
+ *
+ * Scan an iterator, passing each segment to the scanner function.  If the
+ * scanner returns an error at any time, scanning stops and the error is
+ * returned, otherwise the sum of the scanner results is returned.
+ */
+ssize_t iov_iter_scan(struct iov_iter *i, size_t bytes,
+		      ssize_t (*scanner)(struct iov_iter *i, const void *p,
+					 size_t len, size_t off, void *priv),
+		      void *priv)
+{
+	unsigned int gup_flags = 0;
+	ssize_t ret = 0, scanned = 0;
+
+	if (!bytes)
+		return 0;
+	if (WARN_ON(iov_iter_is_discard(i)))
+		return 0;
+	if (iter_is_iovec(i))
+		might_fault();
+
+	if (iov_iter_rw(i) != WRITE)
+		gup_flags |= FOLL_WRITE;
+	if (i->nofault)
+		gup_flags |= FOLL_NOFAULT;
+
+	iterate_and_advance(
+		i, bytes, base, len, off, ({
+				struct page *page;
+				void *q;
+
+				ret = get_user_pages_fast((unsigned long)base, 1,
+							  gup_flags, &page);
+				if (ret < 0)
+					break;
+				q = kmap_local_page(page);
+				ret = scanner(i, q, len, off, priv);
+				kunmap_local(q);
+				put_page(page);
+				if (ret < 0)
+					break;
+				scanned += ret;
+			}), ({
+				ret = scanner(i, base, len, off, priv);
+				if (ret < 0)
+					break;
+				scanned += ret;
+			})
+	);
+	return ret < 0 ? ret : scanned;
+}
+EXPORT_SYMBOL(iov_iter_scan);
+
 size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum,
 			       struct iov_iter *i)
 {



  parent reply	other threads:[~2022-08-23 17:01 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-23 14:12 [PATCH 0/7] smb3: Add iter helpers and use iov_iters down to the network transport David Howells
2022-08-23 14:12 ` [PATCH 1/7] iov_iter: Add a function to extract an iter's buffers to a bvec iter David Howells
2022-08-24  3:52   ` Christoph Hellwig
2022-08-26 21:21   ` Al Viro
2022-10-14 14:06   ` David Howells
2022-08-23 14:12 ` David Howells [this message]
2022-08-26 21:35   ` [PATCH 2/7] iov_iter: Add a general purpose iteration function Al Viro
2022-08-23 14:12 ` [PATCH 3/7] cifs: Add some helper functions David Howells
2022-08-23 14:12 ` [PATCH 4/7] cifs: Add a function to read into an iter from a socket David Howells
2022-09-24  2:50   ` Al Viro
2022-08-23 14:12 ` [PATCH 5/7] cifs: Change the I/O paths to use an iterator rather than a page list David Howells
2022-09-24  4:34   ` Al Viro
2022-08-23 14:12 ` [PATCH 6/7] cifs: Remove unused code David Howells
2022-08-23 14:12 ` [PATCH 7/7] cifs: Add some RDMA send tracepoints David Howells
  -- strict thread matches above, loose matches on Subject: below --
2022-05-25 14:26 [PATCH 0/7] cifs: Use iov_iters down to the network transport David Howells
2022-05-25 14:26 ` [PATCH 2/7] iov_iter: Add a general purpose iteration function David Howells

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=166126394098.708021.10931745751914856461.stgit@warthog.procyon.org.uk \
    --to=dhowells@redhat.com \
    --cc=jlayton@kernel.org \
    --cc=linux-cifs@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nspmangalore@gmail.com \
    --cc=rohiths.msft@gmail.com \
    --cc=smfrench@gmail.com \
    --cc=viro@zeniv.linux.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.