Skip to contentSkip to conversation

drm/amdgpu: validate if sw_init is defined or NULL

PATCH Superseded Sunil Khatri 2 participants 6 messages Oct 9, 2024

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 drm/amdgpu: validate if sw_init is defined or NULL

Before making a function call to sw_init, validate the function pointer.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +7 -5
index 38a7423101f3..4a6def74964e 100644
@@ -2851,13 +2851,15 @@ static int amdgpu_device_ip_init(struct amdgpu_device *adev)
 	for (i = 0; i < adev->num_ip_blocks; i++) {
 		if (!adev->ip_blocks[i].status.valid)
 			continue;
-		r = adev->ip_blocks[i].version->funcs->sw_init(&adev->ip_blocks[i]);
-		if (r) {
-			DRM_ERROR("sw_init of IP block <%s> failed %d\n",
+		if (adev->ip_blocks[i].version->funcs->sw_init) {
+			r = adev->ip_blocks[i].version->funcs->sw_init(&adev->ip_blocks[i]);
+			if (r) {
+				DRM_ERROR("sw_init of IP block <%s> failed %d\n",
 				  adev->ip_blocks[i].version->funcs->name, r);
-			goto init_failed;
+				goto init_failed;
+			}
+			adev->ip_blocks[i].status.sw = true;
 		}
-		adev->ip_blocks[i].status.sw = true;
 
 		if (!amdgpu_ip_member_of_hwini(
 			    adev, adev->ip_blocks[i].version->type))
Signature · 1 line
2.34.1
#1 lore raw

PATCH 2/2 drm/admgpu: clean the dummy sw_init functions

Remove the dummy sw_init functions and set the
corresponding functions to NULL.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_isp.c | 5 -----
 drivers/gpu/drm/amd/amdgpu/cik.c        | 7 +------
 drivers/gpu/drm/amd/amdgpu/si.c         | 7 +------
 3 files changed, 2 insertions(+), 17 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_isp.c +0 -5
index adc0b80ca5db..4add6c412779 100644
@@ -33,11 +33,6 @@
 #include "isp_v4_1_0.h"
 #include "isp_v4_1_1.h"
 
-static int isp_sw_init(struct amdgpu_ip_block *ip_block)
-{
-	return 0;
-}
-
 static int isp_sw_fini(struct amdgpu_ip_block *ip_block)
 {
 	return 0;
drivers/gpu/drm/amd/amdgpu/cik.c +1 -6
index 6c18ab35cf69..bb961b274487 100644
@@ -2124,11 +2124,6 @@ static int cik_common_early_init(struct amdgpu_ip_block *ip_block)
 	return 0;
 }
 
-static int cik_common_sw_init(struct amdgpu_ip_block *ip_block)
-{
-	return 0;
-}
-
 static int cik_common_sw_fini(struct amdgpu_ip_block *ip_block)
 {
 	return 0;
@@ -2195,7 +2190,7 @@ static const struct amd_ip_funcs cik_common_ip_funcs = {
 	.name = "cik_common",
 	.early_init = cik_common_early_init,
 	.late_init = NULL,
-	.sw_init = cik_common_sw_init,
+	.sw_init = NULL,
 	.sw_fini = cik_common_sw_fini,
 	.hw_init = cik_common_hw_init,
 	.hw_fini = cik_common_hw_fini,
drivers/gpu/drm/amd/amdgpu/si.c +1 -6
index b9934661a92e..0578c7ce4dc5 100644
@@ -2148,11 +2148,6 @@ static int si_common_early_init(struct amdgpu_ip_block *ip_block)
 	return 0;
 }
 
-static int si_common_sw_init(struct amdgpu_ip_block *ip_block)
-{
-	return 0;
-}
-
 static int si_common_sw_fini(struct amdgpu_ip_block *ip_block)
 {
 	return 0;
@@ -2691,7 +2686,7 @@ static const struct amd_ip_funcs si_common_ip_funcs = {
 	.name = "si_common",
 	.early_init = si_common_early_init,
 	.late_init = NULL,
-	.sw_init = si_common_sw_init,
+	.sw_init = NULL,
 	.sw_fini = si_common_sw_fini,
 	.hw_init = si_common_hw_init,
 	.hw_fini = si_common_hw_fini,
Signature · 1 line
2.34.1
maintainer
#2 lore raw

Re: [PATCH 2/2] drm/admgpu: clean the dummy sw_init functions

Am 09.10.24 um 10:48 schrieb Sunil Khatri:

Quoted 68 lines

Remove the dummy sw_init functions and set the
corresponding functions to NULL.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_isp.c | 5 -----
  drivers/gpu/drm/amd/amdgpu/cik.c        | 7 +------
  drivers/gpu/drm/amd/amdgpu/si.c         | 7 +------
  3 files changed, 2 insertions(+), 17 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_isp.c +0 -5
index adc0b80ca5db..4add6c412779 100644
@@ -33,11 +33,6 @@
  #include "isp_v4_1_0.h"
  #include "isp_v4_1_1.h"
 
-static int isp_sw_init(struct amdgpu_ip_block *ip_block)
-{
-	return 0;
-}
-
  static int isp_sw_fini(struct amdgpu_ip_block *ip_block)
  {
  	return 0;
drivers/gpu/drm/amd/amdgpu/cik.c +1 -6
index 6c18ab35cf69..bb961b274487 100644
@@ -2124,11 +2124,6 @@ static int cik_common_early_init(struct amdgpu_ip_block *ip_block)
  	return 0;
  }
 
-static int cik_common_sw_init(struct amdgpu_ip_block *ip_block)
-{
-	return 0;
-}
-
  static int cik_common_sw_fini(struct amdgpu_ip_block *ip_block)
  {
  	return 0;
@@ -2195,7 +2190,7 @@ static const struct amd_ip_funcs cik_common_ip_funcs = {
  	.name = "cik_common",
  	.early_init = cik_common_early_init,
  	.late_init = NULL,
-	.sw_init = cik_common_sw_init,
+	.sw_init = NULL,
  	.sw_fini = cik_common_sw_fini,
  	.hw_init = cik_common_hw_init,
  	.hw_fini = cik_common_hw_fini,
drivers/gpu/drm/amd/amdgpu/si.c +0 -5
index b9934661a92e..0578c7ce4dc5 100644
@@ -2148,11 +2148,6 @@ static int si_common_early_init(struct amdgpu_ip_block *ip_block)
  	return 0;
  }
 
-static int si_common_sw_init(struct amdgpu_ip_block *ip_block)
-{
-	return 0;
-}
-
  static int si_common_sw_fini(struct amdgpu_ip_block *ip_block)
  {
  	return 0;

drivers/gpu/drm/amd/amdgpu/si.c static const struct amd_ip_funcs si_common_ip_funcs = { · line 2689

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/si.c +1 -1
@@ -2691,7 +2686,7 @@ static const struct amd_ip_funcs si_common_ip_funcs = {
  	.name = "si_common",
  	.early_init = si_common_early_init,
  	.late_init = NULL,
-	.sw_init = si_common_sw_init,
+	.sw_init = NULL,

BTW don't do that, just don't initialize the field at all.

Uninitialized fields in global variables are NULL or 0 in C by standard definition and Greg usually complains if we do stuff like that.

Regards,
Christian.

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/si.c +0 -0
  	.sw_fini = si_common_sw_fini,
  	.hw_init = si_common_hw_init,
  	.hw_fini = si_common_hw_fini,
#3 lore raw

Re: [PATCH 2/2] drm/admgpu: clean the dummy sw_init functions

Quoted 78 lines · Christian König

Am 09.10.24 um 10:48 schrieb Sunil Khatri:

Quoted 68 lines

Remove the dummy sw_init functions and set the
corresponding functions to NULL.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_isp.c | 5 ----- drivers/gpu/drm/amd/amdgpu/cik.c        | 7 +------ drivers/gpu/drm/amd/amdgpu/si.c         | 7 +------

  3 files changed, 2 insertions(+), 17 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_isp.c +0 -4
index adc0b80ca5db..4add6c412779 100644
@@ -33,11 +33,6 @@
   #include "isp_v4_1_0.h"
   #include "isp_v4_1_1.h"
   -static int isp_sw_init(struct amdgpu_ip_block *ip_block)
-{
-    return 0;
-}
-
   static int isp_sw_fini(struct amdgpu_ip_block *ip_block)
   {
       return 0;
drivers/gpu/drm/amd/amdgpu/cik.c +0 -4
index 6c18ab35cf69..bb961b274487 100644
@@ -2124,11 +2124,6 @@ static int cik_common_early_init(struct 
 amdgpu_ip_block *ip_block)
       return 0;
   }
   -static int cik_common_sw_init(struct amdgpu_ip_block *ip_block)
-{
-    return 0;
-}
-
   static int cik_common_sw_fini(struct amdgpu_ip_block *ip_block)
   {

      return 0;

drivers/gpu/drm/amd/amdgpu/cik.c +1 -1
@@ -2195,7 +2190,7 @@ static const struct amd_ip_funcs 
 cik_common_ip_funcs = {
       .name = "cik_common",
       .early_init = cik_common_early_init,
       .late_init = NULL,
-    .sw_init = cik_common_sw_init,
+    .sw_init = NULL,
       .sw_fini = cik_common_sw_fini,
       .hw_init = cik_common_hw_init,

      .hw_fini = cik_common_hw_fini,

drivers/gpu/drm/amd/amdgpu/si.c +0 -4
index b9934661a92e..0578c7ce4dc5 100644
@@ -2148,11 +2148,6 @@ static int si_common_early_init(struct 
 amdgpu_ip_block *ip_block)
       return 0;
   }
   -static int si_common_sw_init(struct amdgpu_ip_block *ip_block)
-{
-    return 0;
-}
-
   static int si_common_sw_fini(struct amdgpu_ip_block *ip_block)
   {

      return 0;

drivers/gpu/drm/amd/amdgpu/si.c static const struct amd_ip_funcs · line 2690

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/si.c +1 -1
@@ -2691,7 +2686,7 @@ static const struct amd_ip_funcs 
 si_common_ip_funcs = {
       .name = "si_common",
       .early_init = si_common_early_init,
       .late_init = NULL,
-    .sw_init = si_common_sw_init,
+    .sw_init = NULL,

BTW don't do that, just don't initialize the field at all.

Quoted 1 line

Uninitialized fields in global variables are NULL or 0 in C by standard definition and Greg usually complains if we do stuff like that.

Sure will remove the initialization completely for it in v3 patch set.

Quoted 8 lines

Regards,
Christian.

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/si.c +0 -0
       .sw_fini = si_common_sw_fini,
       .hw_init = si_common_hw_init,

      .hw_fini = si_common_hw_fini,

maintainer
#4 lore raw
In reply to Sunil Khatri

Am 09.10.24 um 10:48 schrieb Sunil Khatri:

Quoted 2 lines

Before making a function call to sw_init, validate the function pointer.

Maybe add " like we do for hw_init." or some similar example of optional callback.

Quoted 18 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 12 +++++++-----
  1 file changed, 7 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +1 -3
index 38a7423101f3..4a6def74964e 100644
@@ -2851,13 +2851,15 @@ static int amdgpu_device_ip_init(struct amdgpu_device *adev)
  	for (i = 0; i < adev->num_ip_blocks; i++) {
  		if (!adev->ip_blocks[i].status.valid)
  			continue;
-		r = adev->ip_blocks[i].version->funcs->sw_init(&adev->ip_blocks[i]);
-		if (r) {
-			DRM_ERROR("sw_init of IP block <%s> failed %d\n",
+		if (adev->ip_blocks[i].version->funcs->sw_init) {

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c static int amdgpu_device_ip_init(struct amdgpu_device *adev) · line 2860

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +6 -2
+			r = adev->ip_blocks[i].version->funcs->sw_init(&adev->ip_blocks[i]);
+			if (r) {
+				DRM_ERROR("sw_init of IP block <%s> failed %d\n",
  				  adev->ip_blocks[i].version->funcs->name, r);
-			goto init_failed;
+				goto init_failed;
+			}
+			adev->ip_blocks[i].status.sw = true;
  		}
-		adev->ip_blocks[i].status.sw = true;

I think we should set that to true regardless of the callback being defined or not.

Could be that an IP has a sw_fini callback, but not a sw_init one.

Regards,
Christian.

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +0 -0
  		if (!amdgpu_ip_member_of_hwini(
  			    adev, adev->ip_blocks[i].version->type))
#5 lore raw
Quoted 5 lines · Christian König

Am 09.10.24 um 10:48 schrieb Sunil Khatri:

Quoted 2 lines

Before making a function call to sw_init, validate the function pointer.

Maybe add " like we do for hw_init." or some similar example of optional callback.

Sure will update the commit message also

Quoted 26 lines
Quoted 26 lines

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 12 +++++++-----

  1 file changed, 7 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +4 -3
index 38a7423101f3..4a6def74964e 100644
@@ -2851,13 +2851,15 @@ static int amdgpu_device_ip_init(struct 
 amdgpu_device *adev)
       for (i = 0; i < adev->num_ip_blocks; i++) {
           if (!adev->ip_blocks[i].status.valid)
               continue;
-        r = 
 adev->ip_blocks[i].version->funcs->sw_init(&adev->ip_blocks[i]);
-        if (r) {
-            DRM_ERROR("sw_init of IP block <%s> failed %d\n",
+        if (adev->ip_blocks[i].version->funcs->sw_init) {
+            r = 
 adev->ip_blocks[i].version->funcs->sw_init(&adev->ip_blocks[i]);
+            if (r) {
+                DRM_ERROR("sw_init of IP block <%s> failed %d\n",
 adev->ip_blocks[i].version->funcs->name, r);
Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c static int amdgpu_device_ip_init(struct amdgpu_device *adev) · line 2860

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +3 -2
-            goto init_failed;
+                goto init_failed;
+            }
+            adev->ip_blocks[i].status.sw = true;
           }
-        adev->ip_blocks[i].status.sw = true;

I think we should set that to true regardless of the callback being defined or not.

That exactly is done in v2 patch set already.

Quoted 10 lines

Could be that an IP has a sw_fini callback, but not a sw_init one. Yes there are example of it where sw_init is not there but sw_fini is there

Regards,
Christian.

Quoted 2 lines

            if (!amdgpu_ip_member_of_hwini(
                  adev, adev->ip_blocks[i].version->type))