linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
To: "Gustavo A. R. Silva" <gustavoars@kernel.org>
Cc: "Martin K. Petersen" <martin.petersen@oracle.com>,
	"James E.J. Bottomley" <jejb@linux.ibm.com>,
	GR-QLogic-Storage-Upstream@marvell.com,
	Nilesh Javali <njavali@marvell.com>,
	linux-scsi@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-hardening@vger.kernel.org
Subject: Re: [PATCH 1/2][next] scsi: qla2xxx: Replace one-element array with DECLARE_FLEX_ARRAY() helper
Date: Sat, 19 Nov 2022 11:23:25 +0100	[thread overview]
Message-ID: <629bac45-32c2-9eac-6fed-f2f9e39d2740@wanadoo.fr> (raw)
In-Reply-To: <Y3iaLCY68E6Stgrp@work>

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


  reply	other threads:[~2022-11-19 10:28 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-18 23:46 [PATCH 0/2][next] scsi: qla2xxx: Replace one-element array with flexible-array member Gustavo A. R. Silva
2022-11-18 23:47 ` [PATCH 1/2][next] scsi: qla2xxx: Replace one-element array with DECLARE_FLEX_ARRAY() helper Gustavo A. R. Silva
2022-11-19  7:09   ` Kees Cook
2022-11-19  8:44   ` Christophe JAILLET
2022-11-19  8:56     ` Gustavo A. R. Silva
2022-11-19 10:23       ` Christophe JAILLET [this message]
2022-11-19 19:03         ` Gustavo A. R. Silva
2022-11-18 23:47 ` [PATCH 2/2][next] scsi: qla2xxx: Use struct_size() in code related to struct ct_sns_gpnft_rsp Gustavo A. R. Silva
2022-11-19  7:10   ` Kees Cook
2022-11-19  7:20     ` Gustavo A. R. Silva
2022-11-19  7:49       ` Gustavo A. R. Silva

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=629bac45-32c2-9eac-6fed-f2f9e39d2740@wanadoo.fr \
    --to=christophe.jaillet@wanadoo.fr \
    --cc=GR-QLogic-Storage-Upstream@marvell.com \
    --cc=gustavoars@kernel.org \
    --cc=jejb@linux.ibm.com \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-scsi@vger.kernel.org \
    --cc=martin.petersen@oracle.com \
    --cc=njavali@marvell.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).