Skip to contentSkip to conversation

accel/rocket: fix NULL derefs and missing error check

PATCH In review Ali Khaledi 2 participants 2 messages Mar 2 – Nov 17

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

PATCH 3/3 accel/rocket: fix NULL derefs and missing error check

Fix several issues in the rocket accelerator driver:

rocket_gem.c: Add missing error check after drm_mm_insert_node_generic(). The return value was silently overwritten by iommu_map_sgtable(), causing DMA mapping to use an uninitialized IOVA on MM insertion failure.

rocket_job.c: Add missing NULL check after kvmalloc_array() in rocket_job_push(). A large job can trigger allocation failure, leading to NULL pointer dereference in the following memcpy.

rocket_job.c: Add missing NULL check after kmalloc_objs() in rocket_job_open(). Also fix memory leak of the scheds array when drm_sched_entity_init() fails.

Found by AI-assisted code review (Claude Opus 4.6, Anthropic) in collaboration with Ali Khaledi.

---
 drivers/accel/rocket/rocket_gem.c | 2 ++
 drivers/accel/rocket/rocket_job.c | 9 ++++++++-
 2 files changed, 10 insertions(+), 1 deletion(-)
drivers/accel/rocket/rocket_gem.c +2 -0
index b6a385d2e..7212edc3f 100644
@@ -95,6 +95,8 @@ int rocket_ioctl_create_bo(struct drm_device *dev, void *data, struct drm_file *
 					 rkt_obj->size, PAGE_SIZE,
 					 0, 0);
 	mutex_unlock(&rocket_priv->mm_lock);
+	if (ret)
+		goto err;
 
 	ret = iommu_map_sgtable(rocket_priv->domain->domain,
 				rkt_obj->mm.start,
drivers/accel/rocket/rocket_job.c +8 -1
index ac51bff39..6502b7148 100644
@@ -192,6 +192,8 @@ static int rocket_job_push(struct rocket_job *job)
 
 	bos = kvmalloc_array(job->in_bo_count + job->out_bo_count, sizeof(void *),
 			     GFP_KERNEL);
+	if (!bos)
+		return -ENOMEM;
 	memcpy(bos, job->in_bos, job->in_bo_count * sizeof(void *));
 	memcpy(&bos[job->in_bo_count], job->out_bos, job->out_bo_count * sizeof(void *));
 
@@ -501,6 +503,9 @@ int rocket_job_open(struct rocket_file_priv *rocket_priv)
 	unsigned int core;
 	int ret;
 
+	if (!scheds)
+		return -ENOMEM;
+
 	for (core = 0; core < rdev->num_cores; core++)
 		scheds[core] = &rdev->cores[core].sched;
 
@@ -508,8 +513,10 @@ int rocket_job_open(struct rocket_file_priv *rocket_priv)
 				    DRM_SCHED_PRIORITY_NORMAL,
 				    scheds,
 				    rdev->num_cores, NULL);
-	if (WARN_ON(ret))
+	if (WARN_ON(ret)) {
+		kfree(scheds);
 		return ret;
+	}
 
 	return 0;
 }
Signature · 1 line
2.53.0
#1 lore raw

Hi,

The subject says 3/3 but I only see one patch submitted.

Quoted 7 lines · Ali Khaledi

Fix several issues in the rocket accelerator driver:

rocket_gem.c: Add missing error check after drm_mm_insert_node_generic(). The return value was silently overwritten by iommu_map_sgtable(), causing DMA mapping to use an uninitialized IOVA on MM insertion failure.

Quoted 7 lines

rocket_job.c: Add missing NULL check after kvmalloc_array() in rocket_job_push(). A large job can trigger allocation failure, leading to NULL pointer dereference in the following memcpy.

rocket_job.c: Add missing NULL check after kmalloc_objs() in rocket_job_open(). Also fix memory leak of the scheds array when drm_sched_entity_init() fails.

You could reduce the verbosity of this commit message - no need to show filenames, and you can limit it just to pointing out what is not obvious from the code change itself.

Quoted 2 lines

Found by AI-assisted code review (Claude Opus 4.6, Anthropic) in collaboration with Ali Khaledi.

No need for this statement; if you understand the code and your proposed changes, then submit it without crediting an agent. If you don't understand it, then refrain from submitting until you personally can stand behind your contribution.

Quoted 24 lines
---
  drivers/accel/rocket/rocket_gem.c | 2 ++
  drivers/accel/rocket/rocket_job.c | 9 ++++++++-
  2 files changed, 10 insertions(+), 1 deletion(-)
drivers/accel/rocket/rocket_gem.c +2 -0
index b6a385d2e..7212edc3f 100644
@@ -95,6 +95,8 @@ int rocket_ioctl_create_bo(struct drm_device *dev, void *data, struct drm_file *
  					 rkt_obj->size, PAGE_SIZE,
  					 0, 0);
  	mutex_unlock(&rocket_priv->mm_lock);
+	if (ret)
+		goto err;
 
  	ret = iommu_map_sgtable(rocket_priv->domain->domain,
  				rkt_obj->mm.start,
drivers/accel/rocket/rocket_job.c +0 -0
index ac51bff39..6502b7148 100644

drivers/accel/rocket/rocket_job.c static int rocket_job_push(struct rocket_job *job) · line 198

Quoted 7 lines
drivers/accel/rocket/rocket_job.c +2 -0
@@ -192,6 +192,8 @@ static int rocket_job_push(struct rocket_job *job)
 
  	bos = kvmalloc_array(job->in_bo_count + job->out_bo_count, sizeof(void *),
  			     GFP_KERNEL);
+	if (!bos)
+		return -ENOMEM;
  	memcpy(bos, job->in_bos, job->in_bo_count * sizeof(void *));
  	memcpy(&bos[job->in_bo_count], job->out_bos, job->out_bo_count * sizeof(void *));

These two have already been fixed in other commits.

Quoted 12 lines
drivers/accel/rocket/rocket_job.c +3 -0
@@ -501,6 +503,9 @@ int rocket_job_open(struct rocket_file_priv *rocket_priv)
  	unsigned int core;
  	int ret;
 
+	if (!scheds)
+		return -ENOMEM;
+
  	for (core = 0; core < rdev->num_cores; core++)
  		scheds[core] = &rdev->cores[core].sched;
 
@@ -508,8 +513,10 @@ int rocket_job_open(struct rocket_file_priv *rocket_priv)
  				    DRM_SCHED_PRIORITY_NORMAL,

drivers/accel/rocket/rocket_job.c int rocket_job_open(struct rocket_file_priv *rocket_priv) · line 522

Quoted 10 lines
drivers/accel/rocket/rocket_job.c +3 -1
  				    scheds,
  				    rdev->num_cores, NULL);
-	if (WARN_ON(ret))
+	if (WARN_ON(ret)) {
+		kfree(scheds);
  		return ret;
+	}
 
  	return 0;
  }

This issue still stands but I would encourage you to do a deeper fix if continuing your work here. Even in the non-error path if num_cores is 1 for any reason, it looks like drm will not set sched_list, so the memory will be leaked there too. I think we need a better strategy for handling the scheds.

Daniel