diff mbox series

[1/2,next] scsi: qla2xxx: Replace one-element array with DECLARE_FLEX_ARRAY() helper

Message ID faa40e6b31ecc9387ad1644bb1957aa53d7c682f.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
One-element arrays as fake flex arrays are deprecated and we are moving
towards adopting C99 flexible-array members, instead. So, replace
one-element array declaration in struct ct_sns_gpnft_rsp, which is
ultimately being used inside a union:

drivers/scsi/qla2xxx/qla_def.h:
3240 struct ct_sns_gpnft_pkt {
3241         union {
3242                 struct ct_sns_req req;
3243                 struct ct_sns_gpnft_rsp rsp;
3244         } p;
3245 };

Important to mention is that doing a build before/after this patch results
in no binary differences.

This help us make progress towards globally enabling
-fstrict-flex-arrays=3 [1].

Link: https://github.com/KSPP/linux/issues/245
Link: https://github.com/KSPP/linux/issues/193
Link: https://gcc.gnu.org/pipermail/gcc-patches/2022-October/602902.html [1]
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
---
 drivers/scsi/qla2xxx/qla_def.h | 4 ++--
 drivers/scsi/qla2xxx/qla_gs.c  | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)

Comments

Kees Cook Nov. 19, 2022, 7:09 a.m. UTC | #1
On Fri, Nov 18, 2022 at 05:47:13PM -0600, Gustavo A. R. Silva wrote:
> One-element arrays as fake flex arrays are deprecated and we are moving
> towards adopting C99 flexible-array members, instead. So, replace
> one-element array declaration in struct ct_sns_gpnft_rsp, which is
> ultimately being used inside a union:
> 
> drivers/scsi/qla2xxx/qla_def.h:
> 3240 struct ct_sns_gpnft_pkt {
> 3241         union {
> 3242                 struct ct_sns_req req;
> 3243                 struct ct_sns_gpnft_rsp rsp;
> 3244         } p;
> 3245 };
> 
> Important to mention is that doing a build before/after this patch results
> in no binary differences.
> 
> This help us make progress towards globally enabling
> -fstrict-flex-arrays=3 [1].
> 
> Link: https://github.com/KSPP/linux/issues/245
> Link: https://github.com/KSPP/linux/issues/193
> Link: https://gcc.gnu.org/pipermail/gcc-patches/2022-October/602902.html [1]
> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>

Reviewed-by: Kees Cook <keescook@chromium.org>
Christophe JAILLET Nov. 19, 2022, 8:44 a.m. UTC | #2
Le 19/11/2022 à 00:47, Gustavo A. R. Silva a écrit :
> One-element arrays as fake flex arrays are deprecated and we are moving
> towards adopting C99 flexible-array members, instead. So, replace
> one-element array declaration in struct ct_sns_gpnft_rsp, which is
> ultimately being used inside a union:
> 
> drivers/scsi/qla2xxx/qla_def.h:
> 3240 struct ct_sns_gpnft_pkt {
> 3241         union {
> 3242                 struct ct_sns_req req;
> 3243                 struct ct_sns_gpnft_rsp rsp;
> 3244         } p;
> 3245 };
> 
> Important to mention is that doing a build before/after this patch results
> in no binary differences.

Hi,

