iov_iter: Convert the iterator macros into inline funcs
Subsystems (10)
v5 cover iov_iter: Convert the iterator macros into inline funcs
Hi Jens,
Could you take these patches into the block tree? Note that it's on top of the kunit patchset though it could be separate. The patches convert the iov_iter iteration macros to be inline functions.
(1) Convert iter->user_backed to user_backed_iter() in the sound PCM driver.
(2) Convert iter->user_backed to user_backed_iter() in a couple of infiniband drivers.
(3) Renumber the type enum so that the ITER_* constants match the order in
iterate_and_advance*().
(4) Since (3) puts UBUF and IOVEC at 0 and 1, change user_backed_iter() to just use the type value and get rid of the extra flag.
(5) Convert the iov_iter iteration macros to always-inline functions to make the code easier to follow. It uses function pointers, but they get optimised away. The priv2 argument likewise gets optimised away if unused.
The functions are placed into a header file so that bespoke iterators can be created elsewhere. For instance, rbd has an optimisation that requires it to scan to the buffer it is given to see if it is all zeros. It would be nice if this could use iterate_and_advance() - but that's currently buried inside lib/iov_iter.c.
Whilst we could provide a generic iteration function that takes a pair of function pointers instead, it does add around 16% overhead in the framework, presumably from the combination of function pointers and various mitigations.
Further, if it is known that just a particular iterator-type is in play, just that iteration function can be used.
(6) Move the check for ->copy_mc to _copy_from_iter() and copy_page_from_iter_atomic() rather than in memcpy_from_iter_mc() where it gets repeated for every segment. Instead, we check once and invoke a side function that can use iterate_bvec() rather than iterate_and_advance() and supply a different step function.
(7) Provide a cut-down iteration function that only handles kernel-backed iterators (ie. BVEC, KVEC, XARRAY and DISCARD) for situations where we know that we won't see UBUF/IOVEC. This crops up in a number of filesystems where we decant a UBUF/IOVEC iter into a series of BVEC iters.
(8) Move the copy-and-csum code to net/ where it can be in proximity with the code that uses it. This eliminates the code if CONFIG_NET=n and allows for the slim possibility of it being inlined.
(9) Fold memcpy_and_csum() in to its two users.
(10) Move csum_and_copy_from_iter_full() out of line and merge in csum_and_copy_from_iter() since the former is the only caller of the latter.
(11) Move hash_and_copy_to_iter() to net/ where it can be with its only caller.
Anyway, the changes in compiled function size either side of patches (5)+(6) on x86_64 look like:
__copy_from_iter_mc new 0xe8 _copy_from_iter inc 0x360 -> 0x3a7 +0x47 _copy_from_iter_flushcache inc 0x34c -> 0x38e +0x42 _copy_from_iter_nocache inc 0x354 -> 0x378 +0x24 _copy_mc_to_iter inc 0x396 -> 0x3f1 +0x5b _copy_to_iter inc 0x33b -> 0x385 +0x4a copy_page_from_iter_atomic.part.0 inc 0x393 -> 0x3e0 +0x4d copy_page_to_iter_nofault.part.0 inc 0x3de -> 0x3eb +0xd copyin del 0x30 copyout del 0x2d copyout_mc del 0x2b csum_and_copy_from_iter inc 0x3db -> 0x41d +0x42 csum_and_copy_to_iter inc 0x45d -> 0x48d +0x30 iov_iter_zero inc 0x34a -> 0x36a +0x20 memcpy_from_iter.isra.0 del 0x1f
Note that there's a noticeable expansion on some of the main functions because a number of the helpers get inlined instead of being called.
In terms of benchmarking patch (5)+(6), three runs without them:
iov_kunit_benchmark_ubuf: avg 3955 uS, stddev 169 uS
iov_kunit_benchmark_ubuf: avg 4122 uS, stddev 1292 uS
iov_kunit_benchmark_ubuf: avg 4451 uS, stddev 1362 uS
iov_kunit_benchmark_iovec: avg 6607 uS, stddev 22 uS
iov_kunit_benchmark_iovec: avg 6608 uS, stddev 19 uS
iov_kunit_benchmark_iovec: avg 6609 uS, stddev 24 uS
iov_kunit_benchmark_bvec: avg 3166 uS, stddev 11 uS
iov_kunit_benchmark_bvec: avg 3167 uS, stddev 13 uS
iov_kunit_benchmark_bvec: avg 3170 uS, stddev 16 uS iov_kunit_benchmark_bvec_split: avg 3394 uS, stddev 12 uS iov_kunit_benchmark_bvec_split: avg 3394 uS, stddev 20 uS iov_kunit_benchmark_bvec_split: avg 3395 uS, stddev 19 uS
iov_kunit_benchmark_kvec: avg 2672 uS, stddev 12 uS
iov_kunit_benchmark_kvec: avg 2672 uS, stddev 12 uS
iov_kunit_benchmark_kvec: avg 2672 uS, stddev 9 uS iov_kunit_benchmark_xarray: avg 3719 uS, stddev 9 uS iov_kunit_benchmark_xarray: avg 3719 uS, stddev 9 uS iov_kunit_benchmark_xarray: avg 3721 uS, stddev 24 uS
and three runs with them:
iov_kunit_benchmark_ubuf: avg 4110 uS, stddev 1254 uS
iov_kunit_benchmark_ubuf: avg 4141 uS, stddev 1411 uS
iov_kunit_benchmark_ubuf: avg 4572 uS, stddev 1889 uS
iov_kunit_benchmark_iovec: avg 6582 uS, stddev 27 uS
iov_kunit_benchmark_iovec: avg 6585 uS, stddev 25 uS
iov_kunit_benchmark_iovec: avg 6586 uS, stddev 48 uS
iov_kunit_benchmark_bvec: avg 3175 uS, stddev 13 uS
iov_kunit_benchmark_bvec: avg 3177 uS, stddev 12 uS
iov_kunit_benchmark_bvec: avg 3178 uS, stddev 12 uS iov_kunit_benchmark_bvec_split: avg 3380 uS, stddev 20 uS iov_kunit_benchmark_bvec_split: avg 3384 uS, stddev 15 uS iov_kunit_benchmark_bvec_split: avg 3386 uS, stddev 25 uS
iov_kunit_benchmark_kvec: avg 2671 uS, stddev 11 uS
iov_kunit_benchmark_kvec: avg 2672 uS, stddev 12 uS
iov_kunit_benchmark_kvec: avg 2677 uS, stddev 20 uS iov_kunit_benchmark_xarray: avg 3599 uS, stddev 20 uS iov_kunit_benchmark_xarray: avg 3603 uS, stddev 8 uS iov_kunit_benchmark_xarray: avg 3610 uS, stddev 16 uS
I've pushed the patches here also:
https://git.kernel.org/pub/scm/linux/kernel/git/dhowells/linux-fs.git/log/?h=iov-cleanup
David
Changes
=======
ver #5)
- Moved kunit test patches out to a separate set.
- Moved "iter->count - progress" into individual iteration subfunctions.
- Fix iterate_iovec() and to update iter->__iovec after subtracting it to calculate iter->nr_segs.
- Merged the function inlining patch and the move-to-header patch.
- Rearranged the patch order slightly to put the patches to move networking stuff to net/ last.
- Went back to a simpler patch to special-case ->copy_mc in _copy_from_iter() and copy_page_from_iter_atomic() before we get to call iterate_and_advance() so as to reduce the number of checks for this.
ver #4)
- Fix iterate_bvec() and iterate_kvec() to update iter->bvec and iter->kvec after subtracting it to calculate iter->nr_segs.
- Change iterate_xarray() to use start+progress rather than increasing start to reduce code size.
- Added patches to move some iteration functions over to net/ as the files there can #include the iterator framework.
- Added a patch to benchmark the iteration.
- Tried an experimental patch to make copy_from_iter() and similar always catch MCE.
ver #3)
- Use min_t(size_t,) not min() to avoid a warning on Hexagon.
- Inline all the step functions.
- Added a patch to better handle copy_mc.
ver #2)
- Rebased on top of Willy's changes in linux-next.
- Change the checksum argument to the iteration functions to be a general void* and use it to pass iter->copy_mc flag to memcpy_from_iter_mc() to avoid using a function pointer.
- Arrange the end of the iterate_*() functions to look the same to give the optimiser the best chance.
- Make iterate_and_advance() a wrapper around iterate_and_advance2().
- Adjust iterate_and_advance2() to use if-else-if-else-if-else rather than switch(), to put ITER_BVEC before KVEC and to mark UBUF and IOVEC as likely().
- Move "iter->count += progress" into iterate_and_advance2() from the iterate functions.
- Mark a number of the iterator helpers with __always_inline.
- Fix _copy_from_iter_flushcache() to use memcpy_from_iter_flushcache() not memcpy_from_iter().
David Howells (11):
sound: Fix snd_pcm_readv()/writev() to use iov access functions
infiniband: Use user_backed_iter() to see if iterator is UBUF/IOVEC
iov_iter: Renumber ITER_* constants
iov_iter: Derive user-backedness from the iterator type
iov_iter: Convert iterate*() to inline funcs
iov_iter: Don't deal with iter->copy_mc in memcpy_from_iter_mc()
iov_iter: Add a kernel-type iterator-only iteration function iov_iter, net: Move csum_and_copy_to/from_iter() to net/ iov_iter, net: Fold in csum_and_memcpy()
iov_iter, net: Merge csum_and_copy_from_iter{,_full}() together iov_iter, net: Move hash_and_copy_to_iter() to net/
drivers/infiniband/hw/hfi1/file_ops.c | 2 +- drivers/infiniband/hw/qib/qib_file_ops.c | 2 +- include/linux/iov_iter.h | 305 ++++++++++++++++ include/linux/skbuff.h | 3 + include/linux/uio.h | 29 +- lib/iov_iter.c | 441 +++++++---------------- net/core/datagram.c | 75 +++- net/core/skbuff.c | 40 ++ sound/core/pcm_native.c | 4 +- 9 files changed, 569 insertions(+), 332 deletions(-) create mode 100644 include/linux/iov_iter.h
PATCH v5 1/11 sound: Fix snd_pcm_readv()/writev() to use iov access functions
Fix snd_pcm_readv()/writev() to use iov access functions rather than poking at the iov_iter internals directly.
--- sound/core/pcm_native.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
sound/core/pcm_native.c +2 -2
@@ -3527,7 +3527,7 @@ static ssize_t snd_pcm_readv(struct kiocb *iocb, struct iov_iter *to)if (runtime->state ==SNDRV_PCM_STATE_OPEN || runtime->state ==SNDRV_PCM_STATE_DISCONNECTED )return -EBADFD ; -if (!to->user_backed) +if (!user_backed_iter (to))return -EINVAL ;if (to->nr_segs >1024 || to->nr_segs != runtime->channels)return -EINVAL ;
@@ -3567,7 +3567,7 @@ static ssize_t snd_pcm_writev(struct kiocb *iocb, struct iov_iter *from)if (runtime->state ==SNDRV_PCM_STATE_OPEN || runtime->state ==SNDRV_PCM_STATE_DISCONNECTED )return -EBADFD ; -if (!from->user_backed) +if (!user_backed_iter (from))return -EINVAL ;if (from->nr_segs >128 || from->nr_segs != runtime->channels || !frame_aligned (runtime, iov->iov_len))
Re: [PATCH v5 01/11] sound: Fix snd_pcm_readv()/writev() to use iov access functions
Quoted 11 lines · David Howells
Fix snd_pcm_readv()/writev() to use iov access functions rather than poking at the
iov_iterinternals directly.Signed-off-by David Howells <[email protected]>cc Jaroslav Kysela <[email protected]>cc Takashi Iwai <[email protected]>cc Oswald Buddenhagen <[email protected]>cc Jens Axboe <[email protected]>cc Suren Baghdasaryan <[email protected]>cc Kuninori Morimoto <[email protected]>
Quoted 3 lines
--- sound/core/pcm_native.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Signature · 2 lines
Jaroslav Kysela <[email protected]> Linux Sound Maintainer; ALSA Project; Red Hat, Inc.
Re: [PATCH v5 01/11] sound: Fix snd_pcm_readv()/writev() to use iov access functions
In reply to David HowellsQuoted 4 lines · David Howells
Fix snd_pcm_readv()/writev() to use iov access functions rather than poking at the
iov_iterinternals directly.
Quoted 8 lines
Signed-off-by David Howells <[email protected]>cc Jaroslav Kysela <[email protected]>cc Takashi Iwai <[email protected]>cc Oswald Buddenhagen <[email protected]>cc Jens Axboe <[email protected]>cc Suren Baghdasaryan <[email protected]>cc Kuninori Morimoto <[email protected]>
Would you apply it through your tree, or shall I apply this one via sound git tree?
thanks,
Takashi
Quoted 27 lines
--- sound/core/pcm_native.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)sound/core/pcm_native.c +2
-2@@ -3527,7 +3527,7 @@ static ssize_t snd_pcm_readv(struct kiocb *iocb, struct iov_iter *to)if (runtime->state ==SNDRV_PCM_STATE_OPEN || runtime->state ==SNDRV_PCM_STATE_DISCONNECTED )return -EBADFD ; -if (!to->user_backed) +if (!user_backed_iter (to))return -EINVAL ;if (to->nr_segs >1024 || to->nr_segs != runtime->channels)return -EINVAL ;@@ -3567,7 +3567,7 @@ static ssize_t snd_pcm_writev(struct kiocb *iocb, struct iov_iter *from)if (runtime->state ==SNDRV_PCM_STATE_OPEN || runtime->state ==SNDRV_PCM_STATE_DISCONNECTED )return -EBADFD ; -if (!from->user_backed) +if (!user_backed_iter (from))return -EINVAL ;if (from->nr_segs >128 || from->nr_segs != runtime->channels || !frame_aligned (runtime, iov->iov_len))
Re: [PATCH v5 01/11] sound: Fix snd_pcm_readv()/writev() to use iov access functions
Quoted 2 lines · Takashi Iwai
Would you apply it through your tree, or shall I apply this one via sound git tree?
It's a prerequisite for a later patch in this series, so I'd prefer to keep it with my other patches.
David
Re: [PATCH v5 01/11] sound: Fix snd_pcm_readv()/writev() to use iov access functions
Quoted 8 lines · David Howells
Quoted 2 lines · Takashi Iwai
Would you apply it through your tree, or shall I apply this one via sound git tree?
It's a prerequisite for a later patch in this series, so I'd prefer to keep it with my other patches.
Sure, please go ahead.
thanks,
Takashi
PATCH v5 2/11 infiniband: Use user_backed_iter() to see if iterator is UBUF/IOVEC
In reply to David HowellsUse user_backed_iter() to see if iterator is UBUF/IOVEC rather than poking inside the iterator.
--- drivers/infiniband/hw/hfi1/file_ops.c | 2 +- drivers/infiniband/hw/qib/qib_file_ops.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/infiniband/hw/hfi1/file_ops.c +1 -1
@@ -267,7 +267,7 @@ static ssize_t hfi1_write_iter(struct kiocb *kiocb, struct iov_iter *from)if (!HFI1_CAP_IS_KSET (SDMA ))return -EINVAL ; -if (!from->user_backed) +if (!user_backed_iter (from))return -EINVAL ; idx =srcu_read_lock (&fd->pq_srcu); pq =srcu_dereference (fd->pq, &fd->pq_srcu);
drivers/infiniband/hw/qib/qib_file_ops.c +1 -1
@@ -2244,7 +2244,7 @@ static ssize_t qib_write_iter(struct kiocb *iocb, struct iov_iter *from)struct qib_ctxtdata *rcd =ctxt_fp (iocb->ki_filp);struct qib_user_sdma_queue *pq = fp->pq; -if (!from->user_backed || !from->nr_segs || !pq) +if (!user_backed_iter (from) || !from->nr_segs || !pq)return -EINVAL ;return qib_user_sdma_writev (rcd, pq,iter_iov (from), from->nr_segs);
PATCH v5 3/11 iov_iter: Renumber ITER_* constants
In reply to David HowellsRenumber the ITER_* iterator-type constants to put things in the same order as in the iteration functions and to group user-backed iterators at the bottom.
--- include/linux/uio.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
include/linux/uio.h +2 -2
@@ -21,12 +21,12 @@ struct kvec {enum iter_type {/* iter types */ +ITER_UBUF ,ITER_IOVEC , -ITER_KVEC ,ITER_BVEC , +ITER_KVEC ,ITER_XARRAY ,ITER_DISCARD , -ITER_UBUF , };#define ITER_SOURCE 1 // == WRITE
PATCH v5 4/11 iov_iter: Derive user-backedness from the iterator type
In reply to David HowellsUse the iterator type to determine whether an iterator is user-backed or not rather than using a special flag for it. Now that ITER_UBUF and ITER_IOVEC are 0 and 1, they can be checked with a single comparison.
--- include/linux/uio.h | 4 +--- lib/iov_iter.c | 1 - 2 files changed, 1 insertion(+), 4 deletions(-)
include/linux/uio.h +1 -3
@@ -43,7 +43,6 @@ struct iov_iter {bool copy_mc;bool nofault;bool data_source; -bool user_backed;union {size_t iov_offset;int last_offset;
@@ -143,7 +142,7 @@ static inline unsigned char iov_iter_rw(const struct iov_iter *i)static inline bool user_backed_iter (const struct iov_iter *i) { -return i->user_backed; +return iter_is_ubuf (i) ||iter_is_iovec (i); }/*
@@ -383,7 +382,6 @@ static inline void iov_iter_ubuf(struct iov_iter *i, unsigned int direction, *i = (struct iov_iter ) { .iter_type =ITER_UBUF , .copy_mc = false, - .user_backed = true, .data_source = direction, .ubuf = buf, .count = count,
lib/iov_iter.c +0 -1
@@ -290,7 +290,6 @@ void iov_iter_init(struct iov_iter *i, unsigned int direction, .iter_type = ITER_IOVEC, .copy_mc = false, .nofault = false, - .user_backed = true, .data_source = direction, .__iov = iov, .nr_segs = nr_segs,
PATCH v5 5/11 iov_iter: Convert iterate*() to inline funcs
In reply to David HowellsConvert the iov_iter iteration macros to inline functions to make the code easier to follow.
The functions are marked __always_inline as we don't want to end up with indirect calls in the code. This, however, leaves dealing with ->copy_mc in an awkard situation since the step function (memcpy_from_iter_mc()) needs to test the flag in the iterator, but isn't passed the iterator. This will be dealt with in a follow-up patch.
The variable names in the per-type iterator functions have been harmonised as much as possible and made clearer as to the variable purpose.
The iterator functions are also moved to a header file so that other operations that need to scan over an iterator can be added. For instance, the rbd driver could use this to scan a buffer to see if it is all zeros and libceph could use this to generate a crc.
---
Notes:
Changes
=======
ver #5)
- Merge in patch to move iteration framework to a header file.
- Move "iter->count - progress" into individual iteration subfunctions.
include/linux/iov_iter.h | 274 ++++++++++++++++++++++++++ lib/iov_iter.c | 416 ++++++++++++++++----------------------- 2 files changed, 449 insertions(+), 241 deletions(-) create mode 100644 include/linux/iov_iter.h
include/linux/iov_iter.h new file +274 -0
@@ -0,0 +1,274 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* I/O iterator iteration building functions. +* +* Copyright (C) 2023 Red Hat, Inc. All Rights Reserved. +* Written by David Howells ([email protected]) +*/ + +#ifndef _LINUX_IOV_ITER_H +#define _LINUX_IOV_ITER_H + +#include <linux/uio.h> +#include <linux/bvec.h> + +typedef size_t (*iov_step_f )(void *iter_base,size_t progress,size_t len, +void *priv,void *priv2); +typedef size_t (*iov_ustep_f )(void __user *iter_base,size_t progress,size_t len, +void *priv,void *priv2); + +/* +* Handle ITER_UBUF. +*/ +static __always_inline +size_t iterate_ubuf(struct iov_iter *iter, size_t len, void *priv, void *priv2, +iov_ustep_f step) +{ +void __user *base = iter->ubuf; +size_t progress =0 , remain; + + remain =step (base + iter->iov_offset,0 , len, priv, priv2); + progress = len - remain; + iter->iov_offset += progress; + iter->count -= progress; +return progress; +} + +/* +* Handle ITER_IOVEC. +*/ +static __always_inline +size_t iterate_iovec(struct iov_iter *iter, size_t len, void *priv, void *priv2, +iov_ustep_f step) +{ +const struct iovec *p = iter->__iov; +size_t progress =0 , skip = iter->iov_offset; + +do { +size_t remain, consumed; +size_t part =min (len, p->iov_len - skip); + +if (likely (part)) { + remain =step (p->iov_base + skip, progress, part, priv, priv2); + consumed = part - remain; + progress += consumed; + skip += consumed; + len -= consumed; +if (skip < p->iov_len) +break ; + } + p++; + skip =0 ; + }while (len); + + iter->nr_segs -= p - iter->__iov; + iter->__iov = p; + iter->iov_offset = skip; + iter->count -= progress; +return progress; +} + +/* +* Handle ITER_KVEC. +*/ +static __always_inline +size_t iterate_kvec(struct iov_iter *iter, size_t len, void *priv, void *priv2, +iov_step_f step) +{ +const struct kvec *p = iter->kvec; +size_t progress =0 , skip = iter->iov_offset; + +do { +size_t remain, consumed; +size_t part =min (len, p->iov_len - skip); + +if (likely (part)) { + remain =step (p->iov_base + skip, progress, part, priv, priv2); + consumed = part - remain; + progress += consumed; + skip += consumed; + len -= consumed; +if (skip < p->iov_len) +break ; + } + p++; + skip =0 ; + }while (len); + + iter->nr_segs -= p - iter->kvec; + iter->kvec = p; + iter->iov_offset = skip; + iter->count -= progress; +return progress; +} + +/* +* Handle ITER_BVEC. +*/ +static __always_inline +size_t iterate_bvec(struct iov_iter *iter, size_t len, void *priv, void *priv2, +iov_step_f step) +{ +const struct bio_vec *p = iter->bvec; +size_t progress =0 , skip = iter->iov_offset; + +do { +size_t remain, consumed; +size_t offset = p->bv_offset + skip, part; +void *kaddr =kmap_local_page (p->bv_page + offset /PAGE_SIZE ); + + part =min3 (len, + (size_t )(p->bv_len - skip), + (size_t )(PAGE_SIZE - offset %PAGE_SIZE )); + remain =step (kaddr + offset %PAGE_SIZE , progress, part, priv, priv2); +kunmap_local (kaddr); + consumed = part - remain; + len -= consumed; + progress += consumed; + skip += consumed; +if (skip >= p->bv_len) { + skip =0 ; + p++; + } +if (remain) +break ; + }while (len); + + iter->nr_segs -= p - iter->bvec; + iter->bvec = p; + iter->iov_offset = skip; + iter->count -= progress; +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. +* @len: The amount to iterate over. +* @priv: Data for the step functions. +* @priv2: More data for the step functions. +* @ustep: Function for UBUF/IOVEC iterators; given __user addresses. +* @step: Function for other iterators; given kernel addresses. +* +* Iterate over the next part of an iterator, up to the specified length. The +* buffer is presented in segments, which for kernel iteration are broken up by +* physical pages and mapped, with the mapped address being presented. +* +* Two step functions, @step and @ustep, must be provided, one for handling +* mapped kernel addresses and the other is given user addresses which have the +* potential to fault since no pinning is performed. +* +* The step functions are passed the address and length of the segment, @priv, +* @priv2 and the amount of data so far iterated over (which can, for example, +* be added to @priv to point to the right part of a second buffer). The step +* functions should return the amount of the segment they didn't process (ie. 0 +* indicates complete processsing). +* +* This function returns the amount of data processed (ie. 0 means nothing was +* processed and the value of @len means processes to completion). +*/ +static __always_inline +size_t iterate_and_advance2(struct iov_iter *iter, size_t len, void *priv, +void *priv2,iov_ustep_f ustep,iov_step_f step) +{ +if (unlikely (iter->count < len)) + len = iter->count; +if (unlikely (!len)) +return 0 ; + +if (likely (iter_is_ubuf (iter))) +return iterate_ubuf (iter, len, priv, priv2, ustep); +if (likely (iter_is_iovec (iter))) +return iterate_iovec (iter, len, priv, priv2, ustep); +if (iov_iter_is_bvec (iter)) +return iterate_bvec (iter, len, priv, priv2, step); +if (iov_iter_is_kvec (iter)) +return iterate_kvec (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); +} + +/** +* iterate_and_advance - Iterate over an iterator +* @iter: The iterator to iterate over. +* @len: The amount to iterate over. +* @priv: Data for the step functions. +* @ustep: Function for UBUF/IOVEC iterators; given __user addresses. +* @step: Function for other iterators; given kernel addresses. +* +* As iterate_and_advance2(), but priv2 is always NULL. +*/ +static __always_inline +size_t iterate_and_advance(struct iov_iter *iter, size_t len, void *priv, +iov_ustep_f ustep,iov_step_f step) +{ +return iterate_and_advance2 (iter, len, priv,NULL , ustep, step); +} + +#endif /* _LINUX_IOV_ITER_H */
lib/iov_iter.c +175 -241
531 lines not on this page · raw message
Simon Horman
reviewerRe: [PATCH v5 05/11] iov_iter: Convert iterate*() to inline funcs
On Wed, Sep 20, 2023 at 11:22:25PM +0100, David Howells wrote:
...
Quoted 19 lines
lib/iov_iter.c +2
-9@@ -312,23 +192,29 @@ size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i)return 0 ;if (user_backed_iter (i))might_fault (); -iterate_and_advance (i, bytes, base, len, off, -copyout (base, addr + off, len), -memcpy (base, addr + off, len) - ) - -return bytes; +return iterate_and_advance (i, bytes, (void *)addr, + copy_to_user_iter, memcpy_to_iter); }EXPORT_SYMBOL (_copy_to_iter);#ifdef CONFIG_ARCH_HAS_COPY_MC -static int copyout_mc (void __user *to,const void *from,size_t n) -{ -if (access_ok (to, n)) {
lib/iov_iter.c size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i) · line 208
Quoted 10 lines
lib/iov_iter.c +8
-2-instrument_copy_to_user (to, from, n); - n =copy_mc_to_user ((__force void *) to, from, n); +static __always_inline +size_t copy_to_user_iter_mc(void __user *iter_to, size_t progress, +size_t len,void *from,void *priv2) +{ +if (access_ok (iter_to, len)) { + from += progress; +instrument_copy_to_user (iter_to, from, len); + len =copy_mc_to_user (iter_to, from, len);
Hi David,
Sparse complains a bit about the line above, perhaps the '(__force void *)' should be retained from the old code?
lib/iov_iter.c:208:39: warning: incorrect type in argument 1 (different address spaces)
lib/iov_iter.c:208:39: expected void *to
lib/iov_iter.c:208:39: got void [noderef] __user *iter_to
Quoted 13 lines
lib/iov_iter.c +8
-1} -return n; +return len; +} + +static __always_inline +size_t memcpy_to_iter_mc(void *iter_to, size_t progress, +size_t len,void *from,void *priv2) +{ +return copy_mc_to_kernel (iter_to, from + progress, len); }/**
...
Re: [PATCH v5 05/11] iov_iter: Convert iterate*() to inline funcs
Quoted 2 lines · Simon Horman
Sparse complains a bit about the line above, perhaps the '(
__forcevoid *)' should be retained from the old code?
I've added a patch to add the missing __user to the x86 copy_mc_to_user(). The powerpc one already has it.
David
PATCH v5 6/11 iov_iter: Don't deal with iter->copy_mc in memcpy_from_iter_mc()
In reply to David Howellsiter->copy_mc is only used with a bvec iterator and only by dump_emit_page() in fs/coredump.c so rather than handle this in memcpy_from_iter_mc() where it is checked repeatedly by _copy_from_iter() and copy_page_from_iter_atomic(),
--- lib/iov_iter.c | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-)
lib/iov_iter.c +27 -12
@@ -253,14 +253,33 @@ size_t _copy_mc_to_iter(const void *addr, size_t bytes, struct iov_iter *i)EXPORT_SYMBOL_GPL (_copy_mc_to_iter);#endif /* CONFIG_ARCH_HAS_COPY_MC */ -static size_t memcpy_from_iter_mc (void *iter_from,size_t progress, -size_t len,void *to,void *priv2) +static __always_inline +size_t memcpy_from_iter_mc(void *iter_from, size_t progress, +size_t len,void *to,void *priv2) +{ +return copy_mc_to_kernel (to + progress, iter_from, len); +} + +static size_t __copy_from_iter_mc (void *addr,size_t bytes,struct iov_iter *i) { -struct iov_iter *iter = priv2; +size_t progress; -if (iov_iter_is_copy_mc (iter)) -return copy_mc_to_kernel (to + progress, iter_from, len); -return memcpy_from_iter (iter_from, progress, len, to, priv2); +if (unlikely (i->count < bytes)) + bytes = i->count; +if (unlikely (!bytes)) +return 0 ; + progress =iterate_bvec (i, bytes, addr,NULL , memcpy_from_iter_mc); + i->count -= progress; +return progress; +} + +static __always_inline +size_t __copy_from_iter(void *addr, size_t bytes, struct iov_iter *i) +{ +if (unlikely (iov_iter_is_copy_mc (i))) +return __copy_from_iter_mc (addr, bytes, i); +return iterate_and_advance (i, bytes, addr, + copy_from_user_iter, memcpy_from_iter); }size_t _copy_from_iter (void *addr,size_t bytes,struct iov_iter *i)
@@ -270,9 +289,7 @@ size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i)if (user_backed_iter (i))might_fault (); -return iterate_and_advance2 (i, bytes, addr, i, - copy_from_user_iter, - memcpy_from_iter_mc); +return __copy_from_iter (addr, bytes, i); }EXPORT_SYMBOL (_copy_from_iter);
@@ -493,9 +510,7 @@ size_t copy_page_from_iter_atomic(struct page *page, size_t offset, } p =kmap_atomic (page) + offset; - n =iterate_and_advance2 (i, n, p, i, - copy_from_user_iter, - memcpy_from_iter_mc); +__copy_from_iter (p, n, i);kunmap_atomic (p); copied += n; offset += n;
PATCH v5 7/11 iov_iter: Add a kernel-type iterator-only iteration function
In reply to David HowellsAdd an iteration function that can only iterate over kernel internal-type iterators (ie. BVEC, KVEC, XARRAY) and not user-backed iterators (ie. UBUF and IOVEC). This allows for smaller iterators to be built when it is known the caller won't have a user-backed iterator.
--- include/linux/iov_iter.h | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+)
include/linux/iov_iter.h +31 -0
@@ -271,4 +271,35 @@ size_t iterate_and_advance(struct iov_iter *iter, size_t len, void *priv, return iterate_and_advance2(iter, len, priv, NULL, ustep, step); } +/** +* iterate_and_advance_kernel - Iterate over a kernel iterator +* @iter: The iterator to iterate over. +* @len: The amount to iterate over. +* @priv: Data for the step functions. +* @step: Processing function; given kernel addresses. +* +* Like iterate_and_advance2(), but rejected UBUF and IOVEC iterators and does +* not take a user-step function. +*/ +static __always_inline +size_t iterate_and_advance_kernel(struct iov_iter *iter, size_t len, void *priv, +void *priv2,iov_step_f step) +{ +if (unlikely (iter->count < len)) + len = iter->count; +if (unlikely (!len)) +return 0 ; + +if (iov_iter_is_bvec (iter)) +return iterate_bvec (iter, len, priv, priv2, step); +if (iov_iter_is_kvec (iter)) +return iterate_kvec (iter, len, priv, priv2, step); +if (iov_iter_is_xarray (iter)) +return iterate_xarray (iter, len, priv, priv2, step); +if (iov_iter_is_discard (iter)) +return iterate_discard (iter, len, priv, priv2, step); +WARN_ON_ONCE (1 ); +return 0 ; +} +#endif /* _LINUX_IOV_ITER_H */
Simon Horman
reviewerRe: [PATCH v5 07/11] iov_iter: Add a kernel-type iterator-only iteration function
Quoted 25 lines · David Howells
Add an iteration function that can only iterate over kernel internal-type iterators (ie. BVEC, KVEC, XARRAY) and not user-backed iterators (ie. UBUF and IOVEC). This allows for smaller iterators to be built when it is known the caller won't have a user-backed iterator.
Signed-off-by David Howells <[email protected]>cc Alexander Viro <[email protected]>cc Jens Axboe <[email protected]>cc Christoph Hellwig <[email protected]>cc Christian Brauner <[email protected]>cc Matthew Wilcox <[email protected]>cc Linus Torvalds <[email protected]>cc David Laight <[email protected]>--- include/linux/iov_iter.h | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+)include/linux/iov_iter.h +0
-0
include/linux/iov_iter.h size_t iterate_and_advance(struct iov_iter *iter, size_t len, void *priv, · line 278
Quoted 8 lines
include/linux/iov_iter.h +5
-0@@ -271,4 +271,35 @@ size_t iterate_and_advance(struct iov_iter *iter, size_t len, void *priv,return iterate_and_advance2 (iter, len, priv,NULL , ustep, step); } +/** +* iterate_and_advance_kernel - Iterate over a kernel iterator +* @iter: The iterator to iterate over. +* @len: The amount to iterate over. +* @priv: Data for the step functions.
nit: an entry for @priv2 belongs here
Quoted 9 lines
include/linux/iov_iter.h +9
-0+* @step: Processing function; given kernel addresses. +* +* Like iterate_and_advance2(), but rejected UBUF and IOVEC iterators and does +* not take a user-step function. +*/ +static __always_inline +size_t iterate_and_advance_kernel(struct iov_iter *iter, size_t len, void *priv, +void *priv2,iov_step_f step) +{
...
PATCH v5 8/11 iov_iter, net: Move csum_and_copy_to/from_iter() to net/
In reply to David HowellsMove csum_and_copy_to/from_iter() to net code now that the iteration framework can be #included.
--- include/linux/skbuff.h | 25 ++++++++++++ include/linux/uio.h | 18 --------- lib/iov_iter.c | 89 ------------------------------------------ net/core/datagram.c | 50 +++++++++++++++++++++++- net/core/skbuff.c | 33 ++++++++++++++++ 5 files changed, 107 insertions(+), 108 deletions(-)
include/linux/skbuff.h +25 -0
@@ -3679,6 +3679,31 @@ static inline int __must_check skb_put_padto(struct sk_buff *skb, unsigned int lreturn __skb_put_padto (skb, len, true); } +static inline __wsum csum_and_memcpy (void *to,const void *from,size_t len, +__wsum sum,size_t off) +{ +__wsum next =csum_partial_copy_nocheck (from, to, len); +return csum_block_add (sum, next, off); +} + +struct csum_state { +__wsum csum; +size_t off; +}; + +size_t csum_and_copy_from_iter (void *addr,size_t bytes,__wsum *csum,struct iov_iter *i); + +static __always_inline __must_check +bool csum_and_copy_from_iter_full(void *addr, size_t bytes, +__wsum *csum,struct iov_iter *i) +{ +size_t copied =csum_and_copy_from_iter (addr, bytes, csum, i); +if (likely (copied == bytes)) +return true; +iov_iter_revert (i, copied); +return false; +} +static inline int skb_add_data (struct sk_buff *skb,struct iov_iter *from,int copy) {
include/linux/uio.h +0 -18
@@ -341,24 +341,6 @@ iov_iter_npages_cap(struct iov_iter *i, int maxpages, size_t max_bytes)return npages; } -struct csum_state { -__wsum csum; -size_t off; -}; - -size_t csum_and_copy_to_iter (const void *addr,size_t bytes,void *csstate,struct iov_iter *i); -size_t csum_and_copy_from_iter (void *addr,size_t bytes,__wsum *csum,struct iov_iter *i); - -static __always_inline __must_check -bool csum_and_copy_from_iter_full(void *addr, size_t bytes, -__wsum *csum,struct iov_iter *i) -{ -size_t copied =csum_and_copy_from_iter (addr, bytes, csum, i); -if (likely (copied == bytes)) -return true; -iov_iter_revert (i, copied); -return false; -}size_t hash_and_copy_to_iter (const void *addr,size_t bytes,void *hashp,struct iov_iter *i);
lib/iov_iter.c +0 -89
107 lines not on this page · raw message
net/core/datagram.c +49 -1
62 lines not on this page · raw message
net/core/skbuff.c +33 -0
@@ -62,6 +62,7 @@#include <linux/if_vlan.h> #include <linux/mpls.h> #include <linux/kcov.h> +#include <linux/iov_iter.h> #include <net/protocol.h> #include <net/dst.h>
@@ -6931,3 +6932,35 @@ ssize_t skb_splice_from_iter(struct sk_buff *skb, struct iov_iter *iter,return spliced ?: ret; }EXPORT_SYMBOL (skb_splice_from_iter); + +static __always_inline +size_t memcpy_from_iter_csum(void *iter_from, size_t progress, +size_t len,void *to,void *priv2) +{ +__wsum *csum = priv2; + + *csum =csum_and_memcpy (to + progress, iter_from, len, *csum, progress); +return 0 ; +} + +static __always_inline +size_t copy_from_user_iter_csum(void __user *iter_from, size_t progress, +size_t len,void *to,void *priv2) +{ +__wsum next, *csum = priv2; + + next =csum_and_copy_from_user (iter_from, to + progress, len); + *csum =csum_block_add (*csum, next, progress); +return next ?0 : len; +} + +size_t csum_and_copy_from_iter (void *addr,size_t bytes,__wsum *csum, +struct iov_iter *i) +{ +if (WARN_ON_ONCE (!i->data_source)) +return 0 ; +return iterate_and_advance2 (i, bytes, addr, csum, + copy_from_user_iter_csum, + memcpy_from_iter_csum); +} +EXPORT_SYMBOL (csum_and_copy_from_iter);
PATCH v5 9/11 iov_iter, net: Fold in csum_and_memcpy()
In reply to David HowellsFold csum_and_memcpy() in to its callers.
--- include/linux/skbuff.h | 7 ------- net/core/datagram.c | 3 ++- net/core/skbuff.c | 3 ++- 3 files changed, 4 insertions(+), 9 deletions(-)
include/linux/skbuff.h +0 -7
@@ -3679,13 +3679,6 @@ static inline int __must_check skb_put_padto(struct sk_buff *skb, unsigned int lreturn __skb_put_padto (skb, len, true); } -static inline __wsum csum_and_memcpy (void *to,const void *from,size_t len, -__wsum sum,size_t off) -{ -__wsum next =csum_partial_copy_nocheck (from, to, len); -return csum_block_add (sum, next, off); -} -struct csum_state {__wsum csum;size_t off;
net/core/datagram.c +2 -1
@@ -732,8 +732,9 @@ size_t memcpy_to_iter_csum(void *iter_to, size_t progress, size_t len, void *from, void *priv2) { __wsum *csum = priv2; +__wsum next =csum_partial_copy_nocheck (from, iter_to, len); - *csum =csum_and_memcpy (iter_to, from + progress, len, *csum, progress); + *csum =csum_block_add (*csum, next, progress);return 0 ; }
net/core/skbuff.c +2 -1
@@ -6938,8 +6938,9 @@ size_t memcpy_from_iter_csum(void *iter_from, size_t progress, size_t len, void *to, void *priv2) { __wsum *csum = priv2; +__wsum next =csum_partial_copy_nocheck (iter_from, to + progress, len); - *csum =csum_and_memcpy (to + progress, iter_from, len, *csum, progress); + *csum =csum_block_add (*csum, next, progress);return 0 ; }
PATCH v5 10/11 iov_iter, net: Merge csum_and_copy_from_iter{,_full}() together
In reply to David HowellsMove csum_and_copy_from_iter_full() out of line and then merge csum_and_copy_from_iter() into its only caller.
--- include/linux/skbuff.h | 19 ++----------------- net/core/datagram.c | 5 +++++ net/core/skbuff.c | 20 +++++++++++++------- 3 files changed, 20 insertions(+), 24 deletions(-)
include/linux/skbuff.h +2 -17
@@ -3679,23 +3679,8 @@ static inline int __must_check skb_put_padto(struct sk_buff *skb, unsigned int lreturn __skb_put_padto (skb, len, true); } -struct csum_state { -__wsum csum; -size_t off; -}; - -size_t csum_and_copy_from_iter (void *addr,size_t bytes,__wsum *csum,struct iov_iter *i); - -static __always_inline __must_check -bool csum_and_copy_from_iter_full(void *addr, size_t bytes, -__wsum *csum,struct iov_iter *i) -{ -size_t copied =csum_and_copy_from_iter (addr, bytes, csum, i); -if (likely (copied == bytes)) -return true; -iov_iter_revert (i, copied); -return false; -} +bool csum_and_copy_from_iter_full (void *addr,size_t bytes,__wsum *csum,struct iov_iter *i) +__must_check ;static inline int skb_add_data (struct sk_buff *skb,struct iov_iter *from,int copy)
net/core/datagram.c +5 -0
@@ -738,6 +738,11 @@ size_t memcpy_to_iter_csum(void *iter_to, size_t progress,return 0 ; } +struct csum_state { +__wsum csum; +size_t off; +}; +static size_t csum_and_copy_to_iter (const void *addr,size_t bytes,void *_csstate,struct iov_iter *i) {
net/core/skbuff.c +13 -7
@@ -6955,13 +6955,19 @@ size_t copy_from_user_iter_csum(void __user *iter_from, size_t progress,return next ?0 : len; } -size_t csum_and_copy_from_iter (void *addr,size_t bytes,__wsum *csum, -struct iov_iter *i) +bool csum_and_copy_from_iter_full (void *addr,size_t bytes, +__wsum *csum,struct iov_iter *i) { +size_t copied; +if (WARN_ON_ONCE (!i->data_source)) -return 0 ; -return iterate_and_advance2 (i, bytes, addr, csum, - copy_from_user_iter_csum, - memcpy_from_iter_csum); +return false; + copied =iterate_and_advance2 (i, bytes, addr, csum, + copy_from_user_iter_csum, + memcpy_from_iter_csum); +if (likely (copied == bytes)) +return true; +iov_iter_revert (i, copied); +return false; } -EXPORT_SYMBOL (csum_and_copy_from_iter); +EXPORT_SYMBOL (csum_and_copy_from_iter_full);
PATCH v5 11/11 iov_iter, net: Move hash_and_copy_to_iter() to net/
In reply to David HowellsMove hash_and_copy_to_iter() to be with its only caller in networking code.
--- include/linux/uio.h | 3 --- lib/iov_iter.c | 20 -------------------- net/core/datagram.c | 19 +++++++++++++++++++ 3 files changed, 19 insertions(+), 23 deletions(-)
include/linux/uio.h +0 -3
@@ -341,9 +341,6 @@ iov_iter_npages_cap(struct iov_iter *i, int maxpages, size_t max_bytes)return npages; } -size_t hash_and_copy_to_iter (const void *addr,size_t bytes,void *hashp, -struct iov_iter *i); -struct iovec *iovec_from_user (const struct iovec __user *uvector,unsigned long nr_segs,unsigned long fast_segs,struct iovec *fast_iov,bool compat);
lib/iov_iter.c +0 -20
@@ -1,5 +1,4 @@// SPDX-License-Identifier: GPL-2.0-only -#include <crypto/hash.h> #include <linux/export.h> #include <linux/bvec.h> #include <linux/fault-inject-usercopy.h>
@@ -1093,25 +1092,6 @@ ssize_t iov_iter_get_pages_alloc2(struct iov_iter *i, }EXPORT_SYMBOL (iov_iter_get_pages_alloc2); -size_t hash_and_copy_to_iter (const void *addr,size_t bytes,void *hashp, -struct iov_iter *i) -{ -#ifdef CONFIG_CRYPTO_HASH -struct ahash_request *hash = hashp; -struct scatterlist sg; -size_t copied; - - copied =copy_to_iter (addr, bytes, i); -sg_init_one (&sg, addr, copied); -ahash_request_set_crypt (hash, &sg,NULL , copied); -crypto_ahash_update (hash); -return copied; -#else -return 0 ; -#endif -} -EXPORT_SYMBOL (hash_and_copy_to_iter); -static int iov_npages (const struct iov_iter *i,int maxpages) {size_t skip = i->iov_offset, size = i->count;
net/core/datagram.c +19 -0
@@ -61,6 +61,7 @@#include <net/tcp_states.h> #include <trace/events/skb.h> #include <net/busy_poll.h> +#include <crypto/hash.h> /* * Is a socket 'connection oriented' ?
@@ -489,6 +490,24 @@ static int __skb_datagram_iter(const struct sk_buff *skb, int offset,return 0 ; } +static size_t hash_and_copy_to_iter (const void *addr,size_t bytes,void *hashp, +struct iov_iter *i) +{ +#ifdef CONFIG_CRYPTO_HASH +struct ahash_request *hash = hashp; +struct scatterlist sg; +size_t copied; + + copied =copy_to_iter (addr, bytes, i); +sg_init_one (&sg, addr, copied); +ahash_request_set_crypt (hash, &sg,NULL , copied); +crypto_ahash_update (hash); +return copied; +#else +return 0 ; +#endif +} +/** * skb_copy_and_hash_datagram_iter - Copy datagram to an iovec iterator * and update a hash.
From: David Howells
Quoted 1 line
Sent: 20 September 2023 23:22
...
Quoted 9 lines
(8) Move the copy-and-csum code to
net/where it can be in proximity with the code that uses it. This eliminates the code ifCONFIG_NET=nand allows for the slim possibility of it being inlined.(9) Fold
memcpy_and_csum()in to its two users.(10) Move
csum_and_copy_from_iter_full()out of line and merge incsum_and_copy_from_iter()since the former is the only caller of the latter.
I thought that the real idea behind these was to do the checksum at the same time as the copy to avoid loading the data into the L1 data-cache twice - especially for long buffers.
I wonder how often there are multiple iov[] that actually make it better than just check summing the linear buffer?
I had a feeling that check summing of udp data was done during copy_to/from_user, but the code can't be the copy-and-csum here for that because it is missing support form odd-length buffers.
Intel x86 desktop chips can easily checksum at 8 bytes/clock (But probably not with the current code!).
(I've got ~12 bytes/clock using adox and adcx but that loop is entirely horrid and it would need run-time patching. Especially since I think some AMD cpu execute them very slowly.)
OTOH 'rep movs[bq]' copy will copy 16 bytes/clock (32 if the destination is 32 byte aligned - it pretty much won't be).
So you'd need a csum-and-copy loop that did 16 bytes every three clocks to get the same throughput for long buffers. In principle splitting the 'adc memory' into two instructions is the same number of u-ops - but I'm sure I've tried to do that and failed and the extra memory write can happen in parallel with everything else.
So I don't think you'll get 16 bytes in two clocks - but you might get it is three.
OTOH for a cpu where memcpy is code loop summing the data in the copy loop is likely to be a gain.
But I suspect doing the checksum and copy at the same time got 'all to complicated' to actually implement fully. With most modern ethernet chips checksumming receive pacakets does it really get used enough for the additional complexity?
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
Quoted 10 lines · David Laight
Quoted 9 lines
(8) Move the copy-and-csum code to
net/where it can be in proximity with the code that uses it. This eliminates the code ifCONFIG_NET=nand allows for the slim possibility of it being inlined.(9) Fold
memcpy_and_csum()in to its two users.(10) Move
csum_and_copy_from_iter_full()out of line and merge incsum_and_copy_from_iter()since the former is the only caller of the latter.
Quoted 5 lines
I thought that the real idea behind these was to do the checksum at the same time as the copy to avoid loading the data into the L1 data-cache twice - especially for long buffers.
I wonder how often there are multiple iov[] that actually make it better than just check summing the linear buffer?
It also reduces the overhead for finding the data to checksum in the case the packet gets split since we're doing the checksumming as we copy - but with a linear buffer, that's negligible.
Quoted 3 lines
I had a feeling that check summing of udp data was done during copy_to/from_user, but the code can't be the copy-and-csum here for that because it is missing support form odd-length buffers.
Is there a bug there?
Quoted 18 lines
Intel x86 desktop chips can easily checksum at 8 bytes/clock (But probably not with the current code!).
(I've got ~12 bytes/clock using adox and adcx but that loop is entirely horrid and it would need run-time patching. Especially since I think some AMD cpu execute them very slowly.)OTOH 'rep movs[bq]' copy will copy 16 bytes/clock (32 if the destination is 32 byte aligned - it pretty much won't be).
So you'd need a csum-and-copy loop that did 16 bytes every three clocks to get the same throughput for long buffers. In principle splitting the 'adc memory' into two instructions is the same number of u-ops - but I'm sure I've tried to do that and failed and the extra memory write can happen in parallel with everything else.
So I don't think you'll get 16 bytes in two clocks - but you might get it is three.
Quoted 7 lines
OTOH for a cpu where memcpy is code loop summing the data in the copy loop is likely to be a gain.
But I suspect doing the checksum and copy at the same time got 'all to complicated' to actually implement fully. With most modern ethernet chips checksumming receive pacakets does it really get used enough for the additional complexity?
You may be right. That's more a question for the networking folks than for me. It's entirely possible that the checksumming code is just not used on modern systems these days.
Maybe Willem can comment since he's the UDP maintainer?
David
Quoted 55 lines · David Howells
Quoted 10 lines · David Laight
Quoted 9 lines
(8) Move the copy-and-csum code to
net/where it can be in proximity with the code that uses it. This eliminates the code ifCONFIG_NET=nand allows for the slim possibility of it being inlined.(9) Fold
memcpy_and_csum()in to its two users.(10) Move
csum_and_copy_from_iter_full()out of line and merge incsum_and_copy_from_iter()since the former is the only caller of the latter.Quoted 5 lines
I thought that the real idea behind these was to do the checksum at the same time as the copy to avoid loading the data into the L1 data-cache twice - especially for long buffers.
I wonder how often there are multiple iov[] that actually make it better than just check summing the linear buffer?It also reduces the overhead for finding the data to checksum in the case the packet gets split since we're doing the checksumming as we copy - but with a linear buffer, that's negligible.
Quoted 3 lines
I had a feeling that check summing of udp data was done during copy_to/from_user, but the code can't be the copy-and-csum here for that because it is missing support form odd-length buffers.
Is there a bug there?
Quoted 18 lines
Intel x86 desktop chips can easily checksum at 8 bytes/clock (But probably not with the current code!).
(I've got ~12 bytes/clock using adox and adcx but that loop is entirely horrid and it would need run-time patching. Especially since I think some AMD cpu execute them very slowly.)OTOH 'rep movs[bq]' copy will copy 16 bytes/clock (32 if the destination is 32 byte aligned - it pretty much won't be).
So you'd need a csum-and-copy loop that did 16 bytes every three clocks to get the same throughput for long buffers. In principle splitting the 'adc memory' into two instructions is the same number of u-ops - but I'm sure I've tried to do that and failed and the extra memory write can happen in parallel with everything else.
So I don't think you'll get 16 bytes in two clocks - but you might get it is three.Quoted 7 lines
OTOH for a cpu where memcpy is code loop summing the data in the copy loop is likely to be a gain.
But I suspect doing the checksum and copy at the same time got 'all to complicated' to actually implement fully. With most modern ethernet chips checksumming receive pacakets does it really get used enough for the additional complexity?
Quoted 5 lines
You may be right. That's more a question for the networking folks than for me. It's entirely possible that the checksumming code is just not used on modern systems these days.
Maybe Willem can comment since he's the UDP maintainer?
Perhaps these days it is more relevant to embedded systems than high end servers.
From: Willem de Bruijn
Quoted 31 lines
Sent: 23 September 2023 07:59
Quoted 28 lines · David Howells
Quoted 10 lines · David Laight
Quoted 9 lines
(8) Move the copy-and-csum code to
net/where it can be in proximity with the code that uses it. This eliminates the code ifCONFIG_NET=nand allows for the slim possibility of it being inlined.(9) Fold
memcpy_and_csum()in to its two users.(10) Move
csum_and_copy_from_iter_full()out of line and merge incsum_and_copy_from_iter()since the former is the only caller of the latter.Quoted 5 lines
I thought that the real idea behind these was to do the checksum at the same time as the copy to avoid loading the data into the L1 data-cache twice - especially for long buffers.
I wonder how often there are multiple iov[] that actually make it better than just check summing the linear buffer?It also reduces the overhead for finding the data to checksum in the case the packet gets split since we're doing the checksumming as we copy - but with a linear buffer, that's negligible.
Quoted 3 lines
I had a feeling that check summing of udp data was done during copy_to/from_user, but the code can't be the copy-and-csum here for that because it is missing support form odd-length buffers.
Is there a bug there?
No, I misread the code - i shouldn't scan patches when I'd got a viral head code...
...
Quoted 8 lines
Quoted 5 lines
You may be right. That's more a question for the networking folks than for me. It's entirely possible that the checksumming code is just not used on modern systems these days.
Maybe Willem can comment since he's the UDP maintainer?
Perhaps these days it is more relevant to embedded systems than high end servers.
The checksum and copy are done together.
I probably missed it because the function isn't passed the old checksum (which it can pretty much process for free). Instead the caller is adding it afterwards - which involves and extra explicit csum_add().
The x86-x84 ip checksum loops are all horrid though. The unrolling in them is so 1990's.
With the out-of-order pipeline the memory accesses tend to take care of themselves.
Not to mention that a whole raft of (now oldish) cpu take two clocks to execute 'adc'.
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)