Skip to contentSkip to conversation

drm/amdkfd: FORWARD NULL

PATCH Superseded Andrew Martin 2 participants 3 messages Nov 18, 2025 – Dec 3, 2025

Subsystems (3)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 drm/amdkfd: FORWARD NULL

This patch fixes issues when the code moves forward with a potential NULL pointer, without checking.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_fence.c | 2 +-
 drivers/gpu/drm/amd/amdkfd/kfd_crat.c            | 2 ++
 drivers/gpu/drm/amd/amdkfd/kfd_debug.c           | 7 ++++++-
 drivers/gpu/drm/amd/amdkfd/kfd_process.c         | 3 ---
 4 files changed, 9 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_fence.c +1 -1
index 1ef758ac5076..73c5749d4243 100644
@@ -105,7 +105,7 @@ static const char *amdkfd_fence_get_timeline_name(struct dma_fence *f)
 {
 	struct amdgpu_amdkfd_fence *fence = to_amdgpu_amdkfd_fence(f);
 
-	return fence->timeline_name;
+	return fence ? fence->timeline_name : NULL;
 }
 
 /**
drivers/gpu/drm/amd/amdkfd/kfd_crat.c +2 -0
index 4a7180b46b71..c086a9ed8c89 100644
@@ -2357,6 +2357,8 @@ static int kfd_create_vcrat_image_gpu(void *pcrat_image,
 	if (kdev->kfd->hive_id) {
 		for (nid = 0; nid < proximity_domain; ++nid) {
 			peer_dev = kfd_topology_device_by_proximity_domain_no_lock(nid);
+			if (!peer_dev)
+				continue;
 			if (!peer_dev->gpu)
 				continue;
 			if (peer_dev->gpu->kfd->hive_id != kdev->kfd->hive_id)
drivers/gpu/drm/amd/amdkfd/kfd_debug.c +6 -1
index ba99e0f258ae..11af0c1cddcd 100644
@@ -517,7 +517,10 @@ int kfd_dbg_trap_set_flags(struct kfd_process *target, uint32_t *flags)
 
 	for (i = 0; i < target->n_pdds; i++) {
 		struct kfd_topology_device *topo_dev =
-				kfd_topology_device_by_id(target->pdds[i]->dev->id);
+			kfd_topology_device_by_id(target->pdds[i]->dev->id);
+		if (!topo_dev)
+			return -EINVAL;
+
 		uint32_t caps = topo_dev->node_props.capability;
 
 		if (!(caps & HSA_CAP_TRAP_DEBUG_PRECISE_MEMORY_OPERATIONS_SUPPORTED) &&
@@ -1071,6 +1074,8 @@ int kfd_dbg_trap_device_snapshot(struct kfd_process *target,
 	for (i = 0; i < tmp_num_devices; i++) {
 		struct kfd_process_device *pdd = target->pdds[i];
 		struct kfd_topology_device *topo_dev = kfd_topology_device_by_id(pdd->dev->id);
+		if (!topo_dev)
+			return -EINVAL;
 
 		device_info.gpu_id = pdd->dev->id;
 		device_info.exception_status = pdd->exception_status;
drivers/gpu/drm/amd/amdkfd/kfd_process.c +0 -3
index f5d173f1ca3b..888b1c24c2a6 100644
@@ -1685,9 +1685,6 @@ int kfd_process_device_init_vm(struct kfd_process_device *pdd,
 	struct kfd_node *dev;
 	int ret;
 
-	if (!drm_file)
-		return -EINVAL;
-
 	if (pdd->drm_priv)
 		return -EBUSY;
 
Signature · 1 line
2.43.0
#1 lore raw
Quoted 29 lines · Andrew Martin

This patch fixes issues when the code moves forward with a potential NULL pointer, without checking.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_fence.c | 2 +-
  drivers/gpu/drm/amd/amdkfd/kfd_crat.c            | 2 ++
  drivers/gpu/drm/amd/amdkfd/kfd_debug.c           | 7 ++++++-
  drivers/gpu/drm/amd/amdkfd/kfd_process.c         | 3 ---
  4 files changed, 9 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_fence.c +1 -1
index 1ef758ac5076..73c5749d4243 100644
@@ -105,7 +105,7 @@ static const char *amdkfd_fence_get_timeline_name(struct dma_fence *f)
  {
  	struct amdgpu_amdkfd_fence *fence = to_amdgpu_amdkfd_fence(f);
 
-	return fence->timeline_name;
+	return fence ? fence->timeline_name : NULL;
  }
 
  /**
drivers/gpu/drm/amd/amdkfd/kfd_crat.c +0 -0
index 4a7180b46b71..c086a9ed8c89 100644

drivers/gpu/drm/amd/amdkfd/kfd_crat.c static int kfd_create_vcrat_image_gpu(void *pcrat_image, · line 2363

Quoted 7 lines
drivers/gpu/drm/amd/amdkfd/kfd_crat.c +2 -0
@@ -2357,6 +2357,8 @@ static int kfd_create_vcrat_image_gpu(void *pcrat_image,
  	if (kdev->kfd->hive_id) {
  		for (nid = 0; nid < proximity_domain; ++nid) {
  			peer_dev = kfd_topology_device_by_proximity_domain_no_lock(nid);
+			if (!peer_dev)
+				continue;
  			if (!peer_dev->gpu)
  				continue;

if (!peer_dev || !peer_dev->gpu)
    continue;

Quoted 6 lines
drivers/gpu/drm/amd/amdkfd/kfd_crat.c +0 -0
  			if (peer_dev->gpu->kfd->hive_id != kdev->kfd->hive_id)
drivers/gpu/drm/amd/amdkfd/kfd_debug.c +0 -0
index ba99e0f258ae..11af0c1cddcd 100644

drivers/gpu/drm/amd/amdkfd/kfd_debug.c int kfd_dbg_trap_set_flags(struct kfd_process *target, uint32_t *flags) · line 523

Quoted 8 lines
drivers/gpu/drm/amd/amdkfd/kfd_debug.c +4 -1
@@ -517,7 +517,10 @@ int kfd_dbg_trap_set_flags(struct kfd_process *target, uint32_t *flags)
 
  	for (i = 0; i < target->n_pdds; i++) {
  		struct kfd_topology_device *topo_dev =
-				kfd_topology_device_by_id(target->pdds[i]->dev->id);
+			kfd_topology_device_by_id(target->pdds[i]->dev->id);
+		if (!topo_dev)
+			return -EINVAL;
+

move this after variable caps definition

drivers/gpu/drm/amd/amdkfd/kfd_debug.c int kfd_dbg_trap_set_flags(struct kfd_process *target, uint32_t *flags) · line 525

Quoted 2 lines
drivers/gpu/drm/amd/amdkfd/kfd_debug.c +0 -0
  		uint32_t caps = topo_dev->node_props.capability;
 

uin32_t caps;

if (!topo_dev)
    return -EINVAL;

caps = topo_dev->node_props.capability;

drivers/gpu/drm/amd/amdkfd/kfd_debug.c int kfd_dbg_trap_device_snapshot(struct kfd_process *target, · line 1076

Quoted 5 lines
drivers/gpu/drm/amd/amdkfd/kfd_debug.c +0 -0
  		if (!(caps & HSA_CAP_TRAP_DEBUG_PRECISE_MEMORY_OPERATIONS_SUPPORTED) &&
@@ -1071,6 +1074,8 @@ int kfd_dbg_trap_device_snapshot(struct kfd_process *target,
  	for (i = 0; i < tmp_num_devices; i++) {
  		struct kfd_process_device *pdd = target->pdds[i];
  		struct kfd_topology_device *topo_dev = kfd_topology_device_by_id(pdd->dev->id);

add empty line after variable.

drivers/gpu/drm/amd/amdkfd/kfd_debug.c int kfd_dbg_trap_set_flags(struct kfd_process *target, uint32_t *flags) · line 525

Quoted 3 lines
drivers/gpu/drm/amd/amdkfd/kfd_debug.c +2 -0
+		if (!topo_dev)
+			return -EINVAL;
 

break out of the loop to mutex_unlock then return

if (!topo_dev) {
    r = -EINVAL;
    break;
}

Regards,
Philip

Quoted 16 lines
drivers/gpu/drm/amd/amdkfd/kfd_debug.c +0 -0
  		device_info.gpu_id = pdd->dev->id;
  		device_info.exception_status = pdd->exception_status;
drivers/gpu/drm/amd/amdkfd/kfd_process.c +0 -3
index f5d173f1ca3b..888b1c24c2a6 100644
@@ -1685,9 +1685,6 @@ int kfd_process_device_init_vm(struct kfd_process_device *pdd,
  	struct kfd_node *dev;
  	int ret;
 
-	if (!drm_file)
-		return -EINVAL;
-
  	if (pdd->drm_priv)
  		return -EBUSY;
 
#2 lore raw

[AMD Official Use Only - AMD Internal Distribution Only]

Greetings Philip,

Thanks. A few non-essential comment inline.

Quoted 72 lines

-----Original Message-----
From: Yang, Philip <[email protected]>
Sent: Tuesday, December 2, 2025 2:23 PM
To: Martin, Andrew <[email protected]>; amd-
[email protected]
Subject: Re: [PATCH v3] drm/amdkfd: FORWARD NULL

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_fence.c static const char

Quoted 17 lines · Andrew Martin

This patch fixes issues when the code moves forward with a potential NULL pointer, without checking.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_fence.c | 2 +-
  drivers/gpu/drm/amd/amdkfd/kfd_crat.c            | 2 ++
  drivers/gpu/drm/amd/amdkfd/kfd_debug.c           | 7 ++++++-
  drivers/gpu/drm/amd/amdkfd/kfd_process.c         | 3 ---
  4 files changed, 9 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_fence.c +0 -0
index 1ef758ac5076..73c5749d4243 100644
@@ -105,7 +105,7 @@ static const char

*amdkfd_fence_get_timeline_name(struct dma_fence *f)

drivers/gpu/drm/amd/amdkfd/kfd_crat.c static int kfd_create_vcrat_image_gpu(void

Quoted 14 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_fence.c +1 -1
  {
    struct amdgpu_amdkfd_fence *fence = to_amdgpu_amdkfd_fence(f);
 
-   return fence->timeline_name;
+   return fence ? fence->timeline_name : NULL;
  }
 
  /**
drivers/gpu/drm/amd/amdkfd/kfd_crat.c +0 -0
index 4a7180b46b71..c086a9ed8c89 100644
@@ -2357,6 +2357,8 @@ static int kfd_create_vcrat_image_gpu(void

*pcrat_image,

drivers/gpu/drm/amd/amdkfd/kfd_crat.c static int kfd_create_vcrat_image_gpu(void *pcrat_image,

Quoted 3 lines
drivers/gpu/drm/amd/amdkfd/kfd_crat.c +0 -0
    if (kdev->kfd->hive_id) {
            for (nid = 0; nid < proximity_domain; ++nid) {
                    peer_dev =

kfd_topology_device_by_proximity_domain_no_lock(nid);

drivers/gpu/drm/amd/amdkfd/kfd_crat.c static int kfd_create_vcrat_image_gpu(void *pcrat_image, · line 2363

Quoted 4 lines
drivers/gpu/drm/amd/amdkfd/kfd_crat.c +2 -0
+                   if (!peer_dev)
+                           continue;
                    if (!peer_dev->gpu)
                            continue;

if (!peer_dev || !peer_dev->gpu)

     continue;

drivers/gpu/drm/amd/amdkfd/kfd_crat.c static int kfd_create_vcrat_image_gpu(void *pcrat_image, · line 2364

Quoted 1 line
drivers/gpu/drm/amd/amdkfd/kfd_crat.c +0 -0
                    if (peer_dev->gpu->kfd->hive_id != kdev->kfd->hive_id)

diff --git

Quoted 6 lines

a/drivers/gpu/drm/amd/amdkfd/kfd_debug.c
b/drivers/gpu/drm/amd/amdkfd/kfd_debug.c
index ba99e0f258ae..11af0c1cddcd 100644

drivers/gpu/drm/amd/amdkfd/kfd_debug.c int kfd_dbg_trap_set_flags(struct kfd_process · line 525

Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_debug.c +4 -1
@@ -517,7 +517,10 @@ int kfd_dbg_trap_set_flags(struct kfd_process
 *target, uint32_t *flags)
 
    for (i = 0; i < target->n_pdds; i++) {
            struct kfd_topology_device *topo_dev =
-                           kfd_topology_device_by_id(target->pdds[i]-
 dev->id);
+                   kfd_topology_device_by_id(target->pdds[i]->dev->id);
+           if (!topo_dev)
+                   return -EINVAL;
+

move this after variable caps definition

Quoted 8 lines

drivers/gpu/drm/amd/amdkfd/kfd_debug.c int kfd_dbg_trap_set_flags(struct kfd_process *target, uint32_t *flags) · line 524

Quoted 2 lines
drivers/gpu/drm/amd/amdkfd/kfd_debug.c +0 -0
            uint32_t caps = topo_dev->node_props.capability;

uin32_t caps;

if (!topo_dev)

     return -EINVAL;

caps = topo_dev->node_props.capability;

Thanks again, I thought this flow is slightly cleaner, already in PATCH 4.
...

                uint32_t caps;
                struct kfd_topology_device *topo_dev =
                        kfd_topology_device_by_id(target->pdds[i]->dev->id);
                if (!topo_dev)
                        return -EINVAL;
                caps = topo_dev->node_props.capability;

...

Quoted 8 lines
Quoted 2 lines
            if (!(caps &

HSA_CAP_TRAP_DEBUG_PRECISE_MEMORY_OPERATIONS_SUPPORTED) &&

(quoted diff) +0 -0
@@ -1071,6 +1074,8 @@ int kfd_dbg_trap_device_snapshot(struct
 kfd_process *target,
 >     for (i = 0; i < tmp_num_devices; i++) {
 >             struct kfd_process_device *pdd = target->pdds[i];
Quoted 2 lines

struct kfd_topology_device *topo_dev =
kfd_topology_device_by_id(pdd->dev->id);

Quoted 5 lines

add empty line after variable.

drivers/gpu/drm/amd/amdkfd/kfd_debug.c

Quoted 3 lines
drivers/gpu/drm/amd/amdkfd/kfd_debug.c +2 -0
+           if (!topo_dev)
+                   return -EINVAL;

break out of the loop to mutex_unlock then return

Grateful for this lesson, had you not mentioned it, I would have not notice the unlock.

Quoted 26 lines
if (!topo_dev) {
     r = -EINVAL;
     break;
}

Regards,
Philip

drivers/gpu/drm/amd/amdkfd/kfd_process.c int kfd_process_device_init_vm(struct

Quoted 8 lines
            device_info.gpu_id = pdd->dev->id;
            device_info.exception_status = pdd->exception_status; diff --git

a/drivers/gpu/drm/amd/amdkfd/kfd_process.c
b/drivers/gpu/drm/amd/amdkfd/kfd_process.c
index f5d173f1ca3b..888b1c24c2a6 100644

drivers/gpu/drm/amd/amdkfd/kfd_process.c +0 -0
@@ -1685,9 +1685,6 @@ int kfd_process_device_init_vm(struct

kfd_process_device *pdd,

Quoted 9 lines
drivers/gpu/drm/amd/amdkfd/kfd_process.c +0 -3
    struct kfd_node *dev;
    int ret;
 
-   if (!drm_file)
-           return -EINVAL;
-
    if (pdd->drm_priv)
            return -EBUSY;