scsi: qla2xxx: Replace one-element array with flexible-array member
Subsystems (2)
cover scsi: qla2xxx: Replace one-element array with flexible-array member
Hi!
This series aims to replace a one-element array with flexible-array member in drivers/scsi/qla2xxx/qla_def.h through the use of the DECLARE_FLEX_ARRAY() helper.
This helps with the ongoing efforts to tighten the FORTIFY_SOURCE routines on memcpy() and help us make progress towards globally enabling -fstrict-flex-arrays=3 [1].
Gustavo A. R. Silva (2):
scsi: qla2xxx: Replace one-element array with DECLARE_FLEX_ARRAY()
helper
scsi: qla2xxx: Use struct_size() in code related to struct
ct_sns_gpnft_rsp
drivers/scsi/qla2xxx/qla_def.h | 4 ++-- drivers/scsi/qla2xxx/qla_gs.c | 5 ++--- 2 files changed, 4 insertions(+), 5 deletions(-)
Signature · 1 line
2.34.1
PATCH 1/2 scsi: qla2xxx: Replace one-element array with DECLARE_FLEX_ARRAY() helper
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].
--- drivers/scsi/qla2xxx/qla_def.h | 4 ++-- drivers/scsi/qla2xxx/qla_gs.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-)
drivers/scsi/qla2xxx/qla_def.h +2 -2
@@ -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 */
drivers/scsi/qla2xxx/qla_gs.c +1 -1
@@ -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,
Signature · 1 line
2.34.1
Re: [PATCH 1/2][next] scsi: qla2xxx: Replace one-element array with DECLARE_FLEX_ARRAY() helper
Quoted 16 lines · Gustavo A. R. Silva
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 structct_sns_gpnft_pkt{
3241 union {
3242 structct_sns_reqreq;
3243 structct_sns_gpnft_rsprsp;
3244 } p;
3245 };Important to mention is that doing a build before/after this patch results in no binary differences.
Quoted 7 lines
This help us make progress towards globally enabling -fstrict-flex-arrays=3 [1].
Signature · 1 line
Kees Cook
Re: [PATCH 1/2][next] scsi: qla2xxx: Replace one-element array with DECLARE_FLEX_ARRAY() helper
In reply to Gustavo A. R. SilvaLe 19/11/2022 à 00:47, Gustavo A. R. Silva a écrit :
Quoted 13 lines
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 structct_sns_gpnft_pkt{
3241 union {
3242 structct_sns_reqreq;
3243 structct_sns_gpnft_rsprsp;
3244 } p;
3245 };
Quoted 2 lines
Important to mention is that doing a build before/after this patch results in no binary differences.
Hi,
even with the:
drivers/scsi/qla2xxx/qla_gs.c int qla24xx_async_gpnft(scsi_qla_host_t *vha, u8 fc4_type, srb_t *sp) · line 4077
Quoted 4 lines
rspsz =
sizeof(struct ct_sns_gpnft_rsp)+drivers/scsi/qla2xxx/qla_gs.c +1
-1- ((vha->hw->max_fibre_devices -1 ) * + (vha->hw->max_fibre_devices *sizeof (struct ct_sns_gpn_ft_data ));
change ?
CJ
Quoted 45 lines
This help us make progress towards globally enabling -fstrict-flex-arrays=3 [1].
Signed-off-by Gustavo A. R. Silva <[email protected]>--- drivers/scsi/qla2xxx/qla_def.h | 4 ++-- drivers/scsi/qla2xxx/qla_gs.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-)drivers/scsi/qla2xxx/qla_def.h +2
-2@@ -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 */ drivers/scsi/qla2xxx/qla_gs.c +1
-1@@ -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,
Re: [PATCH 1/2][next] scsi: qla2xxx: Replace one-element array with DECLARE_FLEX_ARRAY() helper
Quoted 19 lines · Christophe JAILLET
Le 19/11/2022 à 00:47, Gustavo A. R. Silva a écrit :
Quoted 13 lines
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 structct_sns_gpnft_pkt{
3241 union {
3242 structct_sns_reqreq;
3243 structct_sns_gpnft_rsprsp;
3244 } p;
3245 };Quoted 2 lines
Important to mention is that doing a build before/after this patch results in no binary differences.
Hi,
Quoted 8 lines
even with the:
drivers/scsi/qla2xxx/qla_gs.c int qla24xx_async_gpnft(scsi_qla_host_t *vha, u8 fc4_type, srb_t *sp) · line 4077
Quoted 4 lines
rspsz =
sizeof(struct ct_sns_gpnft_rsp)+drivers/scsi/qla2xxx/qla_gs.c +1
-1- ((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
Quoted 46 lines
CJ
Quoted 42 lines
This help us make progress towards globally enabling -fstrict-flex-arrays=3 [1].
Signed-off-by Gustavo A. R. Silva <[email protected]>--- drivers/scsi/qla2xxx/qla_def.h | 4 ++-- drivers/scsi/qla2xxx/qla_gs.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-)drivers/scsi/qla2xxx/qla_def.h +2
-2@@ -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 */ drivers/scsi/qla2xxx/qla_gs.c +1
-1@@ -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,
Re: [PATCH 1/2][next] scsi: qla2xxx: Replace one-element array with DECLARE_FLEX_ARRAY() helper
Le 19/11/2022 à 09:56, Gustavo A. R. Silva a écrit :
Quoted 29 lines · Christophe JAILLET
Quoted 19 lines
Le 19/11/2022 à 00:47, Gustavo A. R. Silva a écrit :
Quoted 13 lines
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 structct_sns_gpnft_pkt{
3241 union {
3242 structct_sns_reqreq;
3243 structct_sns_gpnft_rsprsp;
3244 } p;
3245 };Quoted 2 lines
Important to mention is that doing a build before/after this patch results in no binary differences.
Hi,
Quoted 8 lines
even with the:
drivers/scsi/qla2xxx/qla_gs.c int qla24xx_async_gpnft(scsi_qla_host_t *vha, u8 fc4_type, srb_t *sp) · line 4077
Quoted 4 lines
rspsz =
sizeof(struct ct_sns_gpnft_rsp)+drivers/scsi/qla2xxx/qla_gs.c +1
-1- ((vha->hw->max_fibre_devices -1 ) * + (vha->hw->max_fibre_devices *sizeof (struct ct_sns_gpn_ft_data ));change ?
Quoted 8 lines
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 structct_sns_gpn_ft_datainsizeof(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
Quoted 50 lines
--
GustavoQuoted 46 lines
CJ
Quoted 42 lines
This help us make progress towards globally enabling -fstrict-flex-arrays=3 [1].
Signed-off-by Gustavo A. R. Silva <[email protected]>--- drivers/scsi/qla2xxx/qla_def.h | 4 ++-- drivers/scsi/qla2xxx/qla_gs.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-)drivers/scsi/qla2xxx/qla_def.h +2
-2@@ -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 */ drivers/scsi/qla2xxx/qla_gs.c +1
-1@@ -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,
Re: [PATCH 1/2][next] scsi: qla2xxx: Replace one-element array with DECLARE_FLEX_ARRAY() helper
Quoted 2 lines
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
PATCH 2/2 scsi: qla2xxx: Use struct_size() in code related to struct ct_sns_gpnft_rsp
In reply to Gustavo A. R. SilvaPrefer 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.
--- drivers/scsi/qla2xxx/qla_gs.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/scsi/qla2xxx/qla_gs.c +2 -3
@@ -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,
Signature · 1 line
2.34.1
Re: [PATCH 2/2][next] scsi: qla2xxx: Use struct_size() in code related to struct ct_sns_gpnft_rsp
Quoted 18 lines · Gustavo A. R. Silva
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.
--- drivers/scsi/qla2xxx/qla_gs.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)drivers/scsi/qla2xxx/qla_gs.c +0
-0
drivers/scsi/qla2xxx/qla_gs.c int qla24xx_async_gpnft(scsi_qla_host_t *vha, u8 fc4_type, srb_t *sp) · line 4076
Quoted 8 lines
drivers/scsi/qla2xxx/qla_gs.c +2
-3@@ -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
Quoted 6 lines
drivers/scsi/qla2xxx/qla_gs.c +0
-0sp->u.iocb_cmd.u.ctarg.rsp = dma_alloc_coherent(&vha->hw->pdev->dev, rspsz,Signature · 1 line
2.34.1
Signature · 1 line
Kees Cook
Re: [PATCH 2/2][next] scsi: qla2xxx: Use struct_size() in code related to struct ct_sns_gpnft_rsp
Quoted 22 lines · Kees Cook
Quoted 21 lines · Gustavo A. R. Silva
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.
--- drivers/scsi/qla2xxx/qla_gs.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)drivers/scsi/qla2xxx/qla_gs.c +0
-0@@ -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 ;
Quoted 9 lines
drivers/scsi/qla2xxx/qla_gs.c int qla24xx_async_gpnft(scsi_qla_host_t *vha, u8 fc4_type, srb_t *sp) · line 4076
Quoted 5 lines
drivers/scsi/qla2xxx/qla_gs.c +2
-3- 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.rspinstead of the explicit struct with a NULL. (It's just usingtypeof()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
Quoted 12 lines
-Kees
Quoted 6 lines
drivers/scsi/qla2xxx/qla_gs.c +0
-0sp->u.iocb_cmd.u.ctarg.rsp = dma_alloc_coherent(&vha->hw->pdev->dev, rspsz,Signature · 1 line
2.34.1Signature · 1 line
Kees Cook
Re: [PATCH 2/2][next] scsi: qla2xxx: Use struct_size() in code related to struct ct_sns_gpnft_rsp
Quoted 33 lines · Gustavo A. R. Silva
Quoted 22 lines · Kees Cook
Quoted 21 lines · Gustavo A. R. Silva
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.
--- drivers/scsi/qla2xxx/qla_gs.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)drivers/scsi/qla2xxx/qla_gs.c +0
-0@@ -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 ;Quoted 9 lines
drivers/scsi/qla2xxx/qla_gs.c int qla24xx_async_gpnft(scsi_qla_host_t *vha, u8 fc4_type, srb_t *sp) · line 4076
Quoted 5 lines
drivers/scsi/qla2xxx/qla_gs.c +2
-3- 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.rspinstead of the explicit struct with a NULL. (It's just usingtypeof()internally, so it's okay that it isn't allocated yet.)
Quoted 3 lines
mmh... yeah; and considering they're already going all the way down to
sp->u.iocb_cmd.u.ctarg.req_size, I think accessingsp->u.iocb_cmd.u.ctarg.rspis 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