linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] iov_iter: streamline iovec/bvec alignment iteration
@ 2024-01-23 22:24 Jens Axboe
  2024-01-24 15:22 ` Keith Busch
  2024-01-25 16:24 ` Christian Brauner
  0 siblings, 2 replies; 3+ messages in thread
From: Jens Axboe @ 2024-01-23 22:24 UTC (permalink / raw)
  To: LKML; +Cc: Alexander Viro, Christian Brauner, Keith Busch

Rewrite the alignment checking iterators for iovec and bvec to be easier
to read, and also significantly more compact in terms of generated code.
This saves 270 bytes of text on x86-64 for me (with clang-18) and 224
bytes on arm64 (with gcc-13).

In profiles, also saves a bit of time as well for the same workload:

     0.81%     -0.18%  [kernel.vmlinux]  [k] iov_iter_aligned_bvec
     0.48%     -0.09%  [kernel.vmlinux]  [k] iov_iter_is_aligned

which is a nice side benefit as well.

Signed-off-by: Jens Axboe <axboe@kernel.dk>

---

v2: do the other half of the iterators too, as suggested by Keith.
    This further saves some text.

diff --git a/lib/iov_iter.c b/lib/iov_iter.c
index e0aa6b440ca5..15f5040709c3 100644
--- a/lib/iov_iter.c
+++ b/lib/iov_iter.c
@@ -714,12 +714,11 @@ EXPORT_SYMBOL(iov_iter_discard);
 static bool iov_iter_aligned_iovec(const struct iov_iter *i, unsigned addr_mask,
 				   unsigned len_mask)
 {
+	const struct iovec *iov = iter_iov(i);
 	size_t size = i->count;
 	size_t skip = i->iov_offset;
-	unsigned k;
 
-	for (k = 0; k < i->nr_segs; k++, skip = 0) {
-		const struct iovec *iov = iter_iov(i) + k;
+	do {
 		size_t len = iov->iov_len - skip;
 
 		if (len > size)
@@ -729,34 +728,36 @@ static bool iov_iter_aligned_iovec(const struct iov_iter *i, unsigned addr_mask,
 		if ((unsigned long)(iov->iov_base + skip) & addr_mask)
 			return false;
 
+		iov++;
 		size -= len;
-		if (!size)
-			break;
-	}
+		skip = 0;
+	} while (size);
+
 	return true;
 }
 
 static bool iov_iter_aligned_bvec(const struct iov_iter *i, unsigned addr_mask,
 				  unsigned len_mask)
 {
-	size_t size = i->count;
+	const struct bio_vec *bvec = i->bvec;
 	unsigned skip = i->iov_offset;
-	unsigned k;
+	size_t size = i->count;
 
-	for (k = 0; k < i->nr_segs; k++, skip = 0) {
-		size_t len = i->bvec[k].bv_len - skip;
+	do {
+		size_t len = bvec->bv_len;
 
 		if (len > size)
 			len = size;
 		if (len & len_mask)
 			return false;
-		if ((unsigned long)(i->bvec[k].bv_offset + skip) & addr_mask)
+		if ((unsigned long)(bvec->bv_offset + skip) & addr_mask)
 			return false;
 
+		bvec++;
 		size -= len;
-		if (!size)
-			break;
-	}
+		skip = 0;
+	} while (size);
+
 	return true;
 }
 
@@ -800,13 +801,12 @@ EXPORT_SYMBOL_GPL(iov_iter_is_aligned);
 
 static unsigned long iov_iter_alignment_iovec(const struct iov_iter *i)
 {
+	const struct iovec *iov = iter_iov(i);
 	unsigned long res = 0;
 	size_t size = i->count;
 	size_t skip = i->iov_offset;
-	unsigned k;
 
-	for (k = 0; k < i->nr_segs; k++, skip = 0) {
-		const struct iovec *iov = iter_iov(i) + k;
+	do {
 		size_t len = iov->iov_len - skip;
 		if (len) {
 			res |= (unsigned long)iov->iov_base + skip;
@@ -814,30 +814,31 @@ static unsigned long iov_iter_alignment_iovec(const struct iov_iter *i)
 				len = size;
 			res |= len;
 			size -= len;
-			if (!size)
-				break;
 		}
-	}
+		iov++;
+		skip = 0;
+	} while (size);
 	return res;
 }
 
 static unsigned long iov_iter_alignment_bvec(const struct iov_iter *i)
 {
+	const struct bio_vec *bvec = i->bvec;
 	unsigned res = 0;
 	size_t size = i->count;
 	unsigned skip = i->iov_offset;
-	unsigned k;
 
-	for (k = 0; k < i->nr_segs; k++, skip = 0) {
-		size_t len = i->bvec[k].bv_len - skip;
-		res |= (unsigned long)i->bvec[k].bv_offset + skip;
+	do {
+		size_t len = bvec->bv_len - skip;
+		res |= (unsigned long)bvec->bv_offset + skip;
 		if (len > size)
 			len = size;
 		res |= len;
+		bvec++;
 		size -= len;
-		if (!size)
-			break;
-	}
+		skip = 0;
+	} while (size);
+
 	return res;
 }
 
-- 
Jens Axboe


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] iov_iter: streamline iovec/bvec alignment iteration
  2024-01-23 22:24 [PATCH v2] iov_iter: streamline iovec/bvec alignment iteration Jens Axboe