even with the:

 >   		rspsz = sizeof(struct ct_sns_gpnft_rsp) +
 > -			((vha->hw->max_fibre_devices - 1) *
 > +			(vha->hw->max_fibre_devices *
 >   			    sizeof(struct ct_sns_gpn_ft_data));

change ?

CJ

> 
> This help us make progress towards globally enabling
> -fstrict-flex-arrays=3 [1].
> 
> Link: https://github.com/KSPP/linux/issues/245
> Link: https://github.com/KSPP/linux/issues/193
> Link: https://gcc.gnu.org/pipermail/gcc-patches/2022-October/602902.html [1]
> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
> ---
>   drivers/scsi/qla2xxx/qla_def.h | 4 ++--
>   drivers/scsi/qla2xxx/qla_gs.c  | 2 +-
>   2 files changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/scsi/qla2xxx/qla_def.h b/drivers/scsi/qla2xxx/qla_def.h
> index a26a373be9da..1eea977ef426 100644
> --- a/drivers/scsi/qla2xxx/qla_def.h
> +++ b/drivers/scsi/qla2xxx/qla_def.h
> @@ -3151,12 +3151,12 @@ struct ct_sns_gpnft_rsp {
>   		uint8_t vendor_unique;
>   	};
>   	/* Assume the largest number of targets for the union */
> -	struct ct_sns_gpn_ft_data {
> +	DECLARE_FLEX_ARRAY(struct ct_sns_gpn_ft_data {
>   		u8 control_byte;
>   		u8 port_id[3];
>   		u32 reserved;
>   		u8 port_name[8];
> -	} entries[1];
> +	}, entries);
>   };
>   
>   /* CT command response */
> diff --git a/drivers/scsi/qla2xxx/qla_gs.c b/drivers/scsi/qla2xxx/qla_gs.c
> index 64ab070b8716..69d3bc795f90 100644
> --- a/drivers/scsi/qla2xxx/qla_gs.c
> +++ b/drivers/scsi/qla2xxx/qla_gs.c
> @@ -4073,7 +4073,7 @@ 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 - 1) *
> +			(vha->hw->max_fibre_devices *
>   			    sizeof(struct ct_sns_gpn_ft_data));
>   
>   		sp->u.iocb_cmd.u.ctarg.rsp = dma_alloc_coherent(&vha->hw->pdev->dev,
Gustavo A. R. Silva Nov. 19, 2022, 8:56 a.m. UTC | #3
On Sat, Nov 19, 2022 at 09:44:02AM +0100, Christophe JAILLET wrote:
> Le 19/11/2022 à 00:47, Gustavo A. R. Silva a écrit :
> > One-element arrays as fake flex arrays are deprecated and we are moving
> > towards adopting C99 flexible-array members, instead. So, replace
> > one-element array declaration in struct ct_sns_gpnft_rsp, which is
> > ultimately being used inside a union:
> > 
> > drivers/scsi/qla2xxx/qla_def.h:
> > 3240 struct ct_sns_gpnft_pkt {
> > 3241         union {
> > 3242                 struct ct_sns_req req;
> > 3243                 struct ct_sns_gpnft_rsp rsp;
> > 3244         } p;
> > 3245 };
> > 
> > Important to mention is that doing a build before/after this patch results
> > in no binary differences.
> 
> Hi,
> 
> even with the:
> 
> >   		rspsz = sizeof(struct ct_sns_gpnft_rsp) +
> > -			((vha->hw->max_fibre_devices - 1) *
> > +			(vha->hw->max_fibre_devices *
> >   			    sizeof(struct ct_sns_gpn_ft_data));
> 
> change ?

Yep; that change compensates for the removal of the 1 in the declaration
of entries[].

The above piece of code is a common idiom to calculate the size for an
allocation when a one-element array is involved. In the original code
(vha->hw->max_fibre_devices - 1) compensates for the _extra_ size of one
element of type struct ct_sns_gpn_ft_data in sizeof(struct ct_sns_gpnft_rsp).

--
Gustavo

> 
> CJ
> 
> > 
> > This help us make progress towards globally enabling
> > -fstrict-flex-arrays=3 [1].
> > 
> > Link: https://github.com/KSPP/linux/issues/245
> > Link: https://github.com/KSPP/linux/issues/193
> > Link: https://gcc.gnu.org/pipermail/gcc-patches/2022-October/602902.html [1]
> > Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
> > ---
> >   drivers/scsi/qla2xxx/qla_def.h | 4 ++--
> >   drivers/scsi/qla2xxx/qla_gs.c  | 2 +-
> >   2 files changed, 3 insertions(+), 3 deletions(-)
> > 
> > diff --git a/drivers/scsi/qla2xxx/qla_def.h b/drivers/scsi/qla2xxx/qla_def.h
> > index a26a373be9da..1eea977ef426 100644
> > --- a/drivers/scsi/qla2xxx/qla_def.h
> > +++ b/drivers/scsi/qla2xxx/qla_def.h
> > @@ -3151,12 +3151,12 @@ struct ct_sns_gpnft_rsp {
> >   		uint8_t vendor_unique;
> >   	};
> >   	/* Assume the largest number of targets for the union */
> > -	struct ct_sns_gpn_ft_data {
> > +	DECLARE_FLEX_ARRAY(struct ct_sns_gpn_ft_data {
> >   		u8 control_byte;
> >   		u8 port_id[3];
> >   		u32 reserved;
> >   		u8 port_name[8];
> > -	} entries[1];
> > +	}, entries);
> >   };
> >   /* CT command response */
> > diff --git a/drivers/scsi/qla2xxx/qla_gs.c b/drivers/scsi/qla2xxx/qla_gs.c
> > index 64ab070b8716..69d3bc795f90 100644
> > --- a/drivers/scsi/qla2xxx/qla_gs.c
> > +++ b/drivers/scsi/qla2xxx/qla_gs.c
> > @@ -4073,7 +4073,7 @@ 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 - 1) *
> > +			(vha->hw->max_fibre_devices *
> >   			    sizeof(struct ct_sns_gpn_ft_data));
> >   		sp->u.iocb_cmd.u.ctarg.rsp = dma_alloc_coherent(&vha->hw->pdev->dev,
>
Christophe JAILLET Nov. 19, 2022, 10:23 a.m. UTC | #4
Le 19/11/2022 à 09:56, Gustavo A. R. Silva a écrit :
> On Sat, Nov 19, 2022 at 09:44:02AM +0100, Christophe JAILLET wrote:
>> Le 19/11/2022 à 00:47, Gustavo A. R. Silva a écrit :
>>> One-element arrays as fake flex arrays are deprecated and we are moving
>>> towards adopting C99 flexible-array members, instead. So, replace
>>> one-element array declaration in struct ct_sns_gpnft_rsp, which is
>>> ultimately being used inside a union:
>>>
>>> drivers/scsi/qla2xxx/qla_def.h:
>>> 3240 struct ct_sns_gpnft_pkt {
>>> 3241         union {
>>> 3242                 struct ct_sns_req req;
>>> 3243                 struct ct_sns_gpnft_rsp rsp;
>>> 3244         } p;
>>> 3245 };
>>>
>>> Important to mention is that doing a build before/after this patch results
>>> in no binary differences.
>>
>> Hi,
>>
>> even with the:
>>
>>>    		rspsz = sizeof(struct ct_sns_gpnft_rsp) +
>>> -			((vha->hw->max_fibre_devices - 1) *
>>> +			(vha->hw->max_fibre_devices *
>>>    			    sizeof(struct ct_sns_gpn_ft_data));
>>
>> change ?
> 
> Yep; that change compensates for the removal of the 1 in the declaration
> of entries[].
> 
> The above piece of code is a common idiom to calculate the size for an
> allocation when a one-element array is involved. In the original code
> (vha->hw->max_fibre_devices - 1) compensates for the _extra_ size of one
> element of type struct ct_sns_gpn_ft_data in sizeof(struct ct_sns_gpnft_rsp).
> 

Yes, I do agree, that the code is equivalent. I was surprised that a 
compiler was smart enough to generate the same binary code.

With gcc 11.3.0 (x86_64), I do get some differences when I do:
    make drivers/scsi/qla2xxx/qla_gs.o
    objdump -D  drivers/scsi/qla2xxx/qla_gs.o > before.asm

    patch -p1 < patch.diff

    make drivers/scsi/qla2xxx/qla_gs.o
    objdump -D  drivers/scsi/qla2xxx/qla_gs.o > after.asm

    diff -u before.asm after.asm

Mostly some slight ordering of instruction changes, but the binary are 
not the same.

CJ

> --
> Gustavo
> 
>>
>> CJ
>>
>>>
>>> This help us make progress towards globally enabling
>>> -fstrict-flex-arrays=3 [1].
>>>
>>> Link: https://github.com/KSPP/linux/issues/245
>>> Link: https://github.com/KSPP/linux/issues/193
>>> Link: https://gcc.gnu.org/pipermail/gcc-patches/2022-October/602902.html [1]
>>> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
>>> ---
>>>    drivers/scsi/qla2xxx/qla_def.h | 4 ++--
>>>    drivers/scsi/qla2xxx/qla_gs.c  | 2 +-
>>>    2 files changed, 3 insertions(+), 3 deletions(-)
>>>
>>> diff --git a/drivers/scsi/qla2xxx/qla_def.h b/drivers/scsi/qla2xxx/qla_def.h
>>> index a26a373be9da..1eea977ef426 100644
>>> --- a/drivers/scsi/qla2xxx/qla_def.h
>>> +++ b/drivers/scsi/qla2xxx/qla_def.h
>>> @@ -3151,12 +3151,12 @@ struct ct_sns_gpnft_rsp {
>>>    		uint8_t vendor_unique;
>>>    	};
>>>    	/* Assume the largest number of targets for the union */
>>> -	struct ct_sns_gpn_ft_data {
>>> +	DECLARE_FLEX_ARRAY(struct ct_sns_gpn_ft_data {
>>>    		u8 control_byte;
>>>    		u8 port_id[3];
>>>    		u32 reserved;
>>>    		u8 port_name[8];
>>> -	} entries[1];
>>> +	}, entries);
>>>    };
>>>    /* CT command response */
>>> diff --git a/drivers/scsi/qla2xxx/qla_gs.c b/drivers/scsi/qla2xxx/qla_gs.c
>>> index 64ab070b8716..69d3bc795f90 100644
>>> --- a/drivers/scsi/qla2xxx/qla_gs.c
>>> +++ b/drivers/scsi/qla2xxx/qla_gs.c
>>> @@ -4073,7 +4073,7 @@ 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 - 1) *
>>> +			(vha->hw->max_fibre_devices *
>>>    			    sizeof(struct ct_sns_gpn_ft_data));
>>>    		sp->u.iocb_cmd.u.ctarg.rsp = dma_alloc_coherent(&vha->hw->pdev->dev,
>>
>
Gustavo A. R. Silva Nov. 19, 2022, 7:03 p.m. UTC | #5
> Yes, I do agree, that the code is equivalent. I was surprised that a
> compiler was smart enough to generate the same binary code.

We discard the tiny changes that don't affect the logic or the control
flow of the program.

--
Gustavo
diff mbox series

Patch

diff --git a/drivers/scsi/qla2xxx/qla_def.h b/drivers/scsi/qla2xxx/qla_def.h
index a26a373be9da..1eea977ef426 100644
--- a/drivers/scsi/qla2xxx/qla_def.h
+++ b/drivers/scsi/qla2xxx/qla_def.h
@@ -3151,12 +3151,12 @@  struct ct_sns_gpnft_rsp {
 		uint8_t vendor_unique;
 	};
 	/* Assume the largest number of targets for the union */
-	struct ct_sns_gpn_ft_data {
+	DECLARE_FLEX_ARRAY(struct ct_sns_gpn_ft_data {
 		u8 control_byte;
 		u8 port_id[3];
 		u32 reserved;
 		u8 port_name[8];
-	} entries[1];
+	}, entries);
 };
 
 /* CT command response */
diff --git a/drivers/scsi/qla2xxx/qla_gs.c b/drivers/scsi/qla2xxx/qla_gs.c
index 64ab070b8716..69d3bc795f90 100644
--- a/drivers/scsi/qla2xxx/qla_gs.c
+++ b/drivers/scsi/qla2xxx/qla_gs.c
@@ -4073,7 +4073,7 @@  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 - 1) *
+			(vha->hw->max_fibre_devices *
 			    sizeof(struct ct_sns_gpn_ft_data));
 
 		sp->u.iocb_cmd.u.ctarg.rsp = dma_alloc_coherent(&vha->hw->pdev->dev,