2014-04-08 20:42:39

by Matei Oprea

[permalink] [raw]
Subject: [PATCH] scsi: lpfc: lpfc_init: use kcalloc for allocating memory

It's easier to use kcalloc for allocating arrays. While at it
also remove useless casting value.

Signed-off-by: Matei Oprea <[email protected]>
Cc: ROSEdu Kernel Community <[email protected]>
---
drivers/scsi/lpfc/lpfc_init.c | 14 ++++++++------
1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/drivers/scsi/lpfc/lpfc_init.c b/drivers/scsi/lpfc/lpfc_init.c
index 635eeb3..1528f34 100644
--- a/drivers/scsi/lpfc/lpfc_init.c
+++ b/drivers/scsi/lpfc/lpfc_init.c
@@ -4742,12 +4742,14 @@ lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
phba->cfg_sg_seg_cnt = LPFC_DEFAULT_MENLO_SG_SEG_CNT;
}

- if (!phba->sli.ring)
- phba->sli.ring = (struct lpfc_sli_ring *)
- kzalloc(LPFC_SLI3_MAX_RING *
- sizeof(struct lpfc_sli_ring), GFP_KERNEL);
- if (!phba->sli.ring)
- return -ENOMEM;
+ if (unlikely(!phba->sli.ring)) {
+ phba->sli.ring = kcalloc(LPFC_SLI3_MAX_RING,
+ sizeof(struct lpfc_sli_ring),
+ GFP_KERNEL);
+
+ if (!phba->sli.ring)
+ return -ENOMEM;
+ }

/*
* Since lpfc_sg_seg_cnt is module parameter, the sg_dma_buf_size
--
1.9.1


2014-04-09 00:04:31

by Joe Perches

[permalink] [raw]
Subject: Re: [PATCH] scsi: lpfc: lpfc_init: use kcalloc for allocating memory

On Tue, 2014-04-08 at 23:42 +0300, Matei Oprea wrote:
> It's easier to use kcalloc for allocating arrays. While at it
> also remove useless casting value.
[]
> diff --git a/drivers/scsi/lpfc/lpfc_init.c b/drivers/scsi/lpfc/lpfc_init.c
[]
> @@ -4742,12 +4742,14 @@ lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
[]
> - if (!phba->sli.ring)
[]
> + if (unlikely(!phba->sli.ring)) {

Why add the unlikely?

Glancing at the code it seems more like that this
should be likely than unlikely.

> + phba->sli.ring = kcalloc(LPFC_SLI3_MAX_RING,
> + sizeof(struct lpfc_sli_ring),
> + GFP_KERNEL);

It'd be nicer looking if aligned better

phba->sli.ring = kcalloc(LPFC_SLI3_MAX_RING,
sizeof(struct lpfc_sli_ring),
GFP_KERNEL);

2014-04-09 10:48:36

by Boaz Harrosh

[permalink] [raw]
Subject: Re: [PATCH] scsi: lpfc: lpfc_init: use kcalloc for allocating memory

On 04/08/2014 11:42 PM, Matei Oprea wrote:
> It's easier to use kcalloc for allocating arrays. While at it
> also remove useless casting value.
>
> Signed-off-by: Matei Oprea <[email protected]>
> Cc: ROSEdu Kernel Community <[email protected]>
> ---
> drivers/scsi/lpfc/lpfc_init.c | 14 ++++++++------
> 1 file changed, 8 insertions(+), 6 deletions(-)
>
> diff --git a/drivers/scsi/lpfc/lpfc_init.c b/drivers/scsi/lpfc/lpfc_init.c
> index 635eeb3..1528f34 100644
> --- a/drivers/scsi/lpfc/lpfc_init.c
> +++ b/drivers/scsi/lpfc/lpfc_init.c
> @@ -4742,12 +4742,14 @@ lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
> phba->cfg_sg_seg_cnt = LPFC_DEFAULT_MENLO_SG_SEG_CNT;
> }
>
> - if (!phba->sli.ring)
> - phba->sli.ring = (struct lpfc_sli_ring *)
> - kzalloc(LPFC_SLI3_MAX_RING *
> - sizeof(struct lpfc_sli_ring), GFP_KERNEL);
> - if (!phba->sli.ring)
> - return -ENOMEM;
> + if (unlikely(!phba->sli.ring)) {

As Joe Perches said this one is not unlikely, it might or it might not
depending on state.

> + phba->sli.ring = kcalloc(LPFC_SLI3_MAX_RING,
> + sizeof(struct lpfc_sli_ring),
> + GFP_KERNEL);
> +
> + if (!phba->sli.ring)

but this one is unlikely. This is the error path so we want
the hot path to be decoded by the CPU.

> + return -ENOMEM;
> + }
>
> /*
> * Since lpfc_sg_seg_cnt is module parameter, the sg_dma_buf_size
>

Cheers
Boaz