drm/imagination: Fixes for flexible structures
Subsystems (3)
cover drm/imagination: Fixes for flexible structures
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/[email protected]
---
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
PATCH 1/2 drm/imagination: Add and use FLEX_ARRAY_CHECK()
It makes little to no sense to use SIZE_CHECK() on flexible structures, so let's validate something that actually matters instead.
--- drivers/gpu/drm/imagination/pvr_rogue_fwif_check.h | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-)
drivers/gpu/drm/imagination/pvr_rogue_fwif_check.h +18 -1
@@ -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);
Signature · 1 line
2.50.1
Re: [PATCH 1/2] drm/imagination: Add and use FLEX_ARRAY_CHECK()
Quoted 4 lines · 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 <[email protected]>
Thanks,
Alessio
Quoted 49 lines
--- drivers/gpu/drm/imagination/pvr_rogue_fwif_check.h | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-)drivers/gpu/drm/imagination/pvr_rogue_fwif_check.h +18
-1@@ -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);
PATCH 2/2 drm/imagination: Use struct_size_t()
In reply to Matt CosterThe helpers for dealing with flexible structures exist, so let's use them.
--- drivers/gpu/drm/imagination/pvr_queue.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-)
drivers/gpu/drm/imagination/pvr_queue.c +5 -6
@@ -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)
Signature · 1 line
2.50.1
Re: [PATCH 2/2] drm/imagination: Use struct_size_t()
Quoted 3 lines · Matt Coster
The helpers for dealing with flexible structures exist, so let's use them.
Signed-off-by Matt Coster <[email protected]>
Thanks,
Alessio
Quoted 41 lines
--- drivers/gpu/drm/imagination/pvr_queue.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-)drivers/gpu/drm/imagination/pvr_queue.c +5
-6@@ -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)
Quoted 11 lines · 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 structrogue_fwif_frag_ctx_statewas 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.
[...]
Applied, thanks!
[1/2] drm/imagination: Add and use FLEX_ARRAY_CHECK()
commit: f41ef8e3c65f1c424004493f7792ce06daa147df
[2/2] drm/imagination: Use struct_size_t()
commit: b67ac31d7f42b2b2e9ca77c66bbbb3646f039103Best regards,
Signature · 1 line
Alessio Belle <[email protected]>