Skip to contentSkip to conversation

drm/amdgpu: avoid using null object of framebuffer

PATCH In review Julia Zhang 2 participants 4 messages Jun 19, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 drm/amdgpu: avoid using null object of framebuffer

Instead of using state->fb->obj[0] directly, get object from framebuffer by calling drm_gem_fb_get_obj() and return error code when object is null to avoid using null object of framebuffer.

v2: Call drm_gem_fb_get_obj after check old_state->fb for NULL.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vkms.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vkms.c +16 -2
index d60c4a2eeb0c..212f6522859d 100644
@@ -2,6 +2,7 @@
 
 #include <drm/drm_atomic_helper.h>
 #include <drm/drm_simple_kms_helper.h>
+#include <drm/drm_gem_framebuffer_helper.h>
 #include <drm/drm_vblank.h>
 
 #include "amdgpu.h"
@@ -311,7 +312,13 @@ static int amdgpu_vkms_prepare_fb(struct drm_plane *plane,
 		return 0;
 	}
 	afb = to_amdgpu_framebuffer(new_state->fb);
-	obj = new_state->fb->obj[0];
+
+	obj = drm_gem_fb_get_obj(new_state->fb, 0);
+	if (!obj) {
+		DRM_ERROR("Failed to get obj from framebuffer\n");
+		return -EINVAL;
+	}
+
 	rbo = gem_to_amdgpu_bo(obj);
 	adev = amdgpu_ttm_adev(rbo->tbo.bdev);
 
