* [PATCH 0/2] drm/imagination: Fixes for flexible structures
@ 2025-07-09 10:04 Matt Coster
2025-07-09 10:04 ` [PATCH 1/2] drm/imagination: Add and use FLEX_ARRAY_CHECK() Matt Coster
2025-07-09 10:04 ` [PATCH 2/2] drm/imagination: Use struct_size_t() Matt Coster
0 siblings, 2 replies; 5+ messages in thread
From: Matt Coster @ 2025-07-09 10:04 UTC (permalink / raw)
To: Frank Binns, Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann,
David Airlie, Simona Vetter
Cc: dri-devel, linux-kernel, Matt Coster
These fixes were made as part of investigating the Sparse issue reported
in [1]; specifically that the sizeof() check for the flexible structure
struct rogue_fwif_frag_ctx_state was failing under Sparse.
This is actually a fairly pointless check, and the discrepancy is
related to the implicit padding at the end of the structure that is
never used due to the flexible array member. A far more useful check is
to assert that the final member is indeed a flexible array as expected,
and that one element of said array is of the size that we expect.
While fixing that, I stumbled on the useful struct_size_t() macro and
put it to good use wherever the size of the structure in question is
computed.
[1]: https://lore.kernel.org/r/20250606-sprase-reasoning-comments-v1-1-433c0ff11a09@imgtec.com
Signed-off-by: Matt Coster <matt.coster@imgtec.com>
---
Matt Coster (2):
drm/imagination: Add and use FLEX_ARRAY_CHECK()
drm/imagination: Use struct_size_t()
drivers/gpu/drm/imagination/pvr_queue.c | 11 +++++------
drivers/gpu/drm/imagination/pvr_rogue_fwif_check.h | 19 ++++++++++++++++++-
2 files changed, 23 insertions(+), 7 deletions(-)
---
base-commit: fe88fb3421161f3abd974ee2ecbe2d9195f98812
change-id: 20250704-flex-array-check-ac9c3b6f130e
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 1/2] drm/imagination: Add and use FLEX_ARRAY_CHECK()
2025-07-09 10:04 [PATCH 0/2] drm/imagination: Fixes for flexible structures Matt Coster
@ 2025-07-09 10:04 ` Matt Coster
2025-07-15 15:48 ` Alessio Belle
2025-07-09 10:04 ` [PATCH 2/2] drm/imagination: Use struct_size_t() Matt Coster
1 sibling, 1 reply; 5+ messages in thread
From: Matt Coster @ 2025-07-09 10:04 UTC (permalink / raw)
To: Frank Binns, Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann,
David Airlie, Simona Vetter
Cc: dri-devel, linux-kernel, Matt Coster
It makes little to no sense to use SIZE_CHECK() on flexible structures, so
let's validate something that actually matters instead.
Signed-off-by: Matt Coster <matt.coster@imgtec.com>
---
drivers/gpu/drm/imagination/pvr_rogue_fwif_check.h | 19 ++++++++++++++++++-
1 file changed, 18 insertions(+), 1 deletion(-)
diff --git a/drivers/gpu/drm/imagination/pvr_rogue_fwif_check.h b/drivers/gpu/drm/imagination/pvr_rogue_fwif_check.h
index 51dc37e78f41d7bdf45d1f434dd1aa5b9eca700a..e72f4064af187e2be3e26722e1ee1ac632087d3d 100644
--- a/drivers/gpu/drm/imagination/pvr_rogue_fwif_check.h
+++ b/drivers/gpu/drm/imagination/pvr_rogue_fwif_check.h
@@ -5,6 +5,8 @@
#define PVR_ROGUE_FWIF_CHECK_H
#include <linux/build_bug.h>
+#include <linux/overflow.h>
+#include <linux/stddef.h>
#define OFFSET_CHECK(type, member, offset) \
static_assert(offsetof(type, member) == (offset), \
@@ -13,6 +15,21 @@
#define SIZE_CHECK(type, size) \
static_assert(sizeof(type) == (size), #type " is incorrect size")
+/*
+ * Where the last member of a struct is a flexible array member, using
+ * SIZE_CHECK() is pointless. If the structure is not already padded to
+ * alignment without the flexible array member, sizeof() will not match the
+ * offset of the flexible array member and the "correct" sizeof() value is
+ * completely meaningless.
+ *
+ * In those instances, use FLEX_ARRAY_CHECK() instead to assert that the final
+ * field is a flexible array member and that it behaves as expected.
+ */
+#define FLEX_ARRAY_CHECK(type, member) \
+ static_assert(flex_array_size((type *)NULL, member, 1) == \
+ sizeof_field(type, member[0]), \
+ #type "->" #member " is incorrect size")
+
OFFSET_CHECK(struct rogue_fwif_file_info_buf, path, 0);
OFFSET_CHECK(struct rogue_fwif_file_info_buf, info, 200);
OFFSET_CHECK(struct rogue_fwif_file_info_buf, line_num, 400);
@@ -157,7 +174,7 @@ OFFSET_CHECK(struct rogue_fwif_frag_ctx_state, frag_reg_pm_deallocated_mask_stat
OFFSET_CHECK(struct rogue_fwif_frag_ctx_state, frag_reg_dm_pds_mtilefree_status, 4);
OFFSET_CHECK(struct rogue_fwif_frag_ctx_state, ctx_state_flags, 8);
OFFSET_CHECK(struct rogue_fwif_frag_ctx_state, frag_reg_isp_store, 12);
-SIZE_CHECK(struct rogue_fwif_frag_ctx_state, 16);
+FLEX_ARRAY_CHECK(struct rogue_fwif_frag_ctx_state, frag_reg_isp_store);
OFFSET_CHECK(struct rogue_fwif_compute_ctx_state, ctx_state_flags, 0);
SIZE_CHECK(struct rogue_fwif_compute_ctx_state, 4);
--
2.50.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH 2/2] drm/imagination: Use struct_size_t()
2025-07-09 10:04 [PATCH 0/2] drm/imagination: Fixes for flexible structures Matt Coster
2025-07-09 10:04 ` [PATCH 1/2] drm/imagination: Add and use FLEX_ARRAY_CHECK() Matt Coster
@ 2025-07-09 10:04 ` Matt Coster
2025-07-15 16:28 ` Alessio Belle
1 sibling, 1 reply; 5+ messages in thread
From: Matt Coster @ 2025-07-09 10:04 UTC (permalink / raw)
To: Frank Binns, Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann,
David Airlie, Simona Vetter
Cc: dri-devel, linux-kernel, Matt Coster
The helpers for dealing with flexible structures exist, so let's use them.
Signed-off-by: Matt Coster <matt.coster@imgtec.com>
---
drivers/gpu/drm/imagination/pvr_queue.c | 11 +++++------
1 file changed, 5 insertions(+), 6 deletions(-)
diff --git a/drivers/gpu/drm/imagination/pvr_queue.c b/drivers/gpu/drm/imagination/pvr_queue.c
index 5a41ee79fed646a86344cd16e78efdb45ff02e43..094a854576a5a63f56e56acdebf01bdf542ae4d5 100644
--- a/drivers/gpu/drm/imagination/pvr_queue.c
+++ b/drivers/gpu/drm/imagination/pvr_queue.c
@@ -3,6 +3,7 @@
#include <drm/drm_managed.h>
#include <drm/gpu_scheduler.h>
+#include <linux/overflow.h>
#include "pvr_cccb.h"
#include "pvr_context.h"
@@ -35,9 +36,8 @@ static int get_xfer_ctx_state_size(struct pvr_device *pvr_dev)
return err;
}
- return sizeof(struct rogue_fwif_frag_ctx_state) +
- (num_isp_store_registers *
- sizeof(((struct rogue_fwif_frag_ctx_state *)0)->frag_reg_isp_store[0]));
+ return struct_size_t(struct rogue_fwif_frag_ctx_state,
+ frag_reg_isp_store, num_isp_store_registers);
}
static int get_frag_ctx_state_size(struct pvr_device *pvr_dev)
@@ -65,9 +65,8 @@ static int get_frag_ctx_state_size(struct pvr_device *pvr_dev)
return err;
}
- return sizeof(struct rogue_fwif_frag_ctx_state) +
- (num_isp_store_registers *
- sizeof(((struct rogue_fwif_frag_ctx_state *)0)->frag_reg_isp_store[0]));
+ return struct_size_t(struct rogue_fwif_frag_ctx_state,
+ frag_reg_isp_store, num_isp_store_registers);
}
static int get_ctx_state_size(struct pvr_device *pvr_dev, enum drm_pvr_job_type type)
--
2.50.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH 1/2] drm/imagination: Add and use FLEX_ARRAY_CHECK()
2025-07-09 10:04 ` [PATCH 1/2] drm/imagination: Add and use FLEX_ARRAY_CHECK() Matt Coster
@ 2025-07-15 15:48 ` Alessio Belle
0 siblings, 0 replies; 5+ messages in thread
From: Alessio Belle @ 2025-07-15 15:48 UTC (permalink / raw)
To: Matt Coster
Cc: tzimmermann@suse.de, simona@ffwll.ch,
dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org,
Frank Binns, maarten.lankhorst@linux.intel.com,
mripard@kernel.org, airlied@gmail.com
On Wed, 2025-07-09 at 11:04 +0100, Matt Coster wrote:
> It makes little to no sense to use SIZE_CHECK() on flexible structures, so
> let's validate something that actually matters instead.
>
> Signed-off-by: Matt Coster <matt.coster@imgtec.com>
Reviewed-by: Alessio Belle <alessio.belle@imgtec.com>
Thanks,
Alessio
> ---
> drivers/gpu/drm/imagination/pvr_rogue_fwif_check.h | 19 ++++++++++++++++++-
> 1 file changed, 18 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/gpu/drm/imagination/pvr_rogue_fwif_check.h b/drivers/gpu/drm/imagination/pvr_rogue_fwif_check.h
> index 51dc37e78f41d7bdf45d1f434dd1aa5b9eca700a..e72f4064af187e2be3e26722e1ee1ac632087d3d 100644
> --- a/drivers/gpu/drm/imagination/pvr_rogue_fwif_check.h
> +++ b/drivers/gpu/drm/imagination/pvr_rogue_fwif_check.h
> @@ -5,6 +5,8 @@
> #define PVR_ROGUE_FWIF_CHECK_H
>
> #include <linux/build_bug.h>
> +#include <linux/overflow.h>
> +#include <linux/stddef.h>
>
> #define OFFSET_CHECK(type, member, offset) \
> static_assert(offsetof(type, member) == (offset), \
> @@ -13,6 +15,21 @@
> #define SIZE_CHECK(type, size) \
> static_assert(sizeof(type) == (size), #type " is incorrect size")
>
> +/*
> + * Where the last member of a struct is a flexible array member, using
> + * SIZE_CHECK() is pointless. If the structure is not already padded to
> + * alignment without the flexible array member, sizeof() will not match the
> + * offset of the flexible array member and the "correct" sizeof() value is
> + * completely meaningless.
> + *
> + * In those instances, use FLEX_ARRAY_CHECK() instead to assert that the final
> + * field is a flexible array member and that it behaves as expected.
> + */
> +#define FLEX_ARRAY_CHECK(type, member) \
> + static_assert(flex_array_size((type *)NULL, member, 1) == \
> + sizeof_field(type, member[0]), \
> + #type "->" #member " is incorrect size")
> +
> OFFSET_CHECK(struct rogue_fwif_file_info_buf, path, 0);
> OFFSET_CHECK(struct rogue_fwif_file_info_buf, info, 200);
> OFFSET_CHECK(struct rogue_fwif_file_info_buf, line_num, 400);
> @@ -157,7 +174,7 @@ OFFSET_CHECK(struct rogue_fwif_frag_ctx_state, frag_reg_pm_deallocated_mask_stat
> OFFSET_CHECK(struct rogue_fwif_frag_ctx_state, frag_reg_dm_pds_mtilefree_status, 4);
> OFFSET_CHECK(struct rogue_fwif_frag_ctx_state, ctx_state_flags, 8);
> OFFSET_CHECK(struct rogue_fwif_frag_ctx_state, frag_reg_isp_store, 12);
> -SIZE_CHECK(struct rogue_fwif_frag_ctx_state, 16);
> +FLEX_ARRAY_CHECK(struct rogue_fwif_frag_ctx_state, frag_reg_isp_store);
>
> OFFSET_CHECK(struct rogue_fwif_compute_ctx_state, ctx_state_flags, 0);
> SIZE_CHECK(struct rogue_fwif_compute_ctx_state, 4);
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 2/2] drm/imagination: Use struct_size_t()
2025-07-09 10:04 ` [PATCH 2/2] drm/imagination: Use struct_size_t() Matt Coster
@ 2025-07-15 16:28 ` Alessio Belle
0 siblings, 0 replies; 5+ messages in thread
From: Alessio Belle @ 2025-07-15 16:28 UTC (permalink / raw)
To: Matt Coster
Cc: tzimmermann@suse.de, simona@ffwll.ch,
dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org,
Frank Binns, maarten.lankhorst@linux.intel.com,
mripard@kernel.org, airlied@gmail.com
On Wed, 2025-07-09 at 11:04 +0100, Matt Coster wrote:
> The helpers for dealing with flexible structures exist, so let's use them.
>
> Signed-off-by: Matt Coster <matt.coster@imgtec.com>
Reviewed-by: Alessio Belle <alessio.belle@imgtec.com>
Thanks,
Alessio
> ---
> drivers/gpu/drm/imagination/pvr_queue.c | 11 +++++------
> 1 file changed, 5 insertions(+), 6 deletions(-)
>
> diff --git a/drivers/gpu/drm/imagination/pvr_queue.c b/drivers/gpu/drm/imagination/pvr_queue.c
> index 5a41ee79fed646a86344cd16e78efdb45ff02e43..094a854576a5a63f56e56acdebf01bdf542ae4d5 100644
> --- a/drivers/gpu/drm/imagination/pvr_queue.c
> +++ b/drivers/gpu/drm/imagination/pvr_queue.c
> @@ -3,6 +3,7 @@
>
> #include <drm/drm_managed.h>
> #include <drm/gpu_scheduler.h>
> +#include <linux/overflow.h>
>
> #include "pvr_cccb.h"
> #include "pvr_context.h"
> @@ -35,9 +36,8 @@ static int get_xfer_ctx_state_size(struct pvr_device *pvr_dev)
> return err;
> }
>
> - return sizeof(struct rogue_fwif_frag_ctx_state) +
> - (num_isp_store_registers *
> - sizeof(((struct rogue_fwif_frag_ctx_state *)0)->frag_reg_isp_store[0]));
> + return struct_size_t(struct rogue_fwif_frag_ctx_state,
> + frag_reg_isp_store, num_isp_store_registers);
> }
>
> static int get_frag_ctx_state_size(struct pvr_device *pvr_dev)
> @@ -65,9 +65,8 @@ static int get_frag_ctx_state_size(struct pvr_device *pvr_dev)
> return err;
> }
>
> - return sizeof(struct rogue_fwif_frag_ctx_state) +
> - (num_isp_store_registers *
> - sizeof(((struct rogue_fwif_frag_ctx_state *)0)->frag_reg_isp_store[0]));
> + return struct_size_t(struct rogue_fwif_frag_ctx_state,
> + frag_reg_isp_store, num_isp_store_registers);
> }
>
> static int get_ctx_state_size(struct pvr_device *pvr_dev, enum drm_pvr_job_type type)
>
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2025-07-15 16:29 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-07-09 10:04 [PATCH 0/2] drm/imagination: Fixes for flexible structures Matt Coster
2025-07-09 10:04 ` [PATCH 1/2] drm/imagination: Add and use FLEX_ARRAY_CHECK() Matt Coster
2025-07-15 15:48 ` Alessio Belle
2025-07-09 10:04 ` [PATCH 2/2] drm/imagination: Use struct_size_t() Matt Coster
2025-07-15 16:28 ` Alessio Belle
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).