Skip to contentSkip to conversation

scsi: qla2xxx: Replace one-element array with flexible-array member

PATCH Superseded Gustavo A. R. Silva 3 participants 11 messages Nov 19, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

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
#1 lore raw

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
index a26a373be9da..1eea977ef426 100644
@@ -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
index 64ab070b8716..69d3bc795f90 100644
@@ -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
#7 lore raw

PATCH 2/2 scsi: qla2xxx: Use struct_size() in code related to struct ct_sns_gpnft_rsp

In reply to 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 +2 -3
index 69d3bc795f90..27e1df56b0fb 100644
@@ -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
Reviewed-by on v1 1/2
#2 lore raw

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

In reply to Gustavo A. R. Silva
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 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.

Quoted 7 lines

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

Signature · 1 line
Kees Cook
#8 lore raw

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

In reply to Gustavo A. R. Silva
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
index 69d3bc795f90..27e1df56b0fb 100644

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 -0
 		sp->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
#9 lore raw

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
index 69d3bc795f90..27e1df56b0fb 100644
@@ -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.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

Quoted 12 lines

-Kees

Quoted 6 lines
drivers/scsi/qla2xxx/qla_gs.c +0 -0
 		sp->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
#10 lore raw

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
index 69d3bc795f90..27e1df56b0fb 100644
@@ -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.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.)

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 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

#3 lore raw

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

In reply to Gustavo A. R. Silva

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 struct ct_sns_gpnft_pkt {
3241 union {
3242 struct ct_sns_req req;
3243 struct ct_sns_gpnft_rsp rsp;
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].

---
  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
index a26a373be9da..1eea977ef426 100644
@@ -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
index 64ab070b8716..69d3bc795f90 100644
@@ -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,
#4 lore raw

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 struct ct_sns_gpnft_pkt {
3241 union {
3242 struct ct_sns_req req;
3243 struct ct_sns_gpnft_rsp rsp;
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].

---
  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
index a26a373be9da..1eea977ef426 100644
@@ -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
index 64ab070b8716..69d3bc795f90 100644
@@ -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,
#5 lore raw

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 struct ct_sns_gpnft_pkt {
3241 union {
3242 struct ct_sns_req req;
3243 struct ct_sns_gpnft_rsp rsp;
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 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

Quoted 50 lines

--
Gustavo

Quoted 46 lines

CJ

Quoted 42 lines

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
index a26a373be9da..1eea977ef426 100644
@@ -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
index 64ab070b8716..69d3bc795f90 100644
@@ -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,
#6 lore raw

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