Skip to contentSkip to conversation

drm/panfrost: Document base field location constraint in panfrost_gem_object

PATCH In review Boris Brezillon 5 participants 9 messages Nov 29, 2019

Subsystems (3)
lore
Share & tools
lore

Boris Brezillon

author maintainer
#0 lore raw

PATCH 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
index b3517ff9630c..d480261fc177 100644
@@ -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
maintainer Reviewed-by on v2 1/1
#1 lore raw
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.

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
index b3517ff9630c..d480261fc177 100644

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

author maintainer
#2 lore raw
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.

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.

Oh, you're right, I didn't catch that one.

Quoted 19 lines

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
index b3517ff9630c..d480261fc177 100644
@@ -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 -0
 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

#3 lore raw
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

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.

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

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
index b3517ff9630c..d480261fc177 100644
@@ -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 -0
 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

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
author maintainer
#4 lore raw
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.

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.

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

#5 lore raw
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.

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.

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 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.

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
author maintainer
#6 lore raw
Quoted 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

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.

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 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.

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->base so 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

Reviewed-by on v2 1/1
#7 lore raw
In reply to Boris Brezillon
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.

---
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
index b3517ff9630c..d480261fc177 100644
@@ -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

#8 lore raw
In reply to Boris Brezillon
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.

---
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
index b3517ff9630c..d480261fc177 100644

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 -0
 	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
Signature · 6 lines
Ville Syrjälä
Intel
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel