Received: by 2002:a25:d7c1:0:0:0:0:0 with SMTP id o184csp4012489ybg; Tue, 29 Oct 2019 00:25:46 -0700 (PDT) X-Google-Smtp-Source: APXvYqxirCgxwiegHMqdWrehg45nhnyYxcN5F98XxFFP/6PrHF0lgT0LXWpJ0fYyiMs5Grww8qfz X-Received: by 2002:aa7:cd0d:: with SMTP id b13mr18839696edw.146.1572333946656; Tue, 29 Oct 2019 00:25:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1572333946; cv=none; d=google.com; s=arc-20160816; b=VKHUQJ43VC+ZYDTeZrVvUwzsQNzeDlhr1SA0wdLRFrlA3/0ESmhEGSXpIb0F8vNZEj CUTSgFKg1ihW+rMskOSPIgTuTyIBfizx+D7Yc+FbeVLEgkPqBu8nWgNXqwcieaxaZm6P wFJ1d4yjVJFU0sX2x+454s0Nn21vl8ERwLe7qQ7NbHTD1WsN9SHnRsnqjIBqMNyXCUbC O2A19ZCKxKeVGUY2eQCbdfb9Q3o9RsFM3WhLrLK0azb21xZzhj/JhxHe8UQjBgOuNzZM kr2EjNkVnpAx7xQSiog6aY1/8UE83sP9nzKL9/dKBE4rongC2n6RdMkY0tFmT6PFOAHA hmPQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-disposition :content-transfer-encoding:user-agent:in-reply-to:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=Cg6FL8Z4Oi0Duw5nqpZmkZl+hK4mK8GPQGyMFpvz9kE=; b=fRqCNAFmfUY91GmrZYmRDK0rCMjRt3LUkN/0hc0ChNMBrq80Gd6lEfSQL/Hke0TxCR /43OL+JBSA41S1szkvVc2g9O5d3T/W7f+QSPsxpPncjQu6LVHGyQGTDUnB7L75GgaijY uGkPuPomq+DaSSlG2czHiOLjq9I5Eusn8eZYLWtUKoTmgk4vW71lX3PbwsU6xnuqBdU2 NxL+M2KGG2tcp8nTbwBk2ma4KyzF+wfcinDOWw2JWfVIbv7tFNWJELz4or8KBikfAu4v F+eclyOxjAEO8/ELgZguEdXQooN2ITRvMpFK4dAwL6p2qgKXW+AK4RHzTN6RWxrN3tf7 iUsA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=FDPnGskH; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id s28si9292403edm.64.2019.10.29.00.25.23; Tue, 29 Oct 2019 00:25:46 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=FDPnGskH; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730646AbfJ2CZv (ORCPT + 99 others); Mon, 28 Oct 2019 22:25:51 -0400 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:25904 "EHLO us-smtp-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727987AbfJ2CZu (ORCPT ); Mon, 28 Oct 2019 22:25:50 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1572315949; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Cg6FL8Z4Oi0Duw5nqpZmkZl+hK4mK8GPQGyMFpvz9kE=; b=FDPnGskH2/WbXLNHgSXiuFZs0oQq0oWG29wOFjsLe6sZL4Wqo2/+yMSut8e3p2gq2A5W9z y4oBVmxrkAMIF69z92xY7pAMQ1PDf8+VbzBVr3grCJYyVCzLUowYtVBrLRTD+2eLxp5Vhx XMo0NdZRqzM5SWiIk+IbRiYDlk2qrXc= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-288-8x8w1UA6NZOwtEZSwEzdbg-1; Mon, 28 Oct 2019 22:25:47 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 35EC28017DD; Tue, 29 Oct 2019 02:25:46 +0000 (UTC) Received: from ming.t460p (ovpn-8-18.pek2.redhat.com [10.72.8.18]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C23295C1D6; Tue, 29 Oct 2019 02:25:38 +0000 (UTC) Date: Tue, 29 Oct 2019 10:25:33 +0800 From: Ming Lei To: Sultan Alsawaf Cc: Jason Gunthorpe , "Martin K. Petersen" , Thomas Hellstrom , Palmer Dabbelt , Sakari Ailus , Gal Pressman , linux-kernel@vger.kernel.org Subject: Re: [PATCH] scatterlist: Speed up for_each_sg() loop macro Message-ID: <20191029022533.GE22088@ming.t460p> References: <20191025213359.7538-1-sultan@kerneltoast.com> MIME-Version: 1.0 In-Reply-To: <20191025213359.7538-1-sultan@kerneltoast.com> User-Agent: Mutt/1.12.1 (2019-06-15) X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-MC-Unique: 8x8w1UA6NZOwtEZSwEzdbg-1 X-Mimecast-Spam-Score: 0 Content-Type: text/plain; charset=WINDOWS-1252 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Oct 25, 2019 at 02:33:58PM -0700, Sultan Alsawaf wrote: > From: Sultan Alsawaf >=20 > Scatterlists are chained in predictable arrays of up to > SG_MAX_SINGLE_ALLOC sg structs in length. Using this knowledge, speed up > for_each_sg() by using constant operations to determine when to simply > increment the sg pointer by one or get the next sg array in the chain. >=20 > Rudimentary measurements with a trivial loop body show that this yields > roughly a 2x performance gain. >=20 > The following simple test module proves the correctness of the new loop > definition by testing all the different edge cases of sg chains: > #include > #include > #include >=20 > static int __init test_for_each_sg(void) > { > =09static const gfp_t gfp_flags =3D GFP_KERNEL | __GFP_NOFAIL; > struct scatterlist *sg; > struct sg_table *table; > long old =3D 0, new =3D 0; > unsigned int i, nents; >=20 > table =3D kmalloc(sizeof(*table), gfp_flags); > for (nents =3D 1; nents <=3D 3 * SG_MAX_SINGLE_ALLOC; nents++) { > BUG_ON(sg_alloc_table(table, nents, gfp_flags)); > for (sg =3D table->sgl; sg; sg =3D sg_next(sg)) > old ^=3D (long)sg; > for_each_sg(table->sgl, sg, nents, i) > new ^=3D (long)sg; > sg_free_table(table); > } >=20 > BUG_ON(old !=3D new); > kfree(table); > return 0; > } > module_init(test_for_each_sg); >=20 > Signed-off-by: Sultan Alsawaf > --- > include/linux/scatterlist.h | 5 ++++- > 1 file changed, 4 insertions(+), 1 deletion(-) >=20 > diff --git a/include/linux/scatterlist.h b/include/linux/scatterlist.h > index 556ec1ea2574..73f7fd6702d7 100644 > --- a/include/linux/scatterlist.h > +++ 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 nec= essary > */ > #define for_each_sg(sglist, sg, nr, __i)=09\ > -=09for (__i =3D 0, sg =3D (sglist); __i < (nr); __i++, sg =3D sg_next(sg= )) > +=09for (__i =3D 0, sg =3D (sglist); __i < (nr);=09=09\ > +=09 likely(++__i % (SG_MAX_SINGLE_ALLOC - 1) ||=09\ > +=09=09 (__i + 1) >=3D (nr)) ? sg++ :=09=09=09\ > +=09=09 (sg =3D sg_chain_ptr(sg + 1))) > =20 sg_alloc_table_chained() may put a small sglist as the first chunk, then chained with big one, and your patch breaks such usage. Thanks, Ming