diff mbox series

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

Message ID Z_ABiwj7hoXR0fJ5@kspp (mailing list archive)
State New
Headers show
Series [v2,next] drm/nouveau: fifo: Avoid -Wflex-array-member-not-at-end warning | expand

Commit Message

Gustavo A. R. Silva April 4, 2025, 3:58 p.m. UTC
-Wflex-array-member-not-at-end was introduced in GCC-14, and we are
getting ready to enable it, globally.

Adjust heap allocation to account for the flex-array-in-the-middle
issue, and refactor the rest of the code accordingly.

So, with these changes, fix the following warning:

drivers/gpu/drm/nouveau/nvif/fifo.c:29:42: 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>
---
Changes in v2:
 - Adjust heap allocation instead of using the DEFINE_RAW_FLEX() helper.
 - Link: https://lore.kernel.org/lkml/202504041254.6e26LBdj-lkp@intel.com/

v1:
 - Link: https://lore.kernel.org/linux-hardening/Z-7IQcWNePAMQEM0@kspp/

 drivers/gpu/drm/nouveau/nvif/fifo.c | 35 +++++++++++++++--------------
 1 file changed, 18 insertions(+), 17 deletions(-)

Comments

Kees Cook April 7, 2025, 8:09 p.m. UTC | #1
On Fri, Apr 04, 2025 at 09:58:03AM -0600, Gustavo A. R. Silva wrote:
> -Wflex-array-member-not-at-end was introduced in GCC-14, and we are
> getting ready to enable it, globally.
> 
> Adjust heap allocation to account for the flex-array-in-the-middle
> issue, and refactor the rest of the code accordingly.
> 
> So, with these changes, fix the following warning:
> 
> drivers/gpu/drm/nouveau/nvif/fifo.c:29:42: 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>
> ---
> Changes in v2:
>  - Adjust heap allocation instead of using the DEFINE_RAW_FLEX() helper.
>  - Link: https://lore.kernel.org/lkml/202504041254.6e26LBdj-lkp@intel.com/
> 
> v1:
>  - Link: https://lore.kernel.org/linux-hardening/Z-7IQcWNePAMQEM0@kspp/
> 
>  drivers/gpu/drm/nouveau/nvif/fifo.c | 35 +++++++++++++++--------------
>  1 file changed, 18 insertions(+), 17 deletions(-)
> 
> diff --git a/drivers/gpu/drm/nouveau/nvif/fifo.c b/drivers/gpu/drm/nouveau/nvif/fifo.c
> index a463289962b2..2c887dffa45d 100644
> --- a/drivers/gpu/drm/nouveau/nvif/fifo.c
> +++ b/drivers/gpu/drm/nouveau/nvif/fifo.c
> @@ -25,33 +25,34 @@ static int
>  nvif_fifo_runlists(struct nvif_device *device)
>  {
>  	struct nvif_object *object = &device->object;
> -	struct {
> -		struct nv_device_info_v1 m;
> -		struct {
> -			struct nv_device_info_v1_data runlists;
> -			struct nv_device_info_v1_data runlist[64];
> -		} v;
> -	} *a;
> +	struct nv_device_info_v1_data *runlists;
> +	struct nv_device_info_v1_data *runlist;
> +	struct nv_device_info_v1 *a;
> +	const u8 runlist_cnt = 64;
>  	int ret, i;
>  
>  	if (device->runlist)
>  		return 0;
>  
> -	if (!(a = kmalloc(sizeof(*a), GFP_KERNEL)))
> +	a = kmalloc(struct_size(a, data, runlist_cnt + 1), GFP_KERNEL);

Hrm, a pre-existing issue, but this is dumping quite a few uninitialized
bytes into this allocation, as struct nv_device_info_v1::pad is never
initialized and runlists->data is never initialized. It looks like these
bytes are being written out to the video card? Or some kind of ioctl?

This should really be using kzalloc() or making sure stuff isn't
uninitialized.

> +	if (!a)
>  		return -ENOMEM;
> -	a->m.version = 1;
> -	a->m.count = sizeof(a->v) / sizeof(a->v.runlists);
> -	a->v.runlists.mthd = NV_DEVICE_HOST_RUNLISTS;
> -	for (i = 0; i < ARRAY_SIZE(a->v.runlist); i++) {
> -		a->v.runlist[i].mthd = NV_DEVICE_HOST_RUNLIST_ENGINES;
> -		a->v.runlist[i].data = i;
> +
> +	runlists = &a->data[0];
> +	runlist = &a->data[1];
> +	a->version = 1;
> +	a->count = runlist_cnt + 1;

Seems struct nv_device_info_v1::count is the counted_by for struct
nv_device_info_v1::data?

> +	runlists->mthd = NV_DEVICE_HOST_RUNLISTS;
> +	for (i = 0; i < runlist_cnt; i++) {
> +		runlist[i].mthd = NV_DEVICE_HOST_RUNLIST_ENGINES;
> +		runlist[i].data = i;
>  	}
>  
>  	ret = nvif_object_mthd(object, NV_DEVICE_V0_INFO, a, sizeof(*a));
>  	if (ret)
>  		goto done;
>  
> -	device->runlists = fls64(a->v.runlists.data);
> +	device->runlists = fls64(runlists->data);

I assume nvif_object_mthd() writes runlists->data?

>  	device->runlist = kcalloc(device->runlists, sizeof(*device->runlist),
>  				  GFP_KERNEL);
>  	if (!device->runlist) {
> @@ -60,8 +61,8 @@ nvif_fifo_runlists(struct nvif_device *device)
>  	}
>  
>  	for (i = 0; i < device->runlists; i++) {
> -		if (a->v.runlist[i].mthd != NV_DEVICE_INFO_INVALID)
> -			device->runlist[i].engines = a->v.runlist[i].data;
> +		if (runlist[i].mthd != NV_DEVICE_INFO_INVALID)
> +			device->runlist[i].engines = runlist[i].data;
>  	}
>  
>  done:
> -- 
> 2.43.0
>
diff mbox series

Patch

diff --git a/drivers/gpu/drm/nouveau/nvif/fifo.c b/drivers/gpu/drm/nouveau/nvif/fifo.c
index a463289962b2..2c887dffa45d 100644
--- a/drivers/gpu/drm/nouveau/nvif/fifo.c
+++ b/drivers/gpu/drm/nouveau/nvif/fifo.c
@@ -25,33 +25,34 @@  static int
 nvif_fifo_runlists(struct nvif_device *device)
 {
 	struct nvif_object *object = &device->object;
-	struct {
-		struct nv_device_info_v1 m;
-		struct {
-			struct nv_device_info_v1_data runlists;
-			struct nv_device_info_v1_data runlist[64];
-		} v;
-	} *a;
+	struct nv_device_info_v1_data *runlists;
+	struct nv_device_info_v1_data *runlist;
+	struct nv_device_info_v1 *a;
+	const u8 runlist_cnt = 64;
 	int ret, i;
 
 	if (device->runlist)
 		return 0;
 
-	if (!(a = kmalloc(sizeof(*a), GFP_KERNEL)))
+	a = kmalloc(struct_size(a, data, runlist_cnt + 1), GFP_KERNEL);
+	if (!a)
 		return -ENOMEM;
-	a->m.version = 1;
-	a->m.count = sizeof(a->v) / sizeof(a->v.runlists);
-	a->v.runlists.mthd = NV_DEVICE_HOST_RUNLISTS;
-	for (i = 0; i < ARRAY_SIZE(a->v.runlist); i++) {
-		a->v.runlist[i].mthd = NV_DEVICE_HOST_RUNLIST_ENGINES;
-		a->v.runlist[i].data = i;
+
+	runlists = &a->data[0];
+	runlist = &a->data[1];
+	a->version = 1;
+	a->count = runlist_cnt + 1;
+	runlists->mthd = NV_DEVICE_HOST_RUNLISTS;
+	for (i = 0; i < runlist_cnt; i++) {
+		runlist[i].mthd = NV_DEVICE_HOST_RUNLIST_ENGINES;
+		runlist[i].data = i;
 	}
 
 	ret = nvif_object_mthd(object, NV_DEVICE_V0_INFO, a, sizeof(*a));
 	if (ret)
 		goto done;
 
-	device->runlists = fls64(a->v.runlists.data);
+	device->runlists = fls64(runlists->data);
 	device->runlist = kcalloc(device->runlists, sizeof(*device->runlist),
 				  GFP_KERNEL);
 	if (!device->runlist) {
@@ -60,8 +61,8 @@  nvif_fifo_runlists(struct nvif_device *device)
 	}
 
 	for (i = 0; i < device->runlists; i++) {
-		if (a->v.runlist[i].mthd != NV_DEVICE_INFO_INVALID)
-			device->runlist[i].engines = a->v.runlist[i].data;
+		if (runlist[i].mthd != NV_DEVICE_INFO_INVALID)
+			device->runlist[i].engines = runlist[i].data;
 	}
 
 done: