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;
| ^~~~~~~~~~~~
Suggested-by: Matthew Brost <[email protected]>
Fixes: e7b5d23e5d47 ("drm/ttm: Provide a shmem backup implementation")
Signed-off-by: Kees Cook <[email protected]>
---
v2: use struct and container_of (matthew)
v1: https://lore.kernel.org/all/[email protected]/
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 | 11 ++++++++---
1 file changed, 8 insertions(+), 3 deletions(-)
diff --git a/drivers/gpu/drm/ttm/ttm_backup.c b/drivers/gpu/drm/ttm/ttm_backup.c
index 93c007f18855..60cff6c60db4 100644
--- a/drivers/gpu/drm/ttm/ttm_backup.c
+++ b/drivers/gpu/drm/ttm/ttm_backup.c
@@ -9,16 +9,21 @@
/*
* Casting from randomized struct file * to struct ttm_backup * is fine since
- * struct ttm_backup is never defined nor dereferenced.
+ * struct ttm_backup is never defined nor dereferenced. Use a single-member
+ * struct to avoid cast warnings.
*/
+struct ttm_backup {
+ struct file file;
+};
+
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);
}
/*
--
2.34.1