diff mbox series

[2/2,next] scsi: qla2xxx: Use struct_size() in code related to struct ct_sns_gpnft_rsp

Message ID 9bd4775fe9c88b33c3194f841a2ec2f559d58032.1668814746.git.gustavoars@kernel.org (mailing list archive)
State Changes Requested
Headers show
Series scsi: qla2xxx: Replace one-element array with flexible-array member | expand

Commit Message

Gustavo A. R. Silva Nov. 18, 2022, 11:47 p.m. UTC
Prefer struct_size() over open-coded versions of idiom:

sizeof(struct-with-flex-array) + sizeof(typeof-flex-array-elements) * count

where count is the max number of items the flexible array is supposed to
contain.

Link: https://github.com/KSPP/linux/issues/160
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
---
 drivers/scsi/qla2xxx/qla_gs.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

Comments

Kees Cook Nov. 19, 2022, 7:10 a.m. UTC | #1
On Fri, Nov 18, 2022 at 05:47:56PM -0600, Gustavo A. R. Silva wrote:
> Prefer struct_size() over open-coded versions of idiom:
> 
> sizeof(struct-with-flex-array) + sizeof(typeof-flex-array-elements) * count
> 
> where count is the max number of items the flexible array is supposed to
> contain.
> 
> Link: https://github.com/KSPP/linux/issues/160
> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
> ---
>  drivers/scsi/qla2xxx/qla_gs.c | 5 ++---
>  1 file changed, 2 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/scsi/qla2xxx/qla_gs.c b/drivers/scsi/qla2xxx/qla_gs.c
> index 69d3bc795f90..27e1df56b0fb 100644
> --- a/drivers/scsi/qla2xxx/qla_gs.c
> +++ b/drivers/scsi/qla2xxx/qla_gs.c
> @@ -4072,9 +4072,8 @@ int qla24xx_async_gpnft(scsi_qla_host_t *vha, u8 fc4_type, srb_t *sp)
>  		}
>  		sp->u.iocb_cmd.u.ctarg.req_size = GPN_FT_REQ_SIZE;
>  
> -		rspsz = sizeof(struct ct_sns_gpnft_rsp) +
> -			(vha->hw->max_fibre_devices *
> -			    sizeof(struct ct_sns_gpn_ft_data));
> +		rspsz = struct_size((struct ct_sns_gpnft_rsp *)0, entries,
> +				vha->hw->max_fibre_devices);

This should be able to use sp->u.iocb_cmd.u.ctarg.rsp instead of the
explicit struct with a NULL. (It's just using typeof() internally, so
it's okay that it isn't allocated yet.)

-Kees

>  
>  		sp->u.iocb_cmd.u.ctarg.rsp = dma_alloc_coherent(&vha->hw->pdev->dev,
>  								rspsz,
> -- 
> 2.34.1
>
Gustavo A. R. Silva Nov. 19, 2022, 7:20 a.m. UTC | #2
On Fri, Nov 18, 2022 at 11:10:44PM -0800, Kees Cook wrote:
> On Fri, Nov 18, 2022 at 05:47:56PM -0600, Gustavo A. R. Silva wrote:
> > Prefer struct_size() over open-coded versions of idiom:
> > 
> > sizeof(struct-with-flex-array) + sizeof(typeof-flex-array-elements) * count
> > 
> > where count is the max number of items the flexible array is supposed to
> > contain.
> > 
> > Link: https://github.com/KSPP/linux/issues/160
> > Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
> > ---
> >  drivers/scsi/qla2xxx/qla_gs.c | 5 ++---
> >  1 file changed, 2 insertions(+), 3 deletions(-)
> > 
> > diff --git a/drivers/scsi/qla2xxx/qla_gs.c b/drivers/scsi/qla2xxx/qla_gs.c
> > index 69d3bc795f90..27e1df56b0fb 100644
> > --- a/drivers/scsi/qla2xxx/qla_gs.c
> > +++ b/drivers/scsi/qla2xxx/qla_gs.c
> > @@ -4072,9 +4072,8 @@ int qla24xx_async_gpnft(scsi_qla_host_t *vha, u8 fc4_type, srb_t *sp)
> >  		}
> >  		sp->u.iocb_cmd.u.ctarg.req_size = GPN_FT_REQ_SIZE;
> >  
> > -		rspsz = sizeof(struct ct_sns_gpnft_rsp) +
> > -			(vha->hw->max_fibre_devices *
> > -			    sizeof(struct ct_sns_gpn_ft_data));
> > +		rspsz = struct_size((struct ct_sns_gpnft_rsp *)0, entries,
> > +				vha->hw->max_fibre_devices);
> 
> This should be able to use sp->u.iocb_cmd.u.ctarg.rsp instead of the
> explicit struct with a NULL. (It's just using typeof() internally, so
> it's okay that it isn't allocated yet.)

