drm/panfrost: Document base field location constraint in panfrost_gem_object
Subsystems (3)
Boris Brezillon
maintainerPATCH v2 1/1 drm/panfrost: Document base field location constraint in panfrost_gem_object
I've spent hours chasing a memory corruption that was caused by insertion of an extra field field before ->base. Let's document the fact that base has to be the first field in panfrost_gem_object.
---
Changes in v2:
* Use the proper prefix in the subject line
--- drivers/gpu/drm/panfrost/panfrost_gem.h | 4 ++++ 1 file changed, 4 insertions(+)
drivers/gpu/drm/panfrost/panfrost_gem.h +4 -0
@@ -10,6 +10,10 @@struct panfrost_mmu ;struct panfrost_gem_object { +/* +* Must be the first element because we're using some of the +* drm_gem_shmem helpers. +*/ struct drm_gem_shmem_object base;struct sg_table *sgts;
Signature · 6 lines
2.23.0 _______________________________________________ dri-devel mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/dri-devel
Quoted 5 lines · Boris Brezillon
I've spent hours chasing a memory corruption that was caused by insertion of an extra field field before ->base. Let's document the fact that base has to be the first field in
panfrost_gem_object.Signed-off-by Boris Brezillon <[email protected]>
This seems to be a limitation imposed by the gem_create_object() callback - e.g. it's assumed that kfree() can be directly called on the result. Useful to have the documentation though.
Steve
Quoted 13 lines
---
Changes in v2:
* Use the proper prefix in the subject line--- drivers/gpu/drm/panfrost/panfrost_gem.h | 4 ++++ 1 file changed, 4 insertions(+)drivers/gpu/drm/panfrost/panfrost_gem.h +0
-0
drivers/gpu/drm/panfrost/panfrost_gem.h line 19
Quoted 10 lines
drivers/gpu/drm/panfrost/panfrost_gem.h +4
-0@@ -10,6 +10,10 @@struct panfrost_mmu ;struct panfrost_gem_object { +/* +* Must be the first element because we're using some of the +* drm_gem_shmem helpers. +*/ struct drm_gem_shmem_object base;struct sg_table *sgts;
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Boris Brezillon
maintainerQuoted 10 lines · On Fri, 29 Nov 2019 14:13:33 +0000 Steven Price <[email protected]> wrote:
Quoted 5 lines · Boris Brezillon
I've spent hours chasing a memory corruption that was caused by insertion of an extra field field before ->base. Let's document the fact that base has to be the first field in
panfrost_gem_object.Signed-off-by Boris Brezillon <[email protected]>This seems to be a limitation imposed by the
gem_create_object()callback - e.g. it's assumed thatkfree()can be directly called on the result. Useful to have the documentation though.
Oh, you're right, I didn't catch that one.
Quoted 19 lines
Reviewed-by Steven Price <[email protected]>Steve
Quoted 14 lines
---
Changes in v2:
* Use the proper prefix in the subject line--- drivers/gpu/drm/panfrost/panfrost_gem.h | 4 ++++ 1 file changed, 4 insertions(+)drivers/gpu/drm/panfrost/panfrost_gem.h +0
-0@@ -10,6 +10,10 @@struct panfrost_mmu ;
Quoted 10 lines
drivers/gpu/drm/panfrost/panfrost_gem.h line 19
Quoted 9 lines
drivers/gpu/drm/panfrost/panfrost_gem.h +4
-0struct panfrost_gem_object { +/* +* Must be the first element because we're using some of the +* drm_gem_shmem helpers. +*/ struct drm_gem_shmem_object base;struct sg_table *sgts;
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Quoted 8 lines · Boris Brezillon
Quoted 5 lines · On Fri, 29 Nov 2019 14:13:33 +0000 Steven Price <[email protected]> wrote:
Quoted 4 lines · Boris Brezillon
I've spent hours chasing a memory corruption that was caused by insertion of an extra field field before ->base. Let's document the fact that base has to be the first field in
panfrost_gem_object.
Quoted 7 lines
Quoted 5 lines
Quoted 1 line
Signed-off-by Boris Brezillon <[email protected]>This seems to be a limitation imposed by the
gem_create_object()callback - e.g. it's assumed thatkfree()can be directly called on the result. Useful to have the documentation though.Oh, you're right, I didn't catch that one.
As a general rule of thumb, never insert anything before a struct member called base. Even more so if it's of the same kind of $thing, but less spezialed. This pattern is so common it's fairly often not documented anywhere.
-Daniel
Quoted 35 lines
Quoted 20 lines
Reviewed-by Steven Price <[email protected]>Steve
Quoted 14 lines
---
Changes in v2:
* Use the proper prefix in the subject line--- drivers/gpu/drm/panfrost/panfrost_gem.h | 4 ++++ 1 file changed, 4 insertions(+)drivers/gpu/drm/panfrost/panfrost_gem.h +0
-0@@ -10,6 +10,10 @@struct panfrost_mmu ;Quoted 10 lines
drivers/gpu/drm/panfrost/panfrost_gem.h line 19
Quoted 9 lines
drivers/gpu/drm/panfrost/panfrost_gem.h +4
-0struct panfrost_gem_object { +/* +* Must be the first element because we're using some of the +* drm_gem_shmem helpers. +*/ struct drm_gem_shmem_object base;struct sg_table *sgts;_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Signature · 7 lines
Daniel Vetter Software Engineer, Intel Corporation http://blog.ffwll.ch _______________________________________________ dri-devel mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/dri-devel
Boris Brezillon
maintainerQuoted 15 lines · On Fri, 29 Nov 2019 19:40:38 +0100 Daniel Vetter <[email protected]> wrote:
Quoted 14 lines · Boris Brezillon
Quoted 10 lines · On Fri, 29 Nov 2019 14:13:33 +0000 Steven Price <[email protected]> wrote:
Quoted 5 lines · Boris Brezillon
I've spent hours chasing a memory corruption that was caused by insertion of an extra field field before ->base. Let's document the fact that base has to be the first field in
panfrost_gem_object.Signed-off-by Boris Brezillon <[email protected]>This seems to be a limitation imposed by the
gem_create_object()callback - e.g. it's assumed thatkfree()can be directly called on the result. Useful to have the documentation though.
Quoted 6 lines
Quoted 1 line
Oh, you're right, I didn't catch that one.
As a general rule of thumb, never insert anything before a struct member called base. Even more so if it's of the same kind of $thing, but less spezialed. This pattern is so common it's fairly often not documented anywhere.
I could argue that anything using container_of() in its to_xxx() helper is misleading the user into thinking the position of the base field doesn't matter (which is exactly what happened here), but I feel like I already lost this battle, so I'll simply drop the patch.
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Quoted 25 lines · Boris Brezillon
Quoted 15 lines · On Fri, 29 Nov 2019 19:40:38 +0100 Daniel Vetter <[email protected]> wrote:
Quoted 14 lines · Boris Brezillon
Quoted 10 lines · On Fri, 29 Nov 2019 14:13:33 +0000 Steven Price <[email protected]> wrote:
Quoted 5 lines · Boris Brezillon
I've spent hours chasing a memory corruption that was caused by insertion of an extra field field before ->base. Let's document the fact that base has to be the first field in
panfrost_gem_object.Signed-off-by Boris Brezillon <[email protected]>This seems to be a limitation imposed by the
gem_create_object()callback - e.g. it's assumed thatkfree()can be directly called on the result. Useful to have the documentation though.Quoted 6 lines
Quoted 1 line
Oh, you're right, I didn't catch that one.
As a general rule of thumb, never insert anything before a struct member called base. Even more so if it's of the same kind of $thing, but less spezialed. This pattern is so common it's fairly often not documented anywhere.
Quoted 4 lines
I could argue that anything using
container_of()in itsto_xxx()helper is misleading the user into thinking the position of the base field doesn't matter (which is exactly what happened here), but I feel like I already lost this battle, so I'll simply drop the patch.
I agree that this is a bit annoying. Apart from the "let's pass the wrong thing to kfree()" issue the other problem is NULL pointers. If those don't get preserved when going between the base and derived class the code will turn to bad spaghetti.
Not a problem for the typical to_foo_crtc() since we could hide an explicit NULL check in there. But the other direction generally just uses &foo_crtc->base so not going to work. Shouldn't be an impossible task to wrap all of those as well, but I guess no one's motivated enough to actually do it.
Signature · 6 lines
Ville Syrjälä Intel _______________________________________________ dri-devel mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/dri-devel
Boris Brezillon
maintainerQuoted 26 lines · On Fri, 29 Nov 2019 21:43:45 +0200 Ville Syrjälä…
Quoted 25 lines · Boris Brezillon
Quoted 21 lines · On Fri, 29 Nov 2019 19:40:38 +0100 Daniel Vetter <[email protected]> wrote:
Quoted 8 lines · Boris Brezillon
Quoted 5 lines · On Fri, 29 Nov 2019 14:13:33 +0000 Steven Price <[email protected]> wrote:
Quoted 4 lines · Boris Brezillon
I've spent hours chasing a memory corruption that was caused by insertion of an extra field field before ->base. Let's document the fact that base has to be the first field in
panfrost_gem_object.Quoted 7 lines
Quoted 5 lines
Quoted 1 line
Signed-off-by Boris Brezillon <[email protected]>This seems to be a limitation imposed by the
gem_create_object()callback - e.g. it's assumed thatkfree()can be directly called on the result. Useful to have the documentation though.Oh, you're right, I didn't catch that one.
As a general rule of thumb, never insert anything before a struct member called base. Even more so if it's of the same kind of $thing, but less spezialed. This pattern is so common it's fairly often not documented anywhere.
Quoted 7 lines
Quoted 4 lines
I could argue that anything using
container_of()in itsto_xxx()helper is misleading the user into thinking the position of the base field doesn't matter (which is exactly what happened here), but I feel like I already lost this battle, so I'll simply drop the patch.I agree that this is a bit annoying. Apart from the "let's pass the wrong thing to
kfree()"
This is already a disputable design choice IMHO. Why should you delegate the free() to someone who didn't allocate the thing in the first place.
Quoted 7 lines
issue the other problem is NULL
pointers. If those don't get preserved when going between the base and derived class the code will turn to bad spaghetti.Not a problem for the typical
to_foo_crtc()since we could hide an explicit NULL check in there. But the other direction generally just uses&foo_crtc->baseso not going to work.
Well, if you inherit from the base struct, you'll inevitably have NULL checks because of pointer dereferences you have when accessing other fields, so I'd expects most of them to be already present (not saying finding the remaining ones is an easy task).
The real problem I see here is that we re-use helpers manipulating base objects without providing wrappers, and that can only work if base is the first field in the child struct.
Quoted 3 lines
Shouldn't be an impossible task to wrap all of those as well, but I guess no one's motivated enough to actually do it.
Actually, I never suggested to do any of that, because I know how invasive/risky such a change would be. All I was trying to do was make an implicit requirement more explicit with a comment stating the fact and explaining why (BTW, I like your suggestion to use BUILD_BUG_ON() to catch those problems at compilation time).
Anyway, enough on that matter. I'll try to remember that the use of container_of() does not necessarily means fields can be shuffled.
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Quoted 30 lines · Boris Brezillon
I've spent hours chasing a memory corruption that was caused by insertion of an extra field field before ->base. Let's document the fact that base has to be the first field in
panfrost_gem_object.Signed-off-by Boris Brezillon <[email protected]>---
Changes in v2:
* Use the proper prefix in the subject line--- drivers/gpu/drm/panfrost/panfrost_gem.h | 4 ++++ 1 file changed, 4 insertions(+)drivers/gpu/drm/panfrost/panfrost_gem.h +4
-0@@ -10,6 +10,10 @@struct panfrost_mmu ;struct panfrost_gem_object { +/* +* Must be the first element because we're using some of the +* drm_gem_shmem helpers. +*/ struct drm_gem_shmem_object base;struct sg_table *sgts;Signature · 1 line
2.23.0
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Quoted 17 lines · Boris Brezillon
I've spent hours chasing a memory corruption that was caused by insertion of an extra field field before ->base. Let's document the fact that base has to be the first field in
panfrost_gem_object.Signed-off-by Boris Brezillon <[email protected]>---
Changes in v2:
* Use the proper prefix in the subject line--- drivers/gpu/drm/panfrost/panfrost_gem.h | 4 ++++ 1 file changed, 4 insertions(+)drivers/gpu/drm/panfrost/panfrost_gem.h +0
-0
drivers/gpu/drm/panfrost/panfrost_gem.h line 16
Quoted 7 lines
drivers/gpu/drm/panfrost/panfrost_gem.h +4
-0@@ -10,6 +10,10 @@struct panfrost_mmu ;struct panfrost_gem_object { +/* +* Must be the first element because we're using some of the +* drm_gem_shmem helpers. +*/
An accompanying BUILD_BUG_ON() would be a good way to catch anyone ignoring the comment.
Quoted 10 lines
drivers/gpu/drm/panfrost/panfrost_gem.h +0
-0struct drm_gem_shmem_object base;struct sg_table *sgts;Signature · 6 lines
2.23.0 _______________________________________________ dri-devel mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/dri-devel
Signature · 6 lines
Ville Syrjälä Intel _______________________________________________ dri-devel mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/dri-devel