diff mbox series

[v3,2/9] iov_iter: Add a general purpose iteration function

Message ID 165364824973.3334034.10715738699511650662.stgit@warthog.procyon.org.uk (mailing list archive)
State New, archived
Headers show
Series cifs: Use iov_iters down to the network transport | expand

Commit Message

David Howells May 27, 2022, 10:44 a.m. UTC
Add a function, iov_iter_scan(), to iterate over the buffers described by
an I/O iterator, kmapping and passing each contiguous chunk 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      |   40 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+)

Comments

Al Viro May 27, 2022, 2:33 p.m. UTC | #1
On Fri, May 27, 2022 at 11:44:09AM +0100, David Howells wrote:
> Add a function, iov_iter_scan(), to iterate over the buffers described by
> an I/O iterator, kmapping and passing each contiguous chunk 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.

> +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)
> +{
> +	ssize_t ret = 0, scanned = 0;
> +
> +	if (!bytes)
> +		return 0;
> +	if (iter_is_iovec(i))
> +		might_fault();
> +
> +	iterate_and_advance(
> +		i, bytes, base, len, off, ({
> +				ret = scanner(i, base, len, off, priv);
> +				if (ret < 0)
> +					break;
> +				scanned += ret;
> +			}), ({
> +				ret = scanner(i, base, len, off, priv);
> +				if (ret < 0)
> +					break;
> +				scanned += ret;
> +			})
> +	);
> +	return ret < 0 ? ret : scanned;
> +}

Have you even tried to run sparse on that?  How could that possibly work?
You are feeding the same callback both userland and kernel pointers;
that makes no sense.

NAK.
diff mbox series

Patch

diff --git a/include/linux/uio.h b/include/linux/uio.h
index 5a3c6f296b96..8d89203064e7 100644
--- a/include/linux/uio.h
+++ b/include/linux/uio.h
@@ -244,6 +244,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 8db34ddd23be..834e1e268eb6 100644
--- a/lib/iov_iter.c
+++ b/lib/iov_iter.c
@@ -1789,6 +1789,46 @@  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 for each bit
+ * @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)
+{
+	ssize_t ret = 0, scanned = 0;
+
+	if (!bytes)
+		return 0;
+	if (iter_is_iovec(i))
+		might_fault();
+
+	iterate_and_advance(
+		i, bytes, base, len, off, ({
+				ret = scanner(i, base, len, off, priv);
+				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)
 {