Skip to contentSkip to conversation

iov_iter: Add composite, scatterlist and skbuff iterator types

RFC PATCH In review David Howells 4 participants 9 messages Mar 21, 2025 – Mar 23, 2025

Subsystems (6)
lore
Share & tools
lore

David Howells

author maintainer
#0 lore raw

cover iov_iter: Add composite, scatterlist and skbuff iterator types

Hi Leon,

Here are some patches that illustrate some of what I'm thinking of doing to iov iterators. Note that they are incomplete as I won't have time to finish or test them before LSF, but I thought I'd post them for use as a discussion point.

So the first thing I want to do is to move certain iterators out of line from the main inline iteration multiplexor. This code gets relentlessly duplicated and adding further iterator types expands a whole load of places. So the DISCARD iterator (which is just a simple short circuit) and the XARRAY iterator (which is obsolete) move out of line.

Then I want to add three more types, for now:

(1) ITER_ITERLIST. A compound iterator that takes an array of iterators of disparate types. The aim here is to make it possible to fabricate an network message in one go (say an RPC call) and pass it to a socket without the need for corking.

(2) ITER_SCATTERLIST. An iterator that takes a scatterlist. This can be used to act as a bridge in converting interfaces that currently take a scatterlist (e.g. crypto). It requires extra fields adding to the iov_iter struct because chained scatterlists do not have have a rewind capability and so iov_iter_revert() must go back to the beginning and fast-forward.

(3) ITER_SKBUFF. An iterator that takes a network buffer. The aim here is to render skb_to_sgvec() unnecessary for doing crypto operations.

The patches can also be found here:

https://git.kernel.org/pub/scm/linux/kernel/git/dhowells/linux-fs.git/log/?h=iov-experimental

David

David Howells (4):
iov_iter: Move ITER_DISCARD and ITER_XARRAY iteration out-of-line
iov_iter: Add an iterator-of-iterators
iov_iter: Add a scatterlist iterator type
iov_iter: Add a scatterlist iterator type [INCOMPLETE]

 include/linux/iov_iter.h |  77 +----
 include/linux/uio.h      |  37 +++
 lib/iov_iter.c           | 675 ++++++++++++++++++++++++++++++++++++++-
 3 files changed, 710 insertions(+), 79 deletions(-)
author maintainer
#1 lore raw

RFC PATCH 1/4 iov_iter: Move ITER_DISCARD and ITER_XARRAY iteration out-of-line

Move ITER_DISCARD and ITER_XARRAY iteration out-of-line in preparation of adding other iteration types which will also be out-of-line.

---
 include/linux/iov_iter.h | 77 +++-----------------------------------
 lib/iov_iter.c           | 81 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 86 insertions(+), 72 deletions(-)
