Skip to contentSkip to conversation

drm/ttm: Silence randstruct warning about casting struct file

PATCH Superseded Kees Cook 2 participants 3 messages May 1, 2025 – May 2, 2025

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 drm/ttm: Silence randstruct warning about casting struct file

Casting through a "void *" isn't sufficient to convince the randstruct GCC plugin that the result is intentional. Instead operate through an explicit union to silence the warning:

drivers/gpu/drm/ttm/ttm_backup.c: In function 'ttm_file_to_backup':
drivers/gpu/drm/ttm/ttm_backup.c:21:16: note: randstruct: casting between randomized structure pointer types (ssa): 'struct ttm_backup' and 'struct file'

   21 |         return (void *)file;
      |                ^~~~~~~~~~~~
Fixes e7b5d23e5d47 ("drm/ttm: Provide a shmem backup implementation")

---
Cc: Thomas Hellström <[email protected]>
Cc: Christian Koenig <[email protected]>
Cc: Somalapuram Amaranath <[email protected]>
Cc: Matthew Brost <[email protected]>
Cc: Huang Rui <[email protected]>
Cc: Matthew Auld <[email protected]>
Cc: Maarten Lankhorst <[email protected]>
Cc: Maxime Ripard <[email protected]>
Cc: Thomas Zimmermann <[email protected]>
Cc: David Airlie <[email protected]>
Cc: Simona Vetter <[email protected]>
Cc: <[email protected]>

---
 drivers/gpu/drm/ttm/ttm_backup.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/gpu/drm/ttm/ttm_backup.c +7 -1
index 93c007f18855..626af1de562f 100644
@@ -18,7 +18,13 @@ static struct file *ttm_backup_to_file(struct ttm_backup *backup)
 
 static struct ttm_backup *ttm_file_to_backup(struct file *file)
 {
-	return (void *)file;
+	/* Explicit union instead of a cast to make randstruct ignore us. */
+	union {
+		struct file *file;
+		struct ttm_backup *backup;
+	} u;
+	u.file = file;
+	return u.backup;
 }
 
 /*
Signature · 1 line
2.34.1
reviewer
#1 lore raw
Quoted 9 lines · Kees Cook

Casting through a "void *" isn't sufficient to convince the randstruct GCC plugin that the result is intentional. Instead operate through an explicit union to silence the warning:

drivers/gpu/drm/ttm/ttm_backup.c: In function 'ttm_file_to_backup':
drivers/gpu/drm/ttm/ttm_backup.c:21:16: note: randstruct: casting between randomized structure pointer types (ssa): 'struct ttm_backup' and 'struct file'

   21 |         return (void *)file;
      |                ^~~~~~~~~~~~
Quoted 7 lines
Fixes e7b5d23e5d47 ("drm/ttm: Provide a shmem backup implementation")

---
Cc: Thomas Hellström <[email protected]>
Cc: Christian Koenig <[email protected]>
Cc: Somalapuram Amaranath <[email protected]>
Cc: Matthew Brost <[email protected]>

What if we did something like this instead:

drivers/gpu/drm/ttm/ttm_backup.c +6 -2
index 93c007f18855..fe936a87c959 100644
@@ -7,18 +7,22 @@
 #include <linux/page-flags.h>
 #include <linux/swap.h>
 
+struct ttm_backup {
+       struct file file;
+};
+
 /*
  * Casting from randomized struct file * to struct ttm_backup * is fine since
  * struct ttm_backup is never defined nor dereferenced.
  */
 static struct file *ttm_backup_to_file(struct ttm_backup *backup)
 {
-       return (void *)backup;
+       return &backup->file;
 }
 
 static struct ttm_backup *ttm_file_to_backup(struct file *file)
 {
-       return (void *)file;
+       return container_of(file, struct ttm_backup, file);
 }
 

Matt

Quoted 34 lines
Cc Huang Rui <[email protected]>
Cc Matthew Auld <[email protected]>
Cc Maarten Lankhorst <[email protected]>
Cc Maxime Ripard <[email protected]>
Cc Thomas Zimmermann <[email protected]>
Cc David Airlie <[email protected]>
Cc Simona Vetter <[email protected]>
---
 drivers/gpu/drm/ttm/ttm_backup.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/gpu/drm/ttm/ttm_backup.c +7 -1
index 93c007f18855..626af1de562f 100644
@@ -18,7 +18,13 @@ static struct file *ttm_backup_to_file(struct ttm_backup *backup)
 
 static struct ttm_backup *ttm_file_to_backup(struct file *file)
 {
-	return (void *)file;
+	/* Explicit union instead of a cast to make randstruct ignore us. */
+	union {
+		struct file *file;
+		struct ttm_backup *backup;
+	} u;
+	u.file = file;
+	return u.backup;
 }
 
 /*
Signature · 1 line
2.34.1
author
#2 lore raw
Quoted 37 lines · Matthew Brost
Quoted 9 lines · Kees Cook

Casting through a "void *" isn't sufficient to convince the randstruct GCC plugin that the result is intentional. Instead operate through an explicit union to silence the warning:

drivers/gpu/drm/ttm/ttm_backup.c: In function 'ttm_file_to_backup':
drivers/gpu/drm/ttm/ttm_backup.c:21:16: note: randstruct: casting between randomized structure pointer types (ssa): 'struct ttm_backup' and 'struct file'

   21 |         return (void *)file;
      |                ^~~~~~~~~~~~
Quoted 7 lines
Fixes e7b5d23e5d47 ("drm/ttm: Provide a shmem backup implementation")

---
Cc: Thomas Hellström <[email protected]>
Cc: Christian Koenig <[email protected]>
Cc: Somalapuram Amaranath <[email protected]>
Cc: Matthew Brost <[email protected]>

What if we did something like this instead:

drivers/gpu/drm/ttm/ttm_backup.c +4 -0
index 93c007f18855..fe936a87c959 100644
@@ -7,18 +7,22 @@
 #include <linux/page-flags.h>
 #include <linux/swap.h>
 
+struct ttm_backup {
+       struct file file;
+};
+
 /*
  * Casting from randomized struct file * to struct ttm_backup * is fine since
  * struct ttm_backup is never defined nor dereferenced.
  */
 static struct file *ttm_backup_to_file(struct ttm_backup *backup)

drivers/gpu/drm/ttm/ttm_backup.c line 26

Quoted 10 lines
drivers/gpu/drm/ttm/ttm_backup.c +2 -2
 {
-       return (void *)backup;
+       return &backup->file;
 }
 
 static struct ttm_backup *ttm_file_to_backup(struct file *file)
 {
-       return (void *)file;
+       return container_of(file, struct ttm_backup, file);
 }

Yup, that works too! I'll send this as a v2.

Signature · 1 line
Kees Cook