lib/scatterlist: Avoid a double memset
diff mbox series

Message ID 20200920071544.368841-1-christophe.jaillet@wanadoo.fr
State Accepted
Commit 6ed9b92e290b530197c2dda2271f5312abc475e6
Headers show
Series
  • lib/scatterlist: Avoid a double memset
Related show

Commit Message

Christophe JAILLET Sept. 20, 2020, 7:15 a.m. UTC
'sgl' is zeroed a few lines below in 'sg_init_table()'. There is no need to
clear it twice.

Remove the redundant initialization.

Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
---
 lib/scatterlist.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Joe Perches Sept. 20, 2020, 7:28 a.m. UTC | #1
On Sun, 2020-09-20 at 09:15 +0200, Christophe JAILLET wrote:
> 'sgl' is zeroed a few lines below in 'sg_init_table()'. There is no need to
> clear it twice.
> 
> Remove the redundant initialization.

I didn't look very thoroughly, but there are at least
a few more of these with kcalloc and kzalloc like

block/bsg-lib.c-        size_t sz = (sizeof(struct scatterlist) * req->nr_phys_segments);
block/bsg-lib.c-
block/bsg-lib.c-        BUG_ON(!req->nr_phys_segments);
block/bsg-lib.c-
block/bsg-lib.c-        buf->sg_list = kzalloc(sz, GFP_KERNEL);
block/bsg-lib.c-        if (!buf->sg_list)
block/bsg-lib.c-                return -ENOMEM;
block/bsg-lib.c:        sg_init_table(buf->sg_list, req->nr_phys_segments);
---
drivers/target/target_core_rd.c-		sg = kcalloc(sg_per_table + chain_entry, sizeof(*sg),
drivers/target/target_core_rd.c-				GFP_KERNEL);
drivers/target/target_core_rd.c-		if (!sg)
drivers/target/target_core_rd.c-			return -ENOMEM;
drivers/target/target_core_rd.c-
drivers/target/target_core_rd.c:		sg_init_table(sg, sg_per_table + chain_entry);
---
net/rds/rdma.c-		sg = kcalloc(nents, sizeof(*sg), GFP_KERNEL);
net/rds/rdma.c-		if (!sg) {
net/rds/rdma.c-			ret = -ENOMEM;
net/rds/rdma.c-			goto out;
net/rds/rdma.c-		}
net/rds/rdma.c-		WARN_ON(!nents);
net/rds/rdma.c:		sg_init_table(sg, nents);
Julia Lawall Sept. 20, 2020, 8:32 a.m. UTC | #2
On Sun, 20 Sep 2020, Joe Perches wrote:

> On Sun, 2020-09-20 at 09:15 +0200, Christophe JAILLET wrote:
> > 'sgl' is zeroed a few lines below in 'sg_init_table()'. There is no need to
> > clear it twice.
> >
> > Remove the redundant initialization.
>
> I didn't look very thoroughly, but there are at least
> a few more of these with kcalloc and kzalloc like
>
> block/bsg-lib.c-        size_t sz = (sizeof(struct scatterlist) * req->nr_phys_segments);
> block/bsg-lib.c-
> block/bsg-lib.c-        BUG_ON(!req->nr_phys_segments);
> block/bsg-lib.c-
> block/bsg-lib.c-        buf->sg_list = kzalloc(sz, GFP_KERNEL);
> block/bsg-lib.c-        if (!buf->sg_list)
> block/bsg-lib.c-                return -ENOMEM;
> block/bsg-lib.c:        sg_init_table(buf->sg_list, req->nr_phys_segments);
> ---
> drivers/target/target_core_rd.c-		sg = kcalloc(sg_per_table + chain_entry, sizeof(*sg),
> drivers/target/target_core_rd.c-				GFP_KERNEL);
> drivers/target/target_core_rd.c-		if (!sg)
> drivers/target/target_core_rd.c-			return -ENOMEM;
> drivers/target/target_core_rd.c-
> drivers/target/target_core_rd.c:		sg_init_table(sg, sg_per_table + chain_entry);
> ---
> net/rds/rdma.c-		sg = kcalloc(nents, sizeof(*sg), GFP_KERNEL);
> net/rds/rdma.c-		if (!sg) {
> net/rds/rdma.c-			ret = -ENOMEM;
> net/rds/rdma.c-			goto out;
> net/rds/rdma.c-		}
> net/rds/rdma.c-		WARN_ON(!nents);
> net/rds/rdma.c:		sg_init_table(sg, nents);

I found 16 occurrences in the following files:

net/rds/rdma.c
drivers/infiniband/hw/efa/efa_verbs.c
drivers/net/ethernet/freescale/dpaa2/dpaa2-eth.c
drivers/misc/mic/scif/scif_nodeqp.c
block/bsg-lib.c
drivers/dma/sh/rcar-dmac.c
drivers/spi/spi-topcliff-pch.c
net/sunrpc/xprtrdma/frwr_ops.c
drivers/dma/imx-dma.c
drivers/pci/p2pdma.c
drivers/dma/sh/shdma-base.c
drivers/target/target_core_rd.c
drivers/media/common/saa7146/saa7146_core.c
drivers/tty/serial/pch_uart.c
drivers/net/wireless/intel/iwlwifi/fw/dbg.c

julia
Christophe JAILLET Sept. 20, 2020, 8:54 a.m. UTC | #3
Le 20/09/2020 à 10:32, Julia Lawall a écrit :
> 
> 
> On Sun, 20 Sep 2020, Joe Perches wrote:
> 
>> On Sun, 2020-09-20 at 09:15 +0200, Christophe JAILLET wrote:
>>> 'sgl' is zeroed a few lines below in 'sg_init_table()'. There is no need to
>>> clear it twice.
>>>
>>> Remove the redundant initialization.
>>
>> I didn't look very thoroughly, but there are at least
>> a few more of these with kcalloc and kzalloc like
>>
>> block/bsg-lib.c-        size_t sz = (sizeof(struct scatterlist) * req->nr_phys_segments);
>> block/bsg-lib.c-
>> block/bsg-lib.c-        BUG_ON(!req->nr_phys_segments);
>> block/bsg-lib.c-
>> block/bsg-lib.c-        buf->sg_list = kzalloc(sz, GFP_KERNEL);
>> block/bsg-lib.c-        if (!buf->sg_list)
>> block/bsg-lib.c-                return -ENOMEM;
>> block/bsg-lib.c:        sg_init_table(buf->sg_list, req->nr_phys_segments);
>> ---
>> drivers/target/target_core_rd.c-		sg = kcalloc(sg_per_table + chain_entry, sizeof(*sg),
>> drivers/target/target_core_rd.c-				GFP_KERNEL);
>> drivers/target/target_core_rd.c-		if (!sg)
>> drivers/target/target_core_rd.c-			return -ENOMEM;
>> drivers/target/target_core_rd.c-
>> drivers/target/target_core_rd.c:		sg_init_table(sg, sg_per_table + chain_entry);
>> ---
>> net/rds/rdma.c-		sg = kcalloc(nents, sizeof(*sg), GFP_KERNEL);
>> net/rds/rdma.c-		if (!sg) {
>> net/rds/rdma.c-			ret = -ENOMEM;
>> net/rds/rdma.c-			goto out;
>> net/rds/rdma.c-		}
>> net/rds/rdma.c-		WARN_ON(!nents);
>> net/rds/rdma.c:		sg_init_table(sg, nents);
> 
> I found 16 occurrences in the following files:
> 
> net/rds/rdma.c
> drivers/infiniband/hw/efa/efa_verbs.c
> drivers/net/ethernet/freescale/dpaa2/dpaa2-eth.c
> drivers/misc/mic/scif/scif_nodeqp.c
> block/bsg-lib.c
> drivers/dma/sh/rcar-dmac.c
> drivers/spi/spi-topcliff-pch.c
> net/sunrpc/xprtrdma/frwr_ops.c
> drivers/dma/imx-dma.c
> drivers/pci/p2pdma.c
> drivers/dma/sh/shdma-base.c
> drivers/target/target_core_rd.c
> drivers/media/common/saa7146/saa7146_core.c
> drivers/tty/serial/pch_uart.c
> drivers/net/wireless/intel/iwlwifi/fw/dbg.c
> 
> julia
> 

Also in:
     sound/soc/sprd/sprd-pcm-dma.c
    sound/soc/sprd/sprd-pcm-compress.c
which are a bit tricky. It also uses some, IMHO, pointless devm_ functions.

Feel free to propose patches. I'm not focused on that at the moment.

CJ

Patch
diff mbox series

diff --git a/lib/scatterlist.c b/lib/scatterlist.c
index 5d63a8857f36..d94628fa3349 100644
--- a/lib/scatterlist.c
+++ b/lib/scatterlist.c
@@ -504,7 +504,7 @@  struct scatterlist *sgl_alloc_order(unsigned long long length,
 		nalloc++;
 	}
 	sgl = kmalloc_array(nalloc, sizeof(struct scatterlist),
-			    (gfp & ~GFP_DMA) | __GFP_ZERO);
+			    gfp & ~GFP_DMA);
 	if (!sgl)
 		return NULL;