drm/ttm: Silence randstruct warning about casting struct file
Subsystems (4)
PATCH v2 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;
| ^~~~~~~~~~~~---
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(-)
drivers/gpu/drm/ttm/ttm_backup.c +8 -3
@@ -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); }/*
Signature · 1 line
2.34.1
Quoted 10 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; | ^~~~~~~~~~~~Suggested-by Matthew Brost <[email protected]>
I forgot the policy if suggest-by but will add:
Thomas was out today I suspect he will look at this tomorrow when he is back too.
Matt
Quoted 53 lines
Fixes e7b5d23e5d47 ("drm/ttm: Provide a shmem backup implementation")Signed-off-by Kees Cook <[email protected]>---
v2: use struct andcontainer_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(-)drivers/gpu/drm/ttm/ttm_backup.c +8
-3@@ -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); }/* Signature · 1 line
2.34.1
Quoted 10 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 4 lines
Quoted 1 line
Suggested-by Matthew Brost <[email protected]>I forgot the policy if suggest-by but will add:
Reviewed-by Matthew Brost <[email protected]>
Yeah, and feel free to snag authorship entirely if you want. I only updated the comment, really. :)
Quoted 2 lines
Thomas was out today I suspect he will look at this tomorrow when he is back too.
Cool, no rush. It's currently just a warning in -next re-exposed by randstruct getting cleared for allmodconfig again. :)
Thanks!
-Kees
Signature · 1 line
Kees Cook
Al Viro
maintainerQuoted 10 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
Quoted 1 line
Suggested-by Matthew Brost <[email protected]>I forgot the policy if suggest-by but will add:
Reviewed-by Matthew Brost <[email protected]>Thomas was out today I suspect he will look at this tomorrow when he is back too.
[fsdevel and the rest of VFS maintainers Cc'd]
Reason: struct file should *NOT* be embedded into other objects without the core VFS being very explicitly aware of those. The only such case is outright local to fs/file_table.c, and breeding more of those is a really bad idea.
Don't do that. Same goes for struct {dentry, super_block, mount} in case anyone gets similar ideas.
Quoted 23 lines · Al Viro
Quoted 10 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
Quoted 1 line
Suggested-by Matthew Brost <[email protected]>I forgot the policy if suggest-by but will add:
Reviewed-by Matthew Brost <[email protected]>Thomas was out today I suspect he will look at this tomorrow when he is back too.
[fsdevel and the rest of VFS maintainers Cc'd]
NAKed-by Al Viro <[email protected]>
Quoted 7 lines
Reason: struct file should *NOT* be embedded into other objects without the core VFS being very explicitly aware of those. The only such case is outright local to
fs/file_table.c, and breeding more of those is a really bad idea.Don't do that. Same goes for struct {dentry,
super_block, mount} in case anyone gets similar ideas.
Well, in that case, the NAK should be against commit e7b5d23e5d47 ("drm/ttm: Provide a shmem backup implementation"), but that looks to have had 15 revisions already...
But I will just back away slowly now. I was just fixing a warning introduced by it. :)
Signature · 1 line
Kees Cook
Matthew Brost
reviewerQuoted 16 lines · Al Viro
Quoted 15 lines · Matthew Brost
Quoted 10 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; | ^~~~~~~~~~~~Suggested-by Matthew Brost <[email protected]>I forgot the policy if suggest-by but will add:
Reviewed-by Matthew Brost <[email protected]>
Quoted 8 lines
Quoted 2 lines
Thomas was out today I suspect he will look at this tomorrow when he is back too.
[fsdevel and the rest of VFS maintainers Cc'd]
NAKed-by Al Viro <[email protected]>Reason: struct file should *NOT* be embedded into other objects without
I;m fairly certain is just aliasing... but I do understand a file cannot be embedded. Would comment help here indicating no other fields should be added to ttm_backup without struct file be converted to pointer or that just to risky?
Matt
Quoted 6 lines
the core VFS being very explicitly aware of those. The only such case is outright local to
fs/file_table.c, and breeding more of those is a really bad idea.Don't do that. Same goes for struct {dentry,
super_block, mount} in case anyone gets similar ideas.
Al Viro
maintainerQuoted 4 lines · Matthew Brost
I;m fairly certain is just aliasing... but I do understand a file cannot be embedded. Would comment help here indicating no other fields should be added to
ttm_backupwithout struct file be converted to pointer or that just to risky?
What exactly are you trying to do there? IOW, is that always supposed to be a struct file, or something dependent upon something in struct ttm_tt instance, or...?
And what is the lifecycle of that thing? E.g. what is guaranteed about ttm_backup_fini() vs. functions accessing the damn thing? Are they serialized on something/tied to lifecycle stages of struct ttm_tt?
Matthew Brost
reviewerQuoted 10 lines · Al Viro
Quoted 4 lines · Matthew Brost
I;m fairly certain is just aliasing... but I do understand a file cannot be embedded. Would comment help here indicating no other fields should be added to
ttm_backupwithout struct file be converted to pointer or that just to risky?What exactly are you trying to do there? IOW, is that always supposed to be a struct file, or something dependent upon something in struct
ttm_ttinstance, or...?
Create an opaque ttm_backup object for the rest of TTM / drivers to view
- it could change if the backup implementation changed.
Quoted 4 lines
And what is the lifecycle of that thing? E.g. what is guaranteed about
ttm_backup_fini()vs. functions accessing the damn thing? Are they serialized on something/tied to lifecycle stages of structttm_tt?
I believe the life cycle is when ttm_tt is destroyed or api allows overriding the old backup with a new one (currently unused).
Matt
Al Viro
maintainerQuoted 9 lines · Matthew Brost
Quoted 5 lines · Al Viro
On Thu, May 01, 2025 at 09:26:25PM -0700, Matthew Brost wrote:
And what is the lifecycle of that thing? E.g. what is guaranteed about
ttm_backup_fini()vs. functions accessing the damn thing? Are they serialized on something/tied to lifecycle stages of structttm_tt?I believe the life cycle is when
ttm_ttis destroyed or api allows overriding the old backup with a new one (currently unused).
Umm... So can ttm_tt_setup_backup() be called in the middle of e.g. ttm_backup_drop() or ttm_backup_{copy,backup}_page(), etc.?
I mean, if they had been called by ttm_backup.c internals, it would be an internal business of specific implementation, with all serialization, etc. warranties being its responsibility; but if it's called by other code that is supposed to be isolated from details of what ->backup is pointing to...
Sorry for asking dumb questions, but I hadn't seen the original threads. Basically, what prevents the underlying shmem file getting torn apart while another operation is using it? It might very well be simple, but I had enough "it's because of... oh, bugger" moments on the receiving end of such questions...
Quoted 20 lines · Al Viro
Quoted 9 lines · Matthew Brost
Quoted 5 lines · Al Viro
On Thu, May 01, 2025 at 09:26:25PM -0700, Matthew Brost wrote:
And what is the lifecycle of that thing? E.g. what is guaranteed about
ttm_backup_fini()vs. functions accessing the damn thing? Are they serialized on something/tied to lifecycle stages of structttm_tt?I believe the life cycle is when
ttm_ttis destroyed or api allows overriding the old backup with a new one (currently unused).Umm... So can
ttm_tt_setup_backup()be called in the middle of e.g.ttm_backup_drop()or ttm_backup_{copy,backup}_page(), etc.?I mean, if they had been called by
ttm_backup.cinternals, it would be an internal business of specific implementation, with all serialization, etc. warranties being its responsibility; but if it's called by other code that is supposed to be isolated from details of what ->backup is pointing to...
Quoted 5 lines
Sorry for asking dumb questions, but I hadn't seen the original threads. Basically, what prevents the underlying shmem file getting torn apart while another operation is using it? It might very well be simple, but I had enough "it's because of... oh, bugger" moments on the receiving end of such questions...
It's the outside logic which makes sure that the backup structure stays around as long as the BO or the device which needs it is around.
But putting that aside I was not very keen about the whole idea of never defining the ttm_backup structure and just casting it to a file in the backend either.
So I would just completely nuke that unnecessary abstraction and just use a pointer to a file all around.
Regards,
Christian.
Thomas Hellström
reviewerQuoted 36 lines · Christian König
Quoted 24 lines · Al Viro
Quoted 10 lines · Matthew Brost
Quoted 8 lines · Al Viro
On Thu, May 01, 2025 at 09:26:25PM -0700, Matthew Brost wrote:
And what is the lifecycle of that thing? E.g. what is guaranteed about
ttm_backup_fini()vs. functions accessing the damn thing? Are they
serialized on something/tied to lifecycle stages of structttm_tt?Quoted 3 lines
I believe the life cycle is when
ttm_ttis destroyed or api allows
overriding the old backup with a new one (currently unused).Umm... So can
ttm_tt_setup_backup()be called in the middle of e.g.ttm_backup_drop()or ttm_backup_{copy,backup}_page(), etc.?I mean, if they had been called by
ttm_backup.cinternals, it would be an internal business of specific implementation, with all serialization, etc. warranties being its responsibility; but if it's called by other code that is supposed to be isolated from details of what ->backup is pointing to...Quoted 6 lines
Sorry for asking dumb questions, but I hadn't seen the original threads. Basically, what prevents the underlying shmem file getting
torn apart while another operation is using it? It might very well be simple, but I had enough "it's because of... oh, bugger" moments on the receiving end of such questions...It's the outside logic which makes sure that the backup structure stays around as long as the BO or the device which needs it is around.
Quoted 6 lines
But putting that aside I was not very keen about the whole idea of never defining the
ttm_backupstructure and just casting it to a file in the backend either.So I would just completely nuke that unnecessary abstraction and just use a pointer to a file all around.
Hmm, yes there were early the series a number of different implementations of the struct ttm_backup. Initially because previous attempts of using a shmem object failed but now that we've landed on a shmem object, We should be able to replace it with a struct file pointer.
Let me take a look at this.
/Thomas
Quoted 3 lines
Regards,
Christian.