include/linux/iov_iter.h +5 -72
index c4aa58032faf..0c47933df517 100644
@@ -17,6 +17,9 @@ typedef size_t (*iov_step_f)(void *iter_base, size_t progress, size_t len,
 typedef size_t (*iov_ustep_f)(void __user *iter_base, size_t progress, size_t len,
 			      void *priv, void *priv2);
 
+size_t __iterate_and_advance2(struct iov_iter *iter, size_t len, void *priv,
+			      void *priv2, iov_ustep_f ustep, iov_step_f step);
+
 /*
  * Handle ITER_UBUF.
  */
@@ -195,72 +198,6 @@ size_t iterate_folioq(struct iov_iter *iter, size_t len, void *priv, void *priv2
 	return progress;
 }
 
-/*
- * Handle ITER_XARRAY.
- */
-static __always_inline
-size_t iterate_xarray(struct iov_iter *iter, size_t len, void *priv, void *priv2,
-		      iov_step_f step)
-{
-	struct folio *folio;
-	size_t progress = 0;
-	loff_t start = iter->xarray_start + iter->iov_offset;
-	pgoff_t index = start / PAGE_SIZE;
-	XA_STATE(xas, iter->xarray, index);
-
-	rcu_read_lock();
-	xas_for_each(&xas, folio, ULONG_MAX) {
-		size_t remain, consumed, offset, part, flen;
-
-		if (xas_retry(&xas, folio))
-			continue;
-		if (WARN_ON(xa_is_value(folio)))
-			break;
-		if (WARN_ON(folio_test_hugetlb(folio)))
-			break;
-
-		offset = offset_in_folio(folio, start + progress);
-		flen = min(folio_size(folio) - offset, len);
-
-		while (flen) {
-			void *base = kmap_local_folio(folio, offset);
-
-			part = min_t(size_t, flen,
-				     PAGE_SIZE - offset_in_page(offset));
-			remain = step(base, progress, part, priv, priv2);
-			kunmap_local(base);
-
-			consumed = part - remain;
-			progress += consumed;
-			len -= consumed;
-
-			if (remain || len == 0)
-				goto out;
-			flen -= consumed;
-			offset += consumed;
-		}
-	}
-
-out:
-	rcu_read_unlock();
-	iter->iov_offset += progress;
-	iter->count -= progress;
-	return progress;
-}
-
-/*
- * Handle ITER_DISCARD.
- */
-static __always_inline
-size_t iterate_discard(struct iov_iter *iter, size_t len, void *priv, void *priv2,
-		      iov_step_f step)
-{
-	size_t progress = len;
-
-	iter->count -= progress;
-	return progress;
-}
-
 /**
  * iterate_and_advance2 - Iterate over an iterator
  * @iter: The iterator to iterate over.
@@ -306,9 +243,7 @@ size_t iterate_and_advance2(struct iov_iter *iter, size_t len, void *priv,
 		return iterate_kvec(iter, len, priv, priv2, step);
 	if (iov_iter_is_folioq(iter))
 		return iterate_folioq(iter, len, priv, priv2, step);
-	if (iov_iter_is_xarray(iter))
-		return iterate_xarray(iter, len, priv, priv2, step);
-	return iterate_discard(iter, len, priv, priv2, step);
+	return __iterate_and_advance2(iter, len, priv, priv2, ustep, step);
 }
 
 /**
@@ -370,9 +305,7 @@ size_t iterate_and_advance_kernel(struct iov_iter *iter, size_t len, void *priv,
 		return iterate_kvec(iter, len, priv, priv2, step);
 	if (iov_iter_is_folioq(iter))
 		return iterate_folioq(iter, len, priv, priv2, step);
-	if (iov_iter_is_xarray(iter))
-		return iterate_xarray(iter, len, priv, priv2, step);
-	return iterate_discard(iter, len, priv, priv2, step);
+	return __iterate_and_advance2(iter, len, priv, priv2, NULL, step);
 }
 
 #endif /* _LINUX_IOV_ITER_H */
lib/iov_iter.c +81 -0

84 lines not on this page · raw message

author maintainer
#2 lore raw

RFC PATCH 2/4 iov_iter: Add an iterator-of-iterators

In reply to David Howells

Add a new I/O iterator type, ITER_ITERLIST, that allows iteration over a series of I/O iterators, provided the iterators are all the same direction (all ITER_SOURCE or all ITER_DEST) and none of them are themselves ITER_ITERLIST (this function is recursive).

To make reversion possible, I've added an 'orig_count' member into the iov_iter struct so that reversion of an ITER_ITERLIST can know when to go move backwards through the iter list. It might make more sense to make the iterator list element, say:

	struct itervec {
		struct iov_iter iter;
		size_t orig_count;
	};

rather than expanding struct iov_iter itself and have iov_iter_iterlist() set vec[i].orig_count from vec[i].iter->count.

Also, for the moment, I've only permitted its use with source iterators (eg. sendmsg).

To use this, you allocate an array of iterators and point the list iterator at it, e.g.:

	struct iov_iter iters[3];
	struct msghdr msg;

	iov_iter_bvec(&iters[0], ITER_SOURCE, &head_bv, 1,
		      sizeof(marker) + head->iov_len);
	iov_iter_xarray(&iters[1], ITER_SOURCE, xdr->pages,
			xdr->page_fpos, xdr->page_len);
	iov_iter_kvec(&iters[2], ITER_SOURCE, &tail_kv, 1,
		      tail->iov_len);
	iov_iter_iterlist(&msg.msg_iter, ITER_SOURCE, iters, 3, size);

This can be used by network filesystem protocols, such as sunrpc, to glue a header and a trailer on to some data to form a message and then dump the entire message onto the socket in a single go.

[!] Note: I'm not entirely sure that this is a good idea: the problem is
that it's reasonably common practice to copy an iterator by direct assignment - and that works for the existing iterators... but not this one. With the iterator-of-iterators, the list of iterators has to be modified if we recurse. It's probably fine just for calling sendmsg() from network filesystems, but I'm not 100% sure of that.

---
 include/linux/uio.h |  15 +++++
 lib/iov_iter.c      | 158 +++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 172 insertions(+), 1 deletion(-)
include/linux/uio.h +15 -0
index 8ada84e85447..59a586333e1b 100644
@@ -29,6 +29,7 @@ enum iter_type {
 	ITER_FOLIOQ,
 	ITER_XARRAY,
 	ITER_DISCARD,
+	ITER_ITERLIST,
 };
 
 #define ITER_SOURCE	1	// == WRITE
@@ -71,6 +72,7 @@ struct iov_iter {
 				const struct folio_queue *folioq;
 				struct xarray *xarray;
 				void __user *ubuf;
+				struct iov_iterlist *iterlist;
 			};
 			size_t count;
 		};
