Re: [PATCH] scatterlist: Speed up for_each_sg() loop macro

From: Jason Gunthorpe
Date: Mon Oct 28 2019 - 10:17:38 EST


On Fri, Oct 25, 2019 at 02:33:58PM -0700, Sultan Alsawaf wrote:
>
> Signed-off-by: Sultan Alsawaf <sultan@xxxxxxxxxxxxxxx>
> include/linux/scatterlist.h | 5 ++++-
> 1 file changed, 4 insertions(+), 1 deletion(-)
>
> diff --git a/include/linux/scatterlist.h b/include/linux/scatterlist.h
> index 556ec1ea2574..73f7fd6702d7 100644
> +++ b/include/linux/scatterlist.h
> @@ -146,7 +146,10 @@ static inline void sg_set_buf(struct scatterlist *sg, const void *buf,
> * Loop over each sg element, following the pointer to a new list if necessary
> */
> #define for_each_sg(sglist, sg, nr, __i) \
> - for (__i = 0, sg = (sglist); __i < (nr); __i++, sg = sg_next(sg))
> + for (__i = 0, sg = (sglist); __i < (nr); \
> + likely(++__i % (SG_MAX_SINGLE_ALLOC - 1) || \
> + (__i + 1) >= (nr)) ? sg++ : \
> + (sg = sg_chain_ptr(sg + 1)))

This is a big change in the algorithm, why are you sure it is OK?

Did you compare with just inlining sg_net?

Jason