drm/ttm: Silence randstruct warning about casting struct file
Subsystems (3)
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;
| ^~~~~~~~~~~~---
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
@@ -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
Matthew Brost
reviewerQuoted 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): 'structttm_backup' and 'struct file'21 | return (void *)file; | ^~~~~~~~~~~~
Quoted 7 lines
Fixes e7b5d23e5d47 ("drm/ttm: Provide a shmem backup implementation")Signed-off-by Kees Cook <[email protected]>---
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
@@ -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]>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@@ -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
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): 'structttm_backup' and 'struct file'21 | return (void *)file; | ^~~~~~~~~~~~Quoted 7 lines
Fixes e7b5d23e5d47 ("drm/ttm: Provide a shmem backup implementation")Signed-off-by Kees Cook <[email protected]>---
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@@ -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