mmh... yeah; and considering they're already going all the way down to
sp->u.iocb_cmd.u.ctarg.req_size, I think accessing sp->u.iocb_cmd.u.ctarg.rsp
is perfectly fine. :)

I'll respin. Thanks for the feedback!
--
Gustavo

> 
> -Kees
> 
> >  
> >  		sp->u.iocb_cmd.u.ctarg.rsp = dma_alloc_coherent(&vha->hw->pdev->dev,
> >  								rspsz,
> > -- 
> > 2.34.1
> > 
> 
> -- 
> Kees Cook
Gustavo A. R. Silva Nov. 19, 2022, 7:49 a.m. UTC | #3
On Sat, Nov 19, 2022 at 01:20:09AM -0600, Gustavo A. R. Silva wrote:
> On Fri, Nov 18, 2022 at 11:10:44PM -0800, Kees Cook wrote:
> > On Fri, Nov 18, 2022 at 05:47:56PM -0600, Gustavo A. R. Silva wrote:
> > > Prefer struct_size() over open-coded versions of idiom:
> > > 
> > > sizeof(struct-with-flex-array) + sizeof(typeof-flex-array-elements) * count
> > > 
> > > where count is the max number of items the flexible array is supposed to
> > > contain.
> > > 
> > > Link: https://github.com/KSPP/linux/issues/160
> > > Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
> > > ---
> > >  drivers/scsi/qla2xxx/qla_gs.c | 5 ++---
> > >  1 file changed, 2 insertions(+), 3 deletions(-)
> > > 
> > > diff --git a/drivers/scsi/qla2xxx/qla_gs.c b/drivers/scsi/qla2xxx/qla_gs.c
> > > index 69d3bc795f90..27e1df56b0fb 100644
> > > --- a/drivers/scsi/qla2xxx/qla_gs.c
> > > +++ b/drivers/scsi/qla2xxx/qla_gs.c
> > > @@ -4072,9 +4072,8 @@ int qla24xx_async_gpnft(scsi_qla_host_t *vha, u8 fc4_type, srb_t *sp)
> > >  		}
> > >  		sp->u.iocb_cmd.u.ctarg.req_size = GPN_FT_REQ_SIZE;
> > >  
> > > -		rspsz = sizeof(struct ct_sns_gpnft_rsp) +
> > > -			(vha->hw->max_fibre_devices *
> > > -			    sizeof(struct ct_sns_gpn_ft_data));
> > > +		rspsz = struct_size((struct ct_sns_gpnft_rsp *)0, entries,
> > > +				vha->hw->max_fibre_devices);
> > 
> > This should be able to use sp->u.iocb_cmd.u.ctarg.rsp instead of the
> > explicit struct with a NULL. (It's just using typeof() internally, so
> > it's okay that it isn't allocated yet.)
> 
> mmh... yeah; and considering they're already going all the way down to
> sp->u.iocb_cmd.u.ctarg.req_size, I think accessing sp->u.iocb_cmd.u.ctarg.rsp
> is perfectly fine. :)

except that... it seems sp->u.iocb_cmd.u.ctarg.rsp is a pointer to void
and not a struct of type struct ct_sns_gpnft_rsp. :O

drivers/scsi/qla2xxx/qla_def.h:474:
struct ct_arg {
        void            *iocb;
        u16             nport_handle;
        dma_addr_t      req_dma;
        dma_addr_t      rsp_dma;
        u32             req_size;
        u32             rsp_size;
        u32             req_allocated_size;
        u32             rsp_allocated_size;
        void            *req;
        void            *rsp;
        port_id_t       id;
};

I wonder if you wanted to point out something entirely different...?

--
Gustavo
diff mbox series

Patch

diff --git a/drivers/scsi/qla2xxx/qla_gs.c b/drivers/scsi/qla2xxx/qla_gs.c
index 69d3bc795f90..27e1df56b0fb 100644
--- a/drivers/scsi/qla2xxx/qla_gs.c
+++ b/drivers/scsi/qla2xxx/qla_gs.c
@@ -4072,9 +4072,8 @@  int qla24xx_async_gpnft(scsi_qla_host_t *vha, u8 fc4_type, srb_t *sp)
 		}
 		sp->u.iocb_cmd.u.ctarg.req_size = GPN_FT_REQ_SIZE;
 
-		rspsz = sizeof(struct ct_sns_gpnft_rsp) +
-			(vha->hw->max_fibre_devices *
-			    sizeof(struct ct_sns_gpn_ft_data));
+		rspsz = struct_size((struct ct_sns_gpnft_rsp *)0, entries,
+				vha->hw->max_fibre_devices);
 
 		sp->u.iocb_cmd.u.ctarg.rsp = dma_alloc_coherent(&vha->hw->pdev->dev,
 								rspsz,