Skip to contentSkip to conversation

drm/vkms: introduce prepare_fb and cleanup_fb functions

PATCH Superseded Maíra Canal 4 participants 7 messages Jan 5, 2023 – Jan 6, 2023

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/vkms: introduce prepare_fb and cleanup_fb functions

With commit 359c6649cd9a ("drm/gem: Implement shadow-plane {begin, end}_fb_access with vmap"), the behavior of the shadow-plane helpers changed and the vunmap is now performed at the end of
the current pageflip, instead of the end of the following pageflip.

By performing the vunmap at the end of the current pageflip, invalid memory is accessed by the vkms during the plane composition, as the data is being unmapped before being used.

Therefore, introduce again prepare_fb and cleanup_fb functions to the vkms, which were previously removed on commit b43e2ec03b0d ("drm/vkms:
Let shadow-plane helpers prepare the plane's FB").

Fixes 359c6649cd9a ("drm/gem: Implement shadow-plane {begin, end}_fb_access with vmap")
---
 drivers/gpu/drm/vkms/vkms_plane.c | 36 ++++++++++++++++++++++++++++++-
 1 file changed, 35 insertions(+), 1 deletion(-)
drivers/gpu/drm/vkms/vkms_plane.c +0 -0
index c3a845220e10..b3f8a115cc23 100644
@@ -160,10 +160,44 @@ static int vkms_plane_atomic_check(struct drm_plane *plane,
	return 0;

}

+static int vkms_prepare_fb(struct drm_plane *plane,
+ struct drm_plane_state *state)
+{
+ struct drm_shadow_plane_state *shadow_plane_state;
+ struct drm_framebuffer *fb = state->fb;
+ int ret;
+
+ if (!fb)
+ return 0;
+
+ shadow_plane_state = to_drm_shadow_plane_state(state);
+
+ ret = drm_gem_plane_helper_prepare_fb(plane, state);
+ if (ret)
+ return ret;
+
+ return drm_gem_fb_vmap(fb, shadow_plane_state->map, shadow_plane_state->data);
+}
+
+static void vkms_cleanup_fb(struct drm_plane *plane,
+ struct drm_plane_state *state)
+{
+ struct drm_shadow_plane_state *shadow_plane_state;
+ struct drm_framebuffer *fb = state->fb;
+
+ if (!fb)
+ return;
+
+ shadow_plane_state = to_drm_shadow_plane_state(state);
+
+ drm_gem_fb_vunmap(fb, shadow_plane_state->map);
+}
+
static const struct drm_plane_helper_funcs vkms_primary_helper_funcs = {
.atomic_update = vkms_plane_atomic_update,
.atomic_check = vkms_plane_atomic_check,
- DRM_GEM_SHADOW_PLANE_HELPER_FUNCS,
+ .prepare_fb = vkms_prepare_fb,
+ .cleanup_fb = vkms_cleanup_fb,
};

struct vkms_plane *vkms_plane_init(struct vkms_device *vkmsdev,
--
2.39.0

#1 lore raw
Quoted 8 lines · Maíra Canal

With commit 359c6649cd9a ("drm/gem: Implement shadow-plane {begin, end}_fb_access with vmap"), the behavior of the shadow-plane helpers changed and the vunmap is now performed at the end of
the current pageflip, instead of the end of the following pageflip.

By performing the vunmap at the end of the current pageflip, invalid memory is accessed by the vkms during the plane composition, as the data is being unmapped before being used.

Hi Maíra,

Thanks for investigating this issue. Can you add in the commit message the kernel Oops caused by this change?

Besides that, I wonder if the right thing would be to restore the previous behavior of vunmap in shadow-plane helpers, instead of reintroduce driver-specific hooks for vmap/vunmap correctly to vkms.

Maybe Thomas has some inputs on this shadow-plane changing to help us on figuring out the proper fix (?)

Best Regards,

Melissa

Quoted 64 lines

Therefore, introduce again prepare_fb and cleanup_fb functions to the vkms, which were previously removed on commit b43e2ec03b0d ("drm/vkms:
Let shadow-plane helpers prepare the plane's FB").

Fixes 359c6649cd9a ("drm/gem: Implement shadow-plane {begin, end}_fb_access with vmap")
---
 drivers/gpu/drm/vkms/vkms_plane.c | 36 ++++++++++++++++++++++++++++++-
 1 file changed, 35 insertions(+), 1 deletion(-)
drivers/gpu/drm/vkms/vkms_plane.c +35 -2
index c3a845220e10..b3f8a115cc23 100644
@@ -160,10 +160,44 @@ static int vkms_plane_atomic_check(struct drm_plane *plane,
 	return 0;
 }
 
+static int vkms_prepare_fb(struct drm_plane *plane,
+			   struct drm_plane_state *state)
+{
+	struct drm_shadow_plane_state *shadow_plane_state;
+	struct drm_framebuffer *fb = state->fb;
+	int ret;
+
+	if (!fb)
+		return 0;
+
+	shadow_plane_state = to_drm_shadow_plane_state(state);
+
+	ret = drm_gem_plane_helper_prepare_fb(plane, state);
+	if (ret)
+		return ret;
+
+	return drm_gem_fb_vmap(fb, shadow_plane_state->map, shadow_plane_state->data);
+}
+
+static void vkms_cleanup_fb(struct drm_plane *plane,
+			    struct drm_plane_state *state)
+{
+	struct drm_shadow_plane_state *shadow_plane_state;
+	struct drm_framebuffer *fb = state->fb;
+
+	if (!fb)
+		return;
+
+	shadow_plane_state = to_drm_shadow_plane_state(state);
+
+	drm_gem_fb_vunmap(fb, shadow_plane_state->map);
+}
+
 static const struct drm_plane_helper_funcs vkms_primary_helper_funcs = {
 	.atomic_update		= vkms_plane_atomic_update,
 	.atomic_check		= vkms_plane_atomic_check,
-	DRM_GEM_SHADOW_PLANE_HELPER_FUNCS,
+	.prepare_fb		= vkms_prepare_fb,
+	.cleanup_fb		= vkms_cleanup_fb,
 };
 
 struct vkms_plane *vkms_plane_init(struct vkms_device *vkmsdev,
--

2.39.0

maintainer Acked-by
#2 lore raw

Hi

Am 05.01.23 um 19:43 schrieb Melissa Wen:

Quoted 15 lines · Maíra Canal
Quoted 8 lines

With commit 359c6649cd9a ("drm/gem: Implement shadow-plane {begin, end}_fb_access with vmap"), the behavior of the shadow-plane helpers changed and the vunmap is now performed at the end of
the current pageflip, instead of the end of the following pageflip.

By performing the vunmap at the end of the current pageflip, invalid memory is accessed by the vkms during the plane composition, as the data is being unmapped before being used.

Hi Maíra,

Thanks for investigating this issue. Can you add in the commit message the kernel Oops caused by this change?

Quoted 6 lines

Besides that, I wonder if the right thing would be to restore the previous behavior of vunmap in shadow-plane helpers, instead of reintroduce driver-specific hooks for vmap/vunmap correctly to vkms.

Maybe Thomas has some inputs on this shadow-plane changing to help us on figuring out the proper fix (?)

The fix looks good. I left some minor-important comments below.

I would suggest to rethink the overall driver design. Instead of keeping these buffer pinned for long. It might be better to have a per-plane intermediate buffer that you update on each call to atomic_update. That's how real drivers interact with their hardware.

Quoted 12 lines

Best Regards,

Melissa

Quoted 7 lines

Therefore, introduce again prepare_fb and cleanup_fb functions to the vkms, which were previously removed on commit b43e2ec03b0d ("drm/vkms:
Let shadow-plane helpers prepare the plane's FB").

Fixes 359c6649cd9a ("drm/gem: Implement shadow-plane {begin, end}_fb_access with vmap")
Quoted 12 lines
Quoted 12 lines
---
  drivers/gpu/drm/vkms/vkms_plane.c | 36 ++++++++++++++++++++++++++++++-
  1 file changed, 35 insertions(+), 1 deletion(-)
drivers/gpu/drm/vkms/vkms_plane.c +0 -0
index c3a845220e10..b3f8a115cc23 100644
@@ -160,10 +160,44 @@ static int vkms_plane_atomic_check(struct drm_plane *plane,
 	return 0;
  }
 
Quoted 9 lines

drivers/gpu/drm/vkms/vkms_plane.c static int vkms_plane_atomic_check(struct drm_plane *plane, · line 171

Quoted 9 lines
drivers/gpu/drm/vkms/vkms_plane.c +9 -0
+static int vkms_prepare_fb(struct drm_plane *plane,
+			   struct drm_plane_state *state)
+{
+	struct drm_shadow_plane_state *shadow_plane_state;
+	struct drm_framebuffer *fb = state->fb;
+	int ret;
+
+	if (!fb)
+		return 0;

IIRC this cannot be NULL. Only active planes will be 'prepared'.

Quoted 10 lines
Quoted 10 lines
drivers/gpu/drm/vkms/vkms_plane.c +10 -0
+
+	shadow_plane_state = to_drm_shadow_plane_state(state);
+
+	ret = drm_gem_plane_helper_prepare_fb(plane, state);
+	if (ret)
+		return ret;
+
+	return drm_gem_fb_vmap(fb, shadow_plane_state->map, shadow_plane_state->data);
+}
+
Quoted 8 lines

drivers/gpu/drm/vkms/vkms_plane.c static int vkms_plane_atomic_check(struct drm_plane *plane, · line 189

Quoted 8 lines
drivers/gpu/drm/vkms/vkms_plane.c +8 -0
+static void vkms_cleanup_fb(struct drm_plane *plane,
+			    struct drm_plane_state *state)
+{
+	struct drm_shadow_plane_state *shadow_plane_state;
+	struct drm_framebuffer *fb = state->fb;
+
+	if (!fb)
+		return;

Same here. This function won't be called if there has not been a framebuffer.

Quoted 10 lines

drivers/gpu/drm/vkms/vkms_plane.c static int vkms_plane_atomic_check(struct drm_plane *plane, · line 166

Quoted 10 lines
drivers/gpu/drm/vkms/vkms_plane.c +6 -1
+
+	shadow_plane_state = to_drm_shadow_plane_state(state);
+
+	drm_gem_fb_vunmap(fb, shadow_plane_state->map);
+}
+
  static const struct drm_plane_helper_funcs vkms_primary_helper_funcs = {
 	.atomic_update		= vkms_plane_atomic_update,
 	.atomic_check		= vkms_plane_atomic_check,
-	DRM_GEM_SHADOW_PLANE_HELPER_FUNCS,

You're still installing {being/end}_fb_access, which should not be necessary now. Open-coding DRM_GEM_SHADOW_PLANE_HELPER_FUNCS without those helpers would fix that.

Best regards
Thomas

Quoted 8 lines
Quoted 8 lines
drivers/gpu/drm/vkms/vkms_plane.c +2 -1
+	.prepare_fb		= vkms_prepare_fb,
+	.cleanup_fb		= vkms_cleanup_fb,
  };
 
  struct vkms_plane *vkms_plane_init(struct vkms_device *vkmsdev,
--

2.39.0

Signature · 6 lines
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Maxfeldstr. 5, 90409 Nürnberg, Germany
(HRB 36809, AG Nürnberg)
Geschäftsführer: Ivo Totev
#3 lore raw
Quoted 25 lines · Thomas Zimmermann

Hi

Am 05.01.23 um 19:43 schrieb Melissa Wen:

Quoted 15 lines · Maíra Canal
Quoted 8 lines

With commit 359c6649cd9a ("drm/gem: Implement shadow-plane {begin, end}_fb_access with vmap"), the behavior of the shadow-plane helpers changed and the vunmap is now performed at the end of
the current pageflip, instead of the end of the following pageflip.

By performing the vunmap at the end of the current pageflip, invalid memory is accessed by the vkms during the plane composition, as the data is being unmapped before being used.

Hi Maíra,

Thanks for investigating this issue. Can you add in the commit message the kernel Oops caused by this change?

Quoted 6 lines

Besides that, I wonder if the right thing would be to restore the previous behavior of vunmap in shadow-plane helpers, instead of reintroduce driver-specific hooks for vmap/vunmap correctly to vkms.

Maybe Thomas has some inputs on this shadow-plane changing to help us on figuring out the proper fix (?)

Quoted 6 lines

The fix looks good. I left some minor-important comments below.

I would suggest to rethink the overall driver design. Instead of keeping these buffer pinned for long. It might be better to have a per-plane intermediate buffer that you update on each call to atomic_update. That's how real drivers interact with their hardware.

That would mean more copying, and vkms already copies a lot by recomputing the crc every frame. Fundamentally with vkms the cpu is the display engine, and it needs a mapping for as long as the buffer is in use.

Also I guess we really need gitlab ci going, it's a bit silly we're breaking pure sw drivers :-/
-Daniel

Quoted 96 lines
Quoted 13 lines

Best Regards,

Melissa

Quoted 7 lines

Therefore, introduce again prepare_fb and cleanup_fb functions to the vkms, which were previously removed on commit b43e2ec03b0d ("drm/vkms:
Let shadow-plane helpers prepare the plane's FB").

Fixes 359c6649cd9a ("drm/gem: Implement shadow-plane {begin, end}_fb_access with vmap")
Quoted 12 lines
Quoted 12 lines
---
  drivers/gpu/drm/vkms/vkms_plane.c | 36 ++++++++++++++++++++++++++++++-
  1 file changed, 35 insertions(+), 1 deletion(-)
drivers/gpu/drm/vkms/vkms_plane.c +0 -0
index c3a845220e10..b3f8a115cc23 100644
@@ -160,10 +160,44 @@ static int vkms_plane_atomic_check(struct drm_plane *plane,
 	return 0;
  }
 
Quoted 9 lines

drivers/gpu/drm/vkms/vkms_plane.c static int vkms_plane_atomic_check(struct drm_plane *plane, · line 171

Quoted 9 lines
drivers/gpu/drm/vkms/vkms_plane.c +9 -0
+static int vkms_prepare_fb(struct drm_plane *plane,
+			   struct drm_plane_state *state)
+{
+	struct drm_shadow_plane_state *shadow_plane_state;
+	struct drm_framebuffer *fb = state->fb;
+	int ret;
+
+	if (!fb)
+		return 0;

IIRC this cannot be NULL. Only active planes will be 'prepared'.

Quoted 10 lines
Quoted 10 lines
drivers/gpu/drm/vkms/vkms_plane.c +10 -0
+
+	shadow_plane_state = to_drm_shadow_plane_state(state);
+
+	ret = drm_gem_plane_helper_prepare_fb(plane, state);
+	if (ret)
+		return ret;
+
+	return drm_gem_fb_vmap(fb, shadow_plane_state->map, shadow_plane_state->data);
+}
+
Quoted 8 lines

drivers/gpu/drm/vkms/vkms_plane.c static int vkms_plane_atomic_check(struct drm_plane *plane, · line 189

Quoted 8 lines
drivers/gpu/drm/vkms/vkms_plane.c +8 -0
+static void vkms_cleanup_fb(struct drm_plane *plane,
+			    struct drm_plane_state *state)
+{
+	struct drm_shadow_plane_state *shadow_plane_state;
+	struct drm_framebuffer *fb = state->fb;
+
+	if (!fb)
+		return;

Same here. This function won't be called if there has not been a framebuffer.

Quoted 10 lines

drivers/gpu/drm/vkms/vkms_plane.c static int vkms_plane_atomic_check(struct drm_plane *plane, · line 166

Quoted 10 lines
drivers/gpu/drm/vkms/vkms_plane.c +6 -1
+
+	shadow_plane_state = to_drm_shadow_plane_state(state);
+
+	drm_gem_fb_vunmap(fb, shadow_plane_state->map);
+}
+
  static const struct drm_plane_helper_funcs vkms_primary_helper_funcs = {
 	.atomic_update		= vkms_plane_atomic_update,
 	.atomic_check		= vkms_plane_atomic_check,
-	DRM_GEM_SHADOW_PLANE_HELPER_FUNCS,

You're still installing {being/end}_fb_access, which should not be necessary now. Open-coding DRM_GEM_SHADOW_PLANE_HELPER_FUNCS without those helpers would fix that.

Best regards
Thomas

Quoted 8 lines
Quoted 8 lines
drivers/gpu/drm/vkms/vkms_plane.c +2 -1
+	.prepare_fb		= vkms_prepare_fb,
+	.cleanup_fb		= vkms_cleanup_fb,
  };
 
  struct vkms_plane *vkms_plane_init(struct vkms_device *vkmsdev,
--

2.39.0

Signature · 6 lines
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Maxfeldstr. 5, 90409 Nürnberg, Germany
(HRB 36809, AG Nürnberg)
Geschäftsführer: Ivo Totev
Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
#4 lore raw
In reply to Thomas Zimmermann

Hi,

Thanks for the review!

Quoted 79 lines · Thomas Zimmermann

Hi

Am 05.01.23 um 19:43 schrieb Melissa Wen:

Quoted 15 lines · Maíra Canal
Quoted 8 lines

With commit 359c6649cd9a ("drm/gem: Implement shadow-plane {begin, end}_fb_access with vmap"), the behavior of the shadow-plane helpers changed and the vunmap is now performed at the end of
the current pageflip, instead of the end of the following pageflip.

By performing the vunmap at the end of the current pageflip, invalid memory is accessed by the vkms during the plane composition, as the data is being unmapped before being used.

Hi Maíra,

Thanks for investigating this issue. Can you add in the commit message the kernel Oops caused by this change?

Quoted 6 lines

Besides that, I wonder if the right thing would be to restore the previous behavior of vunmap in shadow-plane helpers, instead of reintroduce driver-specific hooks for vmap/vunmap correctly to vkms.

Maybe Thomas has some inputs on this shadow-plane changing to help us on figuring out the proper fix (?)

The fix looks good. I left some minor-important comments below.

I would suggest to rethink the overall driver design. Instead of keeping these buffer pinned for long. It might be better to have a per-plane intermediate buffer that you update on each call to atomic_update. That's how real drivers interact with their hardware.

Quoted 12 lines

Best Regards,

Melissa

Quoted 7 lines

Therefore, introduce again prepare_fb and cleanup_fb functions to the vkms, which were previously removed on commit b43e2ec03b0d ("drm/vkms:
Let shadow-plane helpers prepare the plane's FB").

Fixes 359c6649cd9a ("drm/gem: Implement shadow-plane {begin, end}_fb_access with vmap")
Quoted 12 lines
Quoted 12 lines

---
  drivers/gpu/drm/vkms/vkms_plane.c | 36 ++++++++++++++++++++++++++++++-

  1 file changed, 35 insertions(+), 1 deletion(-)
drivers/gpu/drm/vkms/vkms_plane.c +0 -0
index c3a845220e10..b3f8a115cc23 100644
@@ -160,10 +160,44 @@ static int vkms_plane_atomic_check(struct drm_plane *plane,
     return 0;
   }
 
Quoted 9 lines

drivers/gpu/drm/vkms/vkms_plane.c static int vkms_plane_atomic_check(struct drm_plane *plane, · line 171

Quoted 9 lines
drivers/gpu/drm/vkms/vkms_plane.c +9 -0
+static int vkms_prepare_fb(struct drm_plane *plane,
+               struct drm_plane_state *state)
+{
+    struct drm_shadow_plane_state *shadow_plane_state;
+    struct drm_framebuffer *fb = state->fb;
+    int ret;
+
+    if (!fb)
+        return 0;

IIRC this cannot be NULL. Only active planes will be 'prepared'.>

Quoted 12 lines
Quoted 12 lines
drivers/gpu/drm/vkms/vkms_plane.c +12 -0
+
+    shadow_plane_state = to_drm_shadow_plane_state(state);
+
+    ret = drm_gem_plane_helper_prepare_fb(plane, state);
+    if (ret)
+        return ret;
+
+    return drm_gem_fb_vmap(fb, shadow_plane_state->map, shadow_plane_state->data);
+}
+
+static void vkms_cleanup_fb(struct drm_plane *plane,
+                struct drm_plane_state *state)
Quoted 8 lines
Quoted 6 lines

drivers/gpu/drm/vkms/vkms_plane.c static int vkms_plane_atomic_check(struct drm_plane *plane, · line 189

Quoted 6 lines
drivers/gpu/drm/vkms/vkms_plane.c +6 -0
+{
+    struct drm_shadow_plane_state *shadow_plane_state;
+    struct drm_framebuffer *fb = state->fb;
+
+    if (!fb)
+        return;

Same here. This function won't be called if there has not been a framebuffer.

After removing those two checks, I started to get some NULL pointer dereference errors, so I believe they are somehow necessary.

Quoted 13 lines
Quoted 11 lines

drivers/gpu/drm/vkms/vkms_plane.c static int vkms_plane_atomic_check(struct drm_plane *plane, · line 166

Quoted 11 lines
drivers/gpu/drm/vkms/vkms_plane.c +6 -1
+
+    shadow_plane_state = to_drm_shadow_plane_state(state);
+
+    drm_gem_fb_vunmap(fb, shadow_plane_state->map);
+}
+
   static const struct drm_plane_helper_funcs vkms_primary_helper_funcs = {
     .atomic_update        = vkms_plane_atomic_update,
     .atomic_check        = vkms_plane_atomic_check,
-    DRM_GEM_SHADOW_PLANE_HELPER_FUNCS,

You're still installing {being/end}_fb_access, which should not be necessary now. Open-coding DRM_GEM_SHADOW_PLANE_HELPER_FUNCS without those helpers would fix that.

I'm sorry but I didn't understand this comment. AFAIK I {being/end}_fb_access are NULL as I removed the DRM_GEM_SHADOW_PLANE_HELPER_FUNCS macro.

Best Regards,
- Maíra Canal

Quoted 13 lines

Best regards
Thomas

Quoted 8 lines
Quoted 8 lines
drivers/gpu/drm/vkms/vkms_plane.c +2 -0
+    .prepare_fb        = vkms_prepare_fb,
+    .cleanup_fb        = vkms_cleanup_fb,
   };
 
   struct vkms_plane *vkms_plane_init(struct vkms_device *vkmsdev,
Signature · 1 line
2.39.0
#5 lore raw

Hi

Am 06.01.23 um 12:34 schrieb Maíra Canal:

Quoted 96 lines

Hi,

Thanks for the review!

Quoted 82 lines · Thomas Zimmermann

Hi

Am 05.01.23 um 19:43 schrieb Melissa Wen:

Quoted 15 lines · Maíra Canal
Quoted 8 lines

With commit 359c6649cd9a ("drm/gem: Implement shadow-plane {begin, end}_fb_access with vmap"), the behavior of the shadow-plane helpers changed and the vunmap is now performed at the end of
the current pageflip, instead of the end of the following pageflip.

By performing the vunmap at the end of the current pageflip, invalid memory is accessed by the vkms during the plane composition, as the data is being unmapped before being used.

Hi Maíra,

Thanks for investigating this issue. Can you add in the commit message the kernel Oops caused by this change?

Quoted 6 lines

Besides that, I wonder if the right thing would be to restore the previous behavior of vunmap in shadow-plane helpers, instead of reintroduce driver-specific hooks for vmap/vunmap correctly to vkms.

Maybe Thomas has some inputs on this shadow-plane changing to help us on figuring out the proper fix (?)

The fix looks good. I left some minor-important comments below.

I would suggest to rethink the overall driver design. Instead of keeping these buffer pinned for long. It might be better to have a per-plane intermediate buffer that you update on each call to atomic_update. That's how real drivers interact with their hardware.

Quoted 12 lines

Best Regards,

Melissa

Quoted 7 lines

Therefore, introduce again prepare_fb and cleanup_fb functions to the vkms, which were previously removed on commit b43e2ec03b0d ("drm/vkms:
Let shadow-plane helpers prepare the plane's FB").

Fixes 359c6649cd9a ("drm/gem: Implement shadow-plane {begin, end}_fb_access with vmap")
Quoted 14 lines
Quoted 14 lines

---
  drivers/gpu/drm/vkms/vkms_plane.c | 36
++++++++++++++++++++++++++++++-

  1 file changed, 35 insertions(+), 1 deletion(-)
drivers/gpu/drm/vkms/vkms_plane.c +0 -0
index c3a845220e10..b3f8a115cc23 100644
@@ -160,10 +160,44 @@ static int vkms_plane_atomic_check(struct 
 drm_plane *plane,
     return 0;
   }
 
Quoted 9 lines

drivers/gpu/drm/vkms/vkms_plane.c static int vkms_plane_atomic_check(struct · line 172

Quoted 9 lines
drivers/gpu/drm/vkms/vkms_plane.c +9 -0
+static int vkms_prepare_fb(struct drm_plane *plane,
+               struct drm_plane_state *state)
+{
+    struct drm_shadow_plane_state *shadow_plane_state;
+    struct drm_framebuffer *fb = state->fb;
+    int ret;
+
+    if (!fb)
+        return 0;

IIRC this cannot be NULL. Only active planes will be 'prepared'.>

Quoted 13 lines
Quoted 13 lines
drivers/gpu/drm/vkms/vkms_plane.c +12 -0
+
+    shadow_plane_state = to_drm_shadow_plane_state(state);
+
+    ret = drm_gem_plane_helper_prepare_fb(plane, state);
+    if (ret)
+        return ret;
+
+    return drm_gem_fb_vmap(fb, shadow_plane_state->map, 
 shadow_plane_state->data);
+}
+
+static void vkms_cleanup_fb(struct drm_plane *plane,
+                struct drm_plane_state *state)
Quoted 8 lines
Quoted 6 lines

drivers/gpu/drm/vkms/vkms_plane.c static int vkms_plane_atomic_check(struct · line 191

Quoted 6 lines
drivers/gpu/drm/vkms/vkms_plane.c +6 -0
+{
+    struct drm_shadow_plane_state *shadow_plane_state;
+    struct drm_framebuffer *fb = state->fb;
+
+    if (!fb)
+        return;

Same here. This function won't be called if there has not been a framebuffer.

Quoted 2 lines

After removing those two checks, I started to get some NULL pointer dereference errors, so I believe they are somehow necessary.

Ok.

Quoted 15 lines
Quoted 14 lines
Quoted 12 lines

drivers/gpu/drm/vkms/vkms_plane.c static int vkms_plane_atomic_check(struct · line 168

Quoted 12 lines
drivers/gpu/drm/vkms/vkms_plane.c +6 -1
+
+    shadow_plane_state = to_drm_shadow_plane_state(state);
+
+    drm_gem_fb_vunmap(fb, shadow_plane_state->map);
+}
+
   static const struct drm_plane_helper_funcs vkms_primary_helper_funcs = {
     .atomic_update        = vkms_plane_atomic_update,
     .atomic_check        = vkms_plane_atomic_check,
-    DRM_GEM_SHADOW_PLANE_HELPER_FUNCS,

You're still installing {being/end}_fb_access, which should not be necessary now. Open-coding DRM_GEM_SHADOW_PLANE_HELPER_FUNCS without those helpers would fix that.

Quoted 2 lines

I'm sorry but I didn't understand this comment. AFAIK I {being/end}_fb_access are NULL as I removed the DRM_GEM_SHADOW_PLANE_HELPER_FUNCS macro.

Sorry, I misread that line. You're right.

Best regards
Thomas

Quoted 17 lines

Best Regards,
- Maíra Canal

Quoted 13 lines

Best regards
Thomas

Quoted 8 lines
Quoted 8 lines
drivers/gpu/drm/vkms/vkms_plane.c +2 -0
+    .prepare_fb        = vkms_prepare_fb,
+    .cleanup_fb        = vkms_cleanup_fb,
   };

  struct vkms_plane *vkms_plane_init(struct vkms_device *vkmsdev,

Signature · 1 line
2.39.0
Signature · 6 lines
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Maxfeldstr. 5, 90409 Nürnberg, Germany
(HRB 36809, AG Nürnberg)
Geschäftsführer: Ivo Totev
#6 lore raw
In reply to Maíra Canal
Quoted 93 lines · Maíra Canal

Hi,

Thanks for the review!

Quoted 79 lines · Thomas Zimmermann

Hi

Am 05.01.23 um 19:43 schrieb Melissa Wen:

Quoted 15 lines · Maíra Canal
Quoted 8 lines

With commit 359c6649cd9a ("drm/gem: Implement shadow-plane {begin, end}_fb_access with vmap"), the behavior of the shadow-plane helpers changed and the vunmap is now performed at the end of
the current pageflip, instead of the end of the following pageflip.

By performing the vunmap at the end of the current pageflip, invalid memory is accessed by the vkms during the plane composition, as the data is being unmapped before being used.

Hi Maíra,

Thanks for investigating this issue. Can you add in the commit message the kernel Oops caused by this change?

Quoted 6 lines

Besides that, I wonder if the right thing would be to restore the previous behavior of vunmap in shadow-plane helpers, instead of reintroduce driver-specific hooks for vmap/vunmap correctly to vkms.

Maybe Thomas has some inputs on this shadow-plane changing to help us on figuring out the proper fix (?)

The fix looks good. I left some minor-important comments below.

I would suggest to rethink the overall driver design. Instead of keeping these buffer pinned for long. It might be better to have a per-plane intermediate buffer that you update on each call to atomic_update. That's how real drivers interact with their hardware.

Quoted 12 lines

Best Regards,

Melissa

Quoted 7 lines

Therefore, introduce again prepare_fb and cleanup_fb functions to the vkms, which were previously removed on commit b43e2ec03b0d ("drm/vkms:
Let shadow-plane helpers prepare the plane's FB").

Fixes 359c6649cd9a ("drm/gem: Implement shadow-plane {begin, end}_fb_access with vmap")
Quoted 12 lines
Quoted 12 lines

---
  drivers/gpu/drm/vkms/vkms_plane.c | 36 ++++++++++++++++++++++++++++++-

  1 file changed, 35 insertions(+), 1 deletion(-)
drivers/gpu/drm/vkms/vkms_plane.c +0 -0
index c3a845220e10..b3f8a115cc23 100644
@@ -160,10 +160,44 @@ static int vkms_plane_atomic_check(struct drm_plane *plane,
     return 0;
   }
 
Quoted 9 lines

drivers/gpu/drm/vkms/vkms_plane.c static int vkms_plane_atomic_check(struct drm_plane *plane, · line 171

Quoted 9 lines
drivers/gpu/drm/vkms/vkms_plane.c +9 -0
+static int vkms_prepare_fb(struct drm_plane *plane,
+               struct drm_plane_state *state)
+{
+    struct drm_shadow_plane_state *shadow_plane_state;
+    struct drm_framebuffer *fb = state->fb;
+    int ret;
+
+    if (!fb)
+        return 0;

IIRC this cannot be NULL. Only active planes will be 'prepared'.>

Quoted 12 lines
Quoted 12 lines
drivers/gpu/drm/vkms/vkms_plane.c +12 -0
+
+    shadow_plane_state = to_drm_shadow_plane_state(state);
+
+    ret = drm_gem_plane_helper_prepare_fb(plane, state);
+    if (ret)
+        return ret;
+
+    return drm_gem_fb_vmap(fb, shadow_plane_state->map, shadow_plane_state->data);
+}
+
+static void vkms_cleanup_fb(struct drm_plane *plane,
+                struct drm_plane_state *state)
Quoted 8 lines
Quoted 6 lines

drivers/gpu/drm/vkms/vkms_plane.c static int vkms_plane_atomic_check(struct drm_plane *plane, · line 189

Quoted 6 lines
drivers/gpu/drm/vkms/vkms_plane.c +6 -0
+{
+    struct drm_shadow_plane_state *shadow_plane_state;
+    struct drm_framebuffer *fb = state->fb;
+
+    if (!fb)
+        return;

Same here. This function won't be called if there has not been a framebuffer.

Quoted 2 lines

After removing those two checks, I started to get some NULL pointer dereference errors, so I believe they are somehow necessary.

I'm honestly not sure whether any driver does not have this check ... might be worth to go through them, and if they all have it, pull it into helpers. It does look a bit silly like that :-)
-Daniel

Quoted 34 lines
Quoted 14 lines
Quoted 12 lines

drivers/gpu/drm/vkms/vkms_plane.c static int vkms_plane_atomic_check(struct drm_plane *plane, · line 166

Quoted 12 lines
drivers/gpu/drm/vkms/vkms_plane.c +6 -1
+
+    shadow_plane_state = to_drm_shadow_plane_state(state);
+
+    drm_gem_fb_vunmap(fb, shadow_plane_state->map);
+}
+
   static const struct drm_plane_helper_funcs vkms_primary_helper_funcs = {
     .atomic_update        = vkms_plane_atomic_update,
     .atomic_check        = vkms_plane_atomic_check,
-    DRM_GEM_SHADOW_PLANE_HELPER_FUNCS,

You're still installing {being/end}_fb_access, which should not be necessary now. Open-coding DRM_GEM_SHADOW_PLANE_HELPER_FUNCS without those helpers would fix that.

I'm sorry but I didn't understand this comment. AFAIK I {being/end}_fb_access are NULL as I removed the DRM_GEM_SHADOW_PLANE_HELPER_FUNCS macro.

Best Regards,
- Maíra Canal

Quoted 13 lines

Best regards
Thomas

Quoted 8 lines
Quoted 8 lines
drivers/gpu/drm/vkms/vkms_plane.c +2 -0
+    .prepare_fb        = vkms_prepare_fb,
+    .cleanup_fb        = vkms_cleanup_fb,
   };
 
   struct vkms_plane *vkms_plane_init(struct vkms_device *vkmsdev,
Signature · 1 line
2.39.0
Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch