diff mbox series

[next] drm/nouveau: Avoid -Wflex-array-member-not-at-end warning

Message ID ZsZLFS1CsHkKjw+C@elsanto (mailing list archive)
State New
Headers show
Series [next] drm/nouveau: Avoid -Wflex-array-member-not-at-end warning | expand

Commit Message

Gustavo A. R. Silva Aug. 21, 2024, 8:16 p.m. UTC
Use the `DEFINE_RAW_FLEX()` helper for an on-stack definition of
a flexible structure where the size of the flexible-array member
is known at compile-time, and refactor the rest of the code,
accordingly.

So, with this, fix the following warning:

drivers/gpu/drm/nouveau/dispnv50/disp.c:779:47: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]

Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
---
 drivers/gpu/drm/nouveau/dispnv50/disp.c | 20 +++++++++-----------
 1 file changed, 9 insertions(+), 11 deletions(-)

Comments

Kees Cook Aug. 22, 2024, 5:27 p.m. UTC | #1
On Wed, Aug 21, 2024 at 02:16:21PM -0600, Gustavo A. R. Silva wrote:
> Use the `DEFINE_RAW_FLEX()` helper for an on-stack definition of
> a flexible structure where the size of the flexible-array member
> is known at compile-time, and refactor the rest of the code,
> accordingly.
> 
> So, with this, fix the following warning:
> 
> drivers/gpu/drm/nouveau/dispnv50/disp.c:779:47: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
> 
> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
> ---
>  drivers/gpu/drm/nouveau/dispnv50/disp.c | 20 +++++++++-----------
>  1 file changed, 9 insertions(+), 11 deletions(-)
> 
> diff --git a/drivers/gpu/drm/nouveau/dispnv50/disp.c b/drivers/gpu/drm/nouveau/dispnv50/disp.c
> index eed579a6c858..ddddc69640be 100644
> --- a/drivers/gpu/drm/nouveau/dispnv50/disp.c
> +++ b/drivers/gpu/drm/nouveau/dispnv50/disp.c
> @@ -774,11 +774,9 @@ nv50_hdmi_enable(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc,
>  	struct drm_hdmi_info *hdmi = &nv_connector->base.display_info.hdmi;
>  	union hdmi_infoframe infoframe = { 0 };
>  	const u8 rekey = 56; /* binary driver, and tegra, constant */
> +	DEFINE_RAW_FLEX(struct nvif_outp_infoframe_v0, args, data, 17);
> +	const u8 data_len = 17; /* same length as in DEFINE_RAW_FLEX above. */

To avoid repeating the open-coded "17", this could either be a define:

nv50_hdmi_enable(...)
{
...
#define data_len	17
	DEFINE_RAW_FLEX(struct nvif_outp_infoframe_v0, args, data, data_len);
...rest of function...
#undef data_len
}

or an ungainly but compile-time calculated value that exposes some
DEFINE_FLEX internals:

	const u8 data_len = (sizeof(args_u) - sizeof(*args)) / sizeof(*args->data);

(Maybe a helper is needed for that?)

#define STACK_FLEX_COUNT(name, member)	\
	((sizeof(name##_u) = sizeof(*(name))) / sizeof(*(name)->member))

> @@ -815,29 +813,29 @@ nv50_hdmi_enable(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc,
>  		return;
>  
>  	/* AVI InfoFrame. */
> -	args.infoframe.version = 0;
> -	args.infoframe.head = nv_crtc->index;
> +	args->version = 0;
> +	args->head = nv_crtc->index;

The stack variable (was before and is again) already zero-initialized,
so the "= 0" line shouldn't be needed.

But neither of these comments are show-stoppers, IMO.

Reviewed-by: Kees Cook <kees@kernel.org>
Gustavo A. R. Silva Aug. 27, 2024, 7:09 p.m. UTC | #2
On 22/08/24 11:27, Kees Cook wrote:
> On Wed, Aug 21, 2024 at 02:16:21PM -0600, Gustavo A. R. Silva wrote:
>> Use the `DEFINE_RAW_FLEX()` helper for an on-stack definition of
>> a flexible structure where the size of the flexible-array member
>> is known at compile-time, and refactor the rest of the code,
>> accordingly.
>>
>> So, with this, fix the following warning:
>>
>> drivers/gpu/drm/nouveau/dispnv50/disp.c:779:47: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
>>
>> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
>> ---
>>   drivers/gpu/drm/nouveau/dispnv50/disp.c | 20 +++++++++-----------
>>   1 file changed, 9 insertions(+), 11 deletions(-)
>>
>> diff --git a/drivers/gpu/drm/nouveau/dispnv50/disp.c b/drivers/gpu/drm/nouveau/dispnv50/disp.c
>> index eed579a6c858..ddddc69640be 100644
>> --- a/drivers/gpu/drm/nouveau/dispnv50/disp.c
>> +++ b/drivers/gpu/drm/nouveau/dispnv50/disp.c
>> @@ -774,11 +774,9 @@ nv50_hdmi_enable(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc,
>>   	struct drm_hdmi_info *hdmi = &nv_connector->base.display_info.hdmi;
>>   	union hdmi_infoframe infoframe = { 0 };
>>   	const u8 rekey = 56; /* binary driver, and tegra, constant */
>> +	DEFINE_RAW_FLEX(struct nvif_outp_infoframe_v0, args, data, 17);
>> +	const u8 data_len = 17; /* same length as in DEFINE_RAW_FLEX above. */
> 
> To avoid repeating the open-coded "17", this could either be a define:
> 
> nv50_hdmi_enable(...)
> {
> ...
> #define data_len	17
> 	DEFINE_RAW_FLEX(struct nvif_outp_infoframe_v0, args, data, data_len);
> ...rest of function...
> #undef data_len
> }
> 
> or an ungainly but compile-time calculated value that exposes some
> DEFINE_FLEX internals:
> 
> 	const u8 data_len = (sizeof(args_u) - sizeof(*args)) / sizeof(*args->data);

Yeah, I actually thought of something more like just __struct_size(args) - sizeof(*args),
as the flex array member is `__u8 data[]`.

> 
> (Maybe a helper is needed for that?)
> 
> #define STACK_FLEX_COUNT(name, member)	\
> 	((sizeof(name##_u) = sizeof(*(name))) / sizeof(*(name)->member))

I don't like this `sizeof(name##_u)` part as it is detached from the DEFINE_RAW_FLEX()
internals. Probably use `__struct_size(args)` instead, as in the example above.

> 
>> @@ -815,29 +813,29 @@ nv50_hdmi_enable(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc,
>>   		return;
>>   
>>   	/* AVI InfoFrame. */
>> -	args.infoframe.version = 0;
>> -	args.infoframe.head = nv_crtc->index;
>> +	args->version = 0;
>> +	args->head = nv_crtc->index;
> 
> The stack variable (was before and is again) already zero-initialized,
> so the "= 0" line shouldn't be needed.
> 
> But neither of these comments are show-stoppers, IMO.
> 
> Reviewed-by: Kees Cook <kees@kernel.org>
> 

Thanks!

--
Gustavo
Gustavo A. R. Silva Sept. 13, 2024, 8:09 a.m. UTC | #3
Hi all,

Friendly ping: who can take this, please? 
Danilo Krummrich Sept. 13, 2024, 10:23 a.m. UTC | #4
Hi,

On 9/13/24 10:09 AM, Gustavo A. R. Silva wrote:
> Hi all,
> 
> Friendly ping: who can take this, please? 
diff mbox series

Patch

diff --git a/drivers/gpu/drm/nouveau/dispnv50/disp.c b/drivers/gpu/drm/nouveau/dispnv50/disp.c
index eed579a6c858..ddddc69640be 100644
--- a/drivers/gpu/drm/nouveau/dispnv50/disp.c
+++ b/drivers/gpu/drm/nouveau/dispnv50/disp.c
@@ -774,11 +774,9 @@  nv50_hdmi_enable(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc,
 	struct drm_hdmi_info *hdmi = &nv_connector->base.display_info.hdmi;
 	union hdmi_infoframe infoframe = { 0 };
 	const u8 rekey = 56; /* binary driver, and tegra, constant */
+	DEFINE_RAW_FLEX(struct nvif_outp_infoframe_v0, args, data, 17);
+	const u8 data_len = 17; /* same length as in DEFINE_RAW_FLEX above. */
 	u32 max_ac_packet;
-	struct {
-		struct nvif_outp_infoframe_v0 infoframe;
-		u8 data[17];
-	} args = { 0 };
 	int ret, size;
 
 	max_ac_packet  = mode->htotal - mode->hdisplay;
@@ -815,29 +813,29 @@  nv50_hdmi_enable(struct drm_encoder *encoder, struct nouveau_crtc *nv_crtc,
 		return;
 
 	/* AVI InfoFrame. */
-	args.infoframe.version = 0;
-	args.infoframe.head = nv_crtc->index;
+	args->version = 0;
+	args->head = nv_crtc->index;
 
 	if (!drm_hdmi_avi_infoframe_from_display_mode(&infoframe.avi, &nv_connector->base, mode)) {
 		drm_hdmi_avi_infoframe_quant_range(&infoframe.avi, &nv_connector->base, mode,
 						   HDMI_QUANTIZATION_RANGE_FULL);
 
-		size = hdmi_infoframe_pack(&infoframe, args.data, ARRAY_SIZE(args.data));
+		size = hdmi_infoframe_pack(&infoframe, args->data, data_len);
 	} else {
 		size = 0;
 	}
 
-	nvif_outp_infoframe(&nv_encoder->outp, NVIF_OUTP_INFOFRAME_V0_AVI, &args.infoframe, size);
+	nvif_outp_infoframe(&nv_encoder->outp, NVIF_OUTP_INFOFRAME_V0_AVI, args, size);
 
 	/* Vendor InfoFrame. */
-	memset(&args.data, 0, sizeof(args.data));
+	memset(args->data, 0, data_len);
 	if (!drm_hdmi_vendor_infoframe_from_display_mode(&infoframe.vendor.hdmi,
 							 &nv_connector->base, mode))
-		size = hdmi_infoframe_pack(&infoframe, args.data, ARRAY_SIZE(args.data));
+		size = hdmi_infoframe_pack(&infoframe, args->data, data_len);
 	else
 		size = 0;
 
-	nvif_outp_infoframe(&nv_encoder->outp, NVIF_OUTP_INFOFRAME_V0_VSI, &args.infoframe, size);
+	nvif_outp_infoframe(&nv_encoder->outp, NVIF_OUTP_INFOFRAME_V0_VSI, args, size);
 
 	nv_encoder->hdmi.enabled = true;
 }