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

From: Kees Cook
Date: Thu May 01 2025 - 15:59:19 EST


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")
Signed-off-by: Kees Cook <kees@xxxxxxxxxx>
---
Cc: Thomas Hellström <thomas.hellstrom@xxxxxxxxxxxxxxx>
Cc: Christian Koenig <christian.koenig@xxxxxxx>
Cc: Somalapuram Amaranath <Amaranath.Somalapuram@xxxxxxx>
Cc: Matthew Brost <matthew.brost@xxxxxxxxx>
Cc: Huang Rui <ray.huang@xxxxxxx>
Cc: Matthew Auld <matthew.auld@xxxxxxxxx>
Cc: Maarten Lankhorst <maarten.lankhorst@xxxxxxxxxxxxxxx>
Cc: Maxime Ripard <mripard@xxxxxxxxxx>
Cc: Thomas Zimmermann <tzimmermann@xxxxxxx>
Cc: David Airlie <airlied@xxxxxxxxx>
Cc: Simona Vetter <simona@xxxxxxxx>
Cc: <dri-devel@xxxxxxxxxxxxxxxxxxxxx>
---
drivers/gpu/drm/ttm/ttm_backup.c | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/drivers/gpu/drm/ttm/ttm_backup.c b/drivers/gpu/drm/ttm/ttm_backup.c
index 93c007f18855..626af1de562f 100644
--- a/drivers/gpu/drm/ttm/ttm_backup.c
+++ b/drivers/gpu/drm/ttm/ttm_backup.c
@@ -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;
}

/*
--
2.34.1