@@ -365,12 +372,19 @@ static void amdgpu_vkms_cleanup_fb(struct drm_plane *plane,
 				   struct drm_plane_state *old_state)
 {
 	struct amdgpu_bo *rbo;
+	struct drm_gem_object *obj;
 	int r;
 
 	if (!old_state->fb)
 		return;
 
-	rbo = gem_to_amdgpu_bo(old_state->fb->obj[0]);
+	obj = drm_gem_fb_get_obj(old_state->fb, 0);
+	if (!obj) {
+		DRM_ERROR("Failed to get obj from framebuffer\n");
+		return;
+	}
+
+	rbo = gem_to_amdgpu_bo(obj);
 	r = amdgpu_bo_reserve(rbo, false);
 	if (unlikely(r)) {
 		DRM_ERROR("failed to reserve rbo before unpin\n");
Signature · 1 line
2.34.1
Reviewed-by on v2 1/1
#1 lore raw
Quoted 8 lines · Julia

Instead of using state->fb->obj[0] directly, get object from framebuffer by calling drm_gem_fb_get_obj() and return error code when object is null to avoid using null object of framebuffer.

v2: Call drm_gem_fb_get_obj after check old_state->fb for NULL.

---

Julia, you can add my RB during V1 in the next submission.

Quoted 54 lines
 drivers/gpu/drm/amd/amdgpu/amdgpu_vkms.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vkms.c +16 -2
index d60c4a2eeb0c..212f6522859d 100644
@@ -2,6 +2,7 @@
 
 #include <drm/drm_atomic_helper.h>
 #include <drm/drm_simple_kms_helper.h>
+#include <drm/drm_gem_framebuffer_helper.h>
 #include <drm/drm_vblank.h>
 
 #include "amdgpu.h"
@@ -311,7 +312,13 @@ static int amdgpu_vkms_prepare_fb(struct drm_plane *plane,
 		return 0;
 	}
 	afb = to_amdgpu_framebuffer(new_state->fb);
-	obj = new_state->fb->obj[0];
+
+	obj = drm_gem_fb_get_obj(new_state->fb, 0);
+	if (!obj) {
+		DRM_ERROR("Failed to get obj from framebuffer\n");
+		return -EINVAL;
+	}
+
 	rbo = gem_to_amdgpu_bo(obj);
 	adev = amdgpu_ttm_adev(rbo->tbo.bdev);
 
@@ -365,12 +372,19 @@ static void amdgpu_vkms_cleanup_fb(struct drm_plane *plane,
 				   struct drm_plane_state *old_state)
 {
 	struct amdgpu_bo *rbo;
+	struct drm_gem_object *obj;
 	int r;
 
 	if (!old_state->fb)
 		return;
 
-	rbo = gem_to_amdgpu_bo(old_state->fb->obj[0]);
+	obj = drm_gem_fb_get_obj(old_state->fb, 0);
+	if (!obj) {
+		DRM_ERROR("Failed to get obj from framebuffer\n");
+		return;
+	}
+
+	rbo = gem_to_amdgpu_bo(obj);
 	r = amdgpu_bo_reserve(rbo, false);
 	if (unlikely(r)) {
 		DRM_ERROR("failed to reserve rbo before unpin\n");
Signature · 1 line
2.34.1
#2 lore raw
Quoted 5 lines · Huang Rui
Quoted 4 lines · Julia

Instead of using state->fb->obj[0] directly, get object from framebuffer by calling drm_gem_fb_get_obj() and return error code when object is null to avoid using null object of framebuffer.

Quoted 8 lines
Quoted 4 lines

v2: Call drm_gem_fb_get_obj after check old_state->fb for NULL.

---

Julia, you can add my RB during V1 in the next submission.

CC Fusheng who reported the issue.

Fusheng, may we have your reported-by?

Quoted 55 lines
Quoted 55 lines
 drivers/gpu/drm/amd/amdgpu/amdgpu_vkms.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vkms.c +16 -2
index d60c4a2eeb0c..212f6522859d 100644
@@ -2,6 +2,7 @@
 
 #include <drm/drm_atomic_helper.h>
 #include <drm/drm_simple_kms_helper.h>
+#include <drm/drm_gem_framebuffer_helper.h>
 #include <drm/drm_vblank.h>
 
 #include "amdgpu.h"
@@ -311,7 +312,13 @@ static int amdgpu_vkms_prepare_fb(struct drm_plane *plane,
 		return 0;
 	}
 	afb = to_amdgpu_framebuffer(new_state->fb);
-	obj = new_state->fb->obj[0];
+
+	obj = drm_gem_fb_get_obj(new_state->fb, 0);
+	if (!obj) {
+		DRM_ERROR("Failed to get obj from framebuffer\n");
+		return -EINVAL;
+	}
+
 	rbo = gem_to_amdgpu_bo(obj);
 	adev = amdgpu_ttm_adev(rbo->tbo.bdev);
 
@@ -365,12 +372,19 @@ static void amdgpu_vkms_cleanup_fb(struct drm_plane *plane,
 				   struct drm_plane_state *old_state)
 {
 	struct amdgpu_bo *rbo;
+	struct drm_gem_object *obj;
 	int r;
 
 	if (!old_state->fb)
 		return;
 
-	rbo = gem_to_amdgpu_bo(old_state->fb->obj[0]);
+	obj = drm_gem_fb_get_obj(old_state->fb, 0);
+	if (!obj) {
+		DRM_ERROR("Failed to get obj from framebuffer\n");
+		return;
+	}
+
+	rbo = gem_to_amdgpu_bo(obj);
 	r = amdgpu_bo_reserve(rbo, false);
 	if (unlikely(r)) {
 		DRM_ERROR("failed to reserve rbo before unpin\n");
Signature · 1 line
2.34.1
author Reviewed-by Huang Rui
#3 lore raw

On 2024/6/19 12:53, Huang Rui wrote:

On Wed, Jun 19, 2024 at 12:46:25PM +0800, Huang Rui wrote:

On Wed, Jun 19, 2024 at 12:29:24PM +0800, Zhang, Julia wrote:

Instead of using state->fb->obj[0] directly, get object from framebuffer

by calling drm_gem_fb_get_obj() and return error code when object is

null to avoid using null object of framebuffer.

v2: Call drm_gem_fb_get_obj after check old_state->fb for NULL.

---

Julia, you can add my RB during V1 in the next submission.

CC Fusheng who reported the issue.

Fusheng, may we have your reported-by?

Hi Fusheng,

Can you help to review this patch and may we have you reported-by? Thank you.

Julia

 drivers/gpu/drm/amd/amdgpu/amdgpu_vkms.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vkms.c +0 -0

index d60c4a2eeb0c..212f6522859d 100644

--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vkms.c

+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vkms.c

@@ -2,6 +2,7 @@

#include <drm/drm_atomic_helper.h>

#include <drm/drm_simple_kms_helper.h>

+#include <drm/drm_gem_framebuffer_helper.h>

#include <drm/drm_vblank.h>

#include "amdgpu.h"

@@ -311,7 +312,13 @@ static int amdgpu_vkms_prepare_fb(struct drm_plane *plane,

             return 0;

     }

     afb = to_amdgpu_framebuffer(new_state->fb);

- obj = new_state->fb->obj[0];

+

+ obj = drm_gem_fb_get_obj(new_state->fb, 0);

+ if (!obj) {

+ DRM_ERROR("Failed to get obj from framebuffer\n");

+ return -EINVAL;

+ }

+

     rbo = gem_to_amdgpu_bo(obj);

     adev = amdgpu_ttm_adev(rbo->tbo.bdev);

@@ -365,12 +372,19 @@ static void amdgpu_vkms_cleanup_fb(struct drm_plane *plane,

struct drm_plane_state *old_state)

{

     struct amdgpu_bo *rbo;

+ struct drm_gem_object *obj;

     int r;



     if (!old_state->fb)

             return;

- rbo = gem_to_amdgpu_bo(old_state->fb->obj[0]);

+ obj = drm_gem_fb_get_obj(old_state->fb, 0);

+ if (!obj) {

+ DRM_ERROR("Failed to get obj from framebuffer\n");

+ return;

+ }

+

+ rbo = gem_to_amdgpu_bo(obj);

     r = amdgpu_bo_reserve(rbo, false);

     if (unlikely(r)) {

         DRM_ERROR("failed to reserve rbo before unpin\n");

--

2.34.1