@ 2024-01-24 15:22 ` Keith Busch
  2024-01-25 16:24 ` Christian Brauner
  1 sibling, 0 replies; 3+ messages in thread
From: Keith Busch @ 2024-01-24 15:22 UTC (permalink / raw)
  To: Jens Axboe; +Cc: LKML, Alexander Viro, Christian Brauner

On Tue, Jan 23, 2024 at 03:24:46PM -0700, Jens Axboe wrote:
> Rewrite the alignment checking iterators for iovec and bvec to be easier
> to read, and also significantly more compact in terms of generated code.
> This saves 270 bytes of text on x86-64 for me (with clang-18) and 224
> bytes on arm64 (with gcc-13).
> 
> In profiles, also saves a bit of time as well for the same workload:
> 
>      0.81%     -0.18%  [kernel.vmlinux]  [k] iov_iter_aligned_bvec
>      0.48%     -0.09%  [kernel.vmlinux]  [k] iov_iter_is_aligned
> 
> which is a nice side benefit as well.

Looks good

Reviewed-by: Keith Busch <kbusch@kernel.org>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] iov_iter: streamline iovec/bvec alignment iteration
  2024-01-23 22:24 [PATCH v2] iov_iter: streamline iovec/bvec alignment iteration Jens Axboe
  2024-01-24 15:22 ` Keith Busch
@ 2024-01-25 16:24 ` Christian Brauner
  1 sibling, 0 replies; 3+ messages in thread
From: Christian Brauner @ 2024-01-25 16:24 UTC (permalink / raw)
  To: Jens Axboe; +Cc: Christian Brauner, Alexander Viro, Keith Busch, LKML

On Tue, 23 Jan 2024 15:24:46 -0700, Jens Axboe wrote:
> Rewrite the alignment checking iterators for iovec and bvec to be easier
> to read, and also significantly more compact in terms of generated code.
> This saves 270 bytes of text on x86-64 for me (with clang-18) and 224
> bytes on arm64 (with gcc-13).
> 
> In profiles, also saves a bit of time as well for the same workload:
> 
> [...]

Applied to the vfs.misc branch of the vfs/vfs.git tree.
Patches in the vfs.misc branch should appear in linux-next soon.

Please report any outstanding bugs that were missed during review in a
new review to the original patch series allowing us to drop it.

It's encouraged to provide Acked-bys and Reviewed-bys even though the
patch has now been applied. If possible patch trailers will be updated.

Note that commit hashes shown below are subject to change due to rebase,
trailer updates or similar. If in doubt, please check the listed branch.

tree:   https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git
branch: vfs.misc

[1/1] iov_iter: streamline iovec/bvec alignment iteration
      https://git.kernel.org/vfs/vfs/c/89d0b83ecf39

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-01-25 16:24 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-23 22:24 [PATCH v2] iov_iter: streamline iovec/bvec alignment iteration Jens Axboe
2024-01-24 15:22 ` Keith Busch
2024-01-25 16:24 ` Christian Brauner

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).