@@ -82,6 +84,11 @@ struct iov_iter {
 	};
 };
 
+struct iov_iterlist {
+	struct iov_iter	iter;
+	size_t		orig_count;
+};
+
 typedef __u16 uio_meta_flags_t;
 
 struct uio_meta {
@@ -149,6 +156,11 @@ static inline bool iov_iter_is_xarray(const struct iov_iter *i)
 	return iov_iter_type(i) == ITER_XARRAY;
 }
 
+static inline bool iov_iter_is_iterlist(const struct iov_iter *i)
+{
+	return iov_iter_type(i) == ITER_ITERLIST;
+}
+
 static inline unsigned char iov_iter_rw(const struct iov_iter *i)
 {
 	return i->data_source ? WRITE : READ;
@@ -302,6 +314,9 @@ void iov_iter_folio_queue(struct iov_iter *i, unsigned int direction,
 			  unsigned int first_slot, unsigned int offset, size_t count);
 void iov_iter_xarray(struct iov_iter *i, unsigned int direction, struct xarray *xarray,
 		     loff_t start, size_t count);
+void iov_iter_iterlist(struct iov_iter *i, unsigned int direction,
+		       struct iov_iterlist *iterlist, unsigned long nr_segs,
+		       size_t count);
 ssize_t iov_iter_get_pages2(struct iov_iter *i, struct page **pages,
 			size_t maxsize, unsigned maxpages, size_t *start);
 ssize_t iov_iter_get_pages_alloc2(struct iov_iter *i, struct page ***pages,
lib/iov_iter.c +157 -1

240 lines not on this page · raw message

author maintainer
#3 lore raw

RFC PATCH 3/4 iov_iter: Add a scatterlist iterator type

In reply to David Howells

Add an iterator type that can iterate over a scatterlist. This can be used as a bridge to help convert things that take scatterlists into things that take I/O iterators.

---
 include/linux/uio.h |  12 ++
 lib/iov_iter.c      | 315 +++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 321 insertions(+), 6 deletions(-)
include/linux/uio.h +12 -0
index 59a586333e1b..0e50f4af6877 100644
@@ -12,6 +12,7 @@
 
 struct page;
 struct folio_queue;
+struct scatterlist;
 
 typedef unsigned int __bitwise iov_iter_extraction_t;
 
@@ -30,6 +31,7 @@ enum iter_type {
 	ITER_XARRAY,
 	ITER_DISCARD,
 	ITER_ITERLIST,
+	ITER_SCATTERLIST,
 };
 
 #define ITER_SOURCE	1	// == WRITE
@@ -46,6 +48,7 @@ struct iov_iter {
 	bool nofault;
 	bool data_source;
 	size_t iov_offset;
+	size_t orig_count;
 	/*
 	 * Hack alert: overlay ubuf_iovec with iovec + count, so
 	 * that the members resolve correctly regardless of the type
@@ -73,11 +76,13 @@ struct iov_iter {
 				struct xarray *xarray;
 				void __user *ubuf;
 				struct iov_iterlist *iterlist;
+				struct scatterlist *sglist;
 			};
 			size_t count;
 		};
 	};
 	union {
+		struct scatterlist *sglist_head;
 		unsigned long nr_segs;
 		u8 folioq_slot;
 		loff_t xarray_start;
@@ -161,6 +166,11 @@ static inline bool iov_iter_is_iterlist(const struct iov_iter *i)
 	return iov_iter_type(i) == ITER_ITERLIST;
 }
 
+static inline bool iov_iter_is_scatterlist(const struct iov_iter *i)
+{
+	return iov_iter_type(i) == ITER_SCATTERLIST;
+}
+
 static inline unsigned char iov_iter_rw(const struct iov_iter *i)
 {
 	return i->data_source ? WRITE : READ;
@@ -317,6 +327,8 @@ void iov_iter_xarray(struct iov_iter *i, unsigned int direction, struct xarray *
 void iov_iter_iterlist(struct iov_iter *i, unsigned int direction,
 		       struct iov_iterlist *iterlist, unsigned long nr_segs,
 		       size_t count);
+void iov_iter_scatterlist(struct iov_iter *i, unsigned int direction,
+			  struct scatterlist *sglist, size_t count);
 ssize_t iov_iter_get_pages2(struct iov_iter *i, struct page **pages,
 			size_t maxsize, unsigned maxpages, size_t *start);
 ssize_t iov_iter_get_pages_alloc2(struct iov_iter *i, struct page ***pages,
lib/iov_iter.c +309 -6

424 lines not on this page · raw message

author maintainer
#4 lore raw

RFC PATCH 4/4 iov_iter: Add a scatterlist iterator type [INCOMPLETE]

In reply to David Howells

Add an iterator type that can iterate over a socket buffer.

[!] Note this is not yet completely implemented and won't compile.

---
 include/linux/uio.h |  10 ++++
 lib/iov_iter.c      | 121 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 131 insertions(+)
include/linux/uio.h +10 -0
index 0e50f4af6877..87d6ba660489 100644
@@ -13,6 +13,7 @@
 struct page;
 struct folio_queue;
 struct scatterlist;
+struct sk_buff;
 
 typedef unsigned int __bitwise iov_iter_extraction_t;
 
@@ -32,6 +33,7 @@ enum iter_type {
 	ITER_DISCARD,
 	ITER_ITERLIST,
 	ITER_SCATTERLIST,
+	ITER_SKBUFF,
 };
 
 #define ITER_SOURCE	1	// == WRITE
@@ -77,6 +79,7 @@ struct iov_iter {
 				void __user *ubuf;
 				struct iov_iterlist *iterlist;
 				struct scatterlist *sglist;
+				const struct sk_buff *skb;
 			};
 			size_t count;
 		};
@@ -171,6 +174,11 @@ static inline bool iov_iter_is_scatterlist(const struct iov_iter *i)
 	return iov_iter_type(i) == ITER_SCATTERLIST;
 }
 
+static inline bool iov_iter_is_skbuff(const struct iov_iter *i)
+{
+	return iov_iter_type(i) == ITER_SKBUFF;
+}
+
 static inline unsigned char iov_iter_rw(const struct iov_iter *i)
 {
 	return i->data_source ? WRITE : READ;
@@ -329,6 +337,8 @@ void iov_iter_iterlist(struct iov_iter *i, unsigned int direction,
 		       size_t count);
 void iov_iter_scatterlist(struct iov_iter *i, unsigned int direction,
 			  struct scatterlist *sglist, size_t count);
+void iov_iter_skbuff(struct iov_iter *i, unsigned int direction,
+		     const struct sk_buff *skb, size_t count);
 ssize_t iov_iter_get_pages2(struct iov_iter *i, struct page **pages,
 			size_t maxsize, unsigned maxpages, size_t *start);
 ssize_t iov_iter_get_pages_alloc2(struct iov_iter *i, struct page ***pages,
lib/iov_iter.c +121 -0

151 lines not on this page · raw message

#5 lore raw
In reply to David Howells

This is going entirely in the wrong direction. We don't need more iter types but less. The reason why we have to many is because the underlying representation of the ranges is a mess which goes deeper than just the iterator, because it also means we have to convert between the underlying representations all the time.

E.g. the socket code should have (and either has for a while or at least there were patches) been using bio_vecs instead of reinventing them as sk fragment. The crypto code should not be using scatterlists, which are a horrible data structure because they mix up the physical memory description and the dma mapping information which isn't even used for most uses, etc.

So instead of more iters let's convert everyone to a common scatter/gather memory definition, which simplifies the iters. For now that is the bio_vec, which really should be converted from storing a struct page to a phys_addr_t (and maybe renamed if that helps adoption). That allows to trivially kill the kvec for example.

As for the head/tail - that seems to be a odd NFS/sunrpc fetish. I've actually started a little project to just convert the sunrpc code to use bio_vecs, which massively simplifies the code, and allows directly passing it to the iters in the socket API. It doesn't quite work yet but shows how all these custom (and in this case rather ad-hoc) memory fragment representation cause a huge mess.

I don't think the iterlist can work in practice, but it would be nice to have for a few use cases. If it worked it should hopefully allow to kill off the odd xarray iterator.

#6 lore raw
Quoted 26 lines · Christoph Hellwig

This is going entirely in the wrong direction. We don't need more iter types but less. The reason why we have to many is because the underlying representation of the ranges is a mess which goes deeper than just the iterator, because it also means we have to convert between the underlying representations all the time.

E.g. the socket code should have (and either has for a while or at least there were patches) been using bio_vecs instead of reinventing them as sk fragment. The crypto code should not be using scatterlists, which are a horrible data structure because they mix up the physical memory description and the dma mapping information which isn't even used for most uses, etc.

So instead of more iters let's convert everyone to a common scatter/gather memory definition, which simplifies the iters. For now that is the bio_vec, which really should be converted from storing a struct page to a phys_addr_t (and maybe renamed if that helps adoption). That allows to trivially kill the kvec for example.

As for the head/tail - that seems to be a odd NFS/sunrpc fetish. I've actually started a little project to just convert the sunrpc code to use bio_vecs, which massively simplifies the code, and allows directly passing it to the iters in the socket API. It doesn't quite work yet but shows how all these custom (and in this case rather ad-hoc) memory fragment representation cause a huge mess.

Quoted 4 lines

I don't think the iterlist can work in practice, but it would be nice to have for a few use cases. If it worked it should hopefully allow to kill off the odd xarray iterator.

Can we have a session around this?
IE define how iterators should be used, and what the iterator elements
should be. If we do it properly this will also fix the frozen page discussion we're having; if we define iterators whose data elements are _not_ pages then clearly one cannot take a reference to them.

But in either case, we should define the long-term goal such that people can start converting stuff.

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke                  Kernel Storage Architect
[email protected]                                +49 911 74053 688
SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg
HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich
#7 lore raw
Quoted 1 line · Hannes Reinecke

Can we have a session around this?

Wednesday, 10:30

#8 lore raw
In reply to Christoph Hellwig
Quoted 9 lines · Christoph Hellwig

This is going entirely in the wrong direction. We don't need more iter types but less. The reason why we have to many is because the underlying representation of the ranges is a mess which goes deeper than just the iterator, because it also means we have to convert between the underlying representations all the time.

E.g. the socket code should have (and either has for a while or at least there were patches) been using bio_vecs instead of reinventing them as sk fragment. The crypto code should not be using scatterlists, which are a

I did this work six years ago -- see 8842d285bafa

Unfortunately, networking is full of inconsiderate arseholes who backed it out without even talking to me in 21d2e6737c97