dri-devel.lists.freedesktop.org archive mirror
 help / color / mirror / Atom feed
From: Matt Coster <matt.coster@imgtec.com>
To: Frank Binns <frank.binns@imgtec.com>,
	Maarten Lankhorst <maarten.lankhorst@linux.intel.com>,
	Maxime Ripard <mripard@kernel.org>,
	Thomas Zimmermann <tzimmermann@suse.de>,
	David Airlie <airlied@gmail.com>, Simona Vetter <simona@ffwll.ch>
Cc: <dri-devel@lists.freedesktop.org>, <linux-kernel@vger.kernel.org>,
	"Matt Coster" <matt.coster@imgtec.com>
Subject: [PATCH 1/2] drm/imagination: Add and use FLEX_ARRAY_CHECK()
Date: Wed, 9 Jul 2025 11:04:09 +0100	[thread overview]
Message-ID: <20250709-flex-array-check-v1-1-8adeb0bf0cde@imgtec.com> (raw)
In-Reply-To: <20250709-flex-array-check-v1-0-8adeb0bf0cde@imgtec.com>

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


  reply	other threads:[~2025-07-09 10:04 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-09 10:04 [PATCH 0/2] drm/imagination: Fixes for flexible structures Matt Coster
2025-07-09 10:04 ` Matt Coster [this message]
2025-07-15 15:48   ` [PATCH 1/2] drm/imagination: Add and use FLEX_ARRAY_CHECK() 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

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=20250709-flex-array-check-v1-1-8adeb0bf0cde@imgtec.com \
    --to=matt.coster@imgtec.com \
    --cc=airlied@gmail.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=frank.binns@imgtec.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=maarten.lankhorst@linux.intel.com \
    --cc=mripard@kernel.org \
    --cc=simona@ffwll.ch \
    --cc=tzimmermann@suse.de \
    /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).