Skip to contentSkip to conversation

drm/msm: Only fini scheduler after successful init

PATCH New Ruoyu Wang 2 participants 2 messages Jul 9

Subsystems (4)
lore
Share & tools
lore

Ruoyu Wang

author
#0 lore raw

PATCH 1/1 drm/msm: Only fini scheduler after successful init

msm_ringbuffer_new() destroys a partially initialized ring through msm_ringbuffer_destroy() when an allocation or scheduler setup step fails.

If drm_sched_init() fails before it finishes initializing the scheduler, the failure path still calls drm_sched_fini(). That teardown path assumes the scheduler work items, lists, and workqueue state were initialized.

Track successful scheduler initialization and call drm_sched_fini() only after drm_sched_init() returned 0.

This issue was found by a static analysis checker and confirmed by manual source review.

Fixes 1d8a5ca436ee ("drm/msm: Conversion to drm scheduler")
---
 drivers/gpu/drm/msm/msm_ringbuffer.c | 7 ++++---
 drivers/gpu/drm/msm/msm_ringbuffer.h | 1 +
 2 files changed, 5 insertions(+), 3 deletions(-)
drivers/gpu/drm/msm/msm_ringbuffer.c +4 -3
index 0d14c31bd4e47..af2b0edc4d586 100644
@@ -108,9 +108,9 @@ struct msm_ringbuffer *msm_ringbuffer_new(struct msm_gpu *gpu, int id,
 	ring->memptrs_iova = memptrs_iova;
 
 	ret = drm_sched_init(&ring->sched, &args);
-	if (ret) {
+	if (ret)
 		goto fail;
-	}
+	ring->sched_initialized = true;
 
 	INIT_LIST_HEAD(&ring->submits);
 	spin_lock_init(&ring->submit_lock);
@@ -132,7 +132,8 @@ void msm_ringbuffer_destroy(struct msm_ringbuffer *ring)
 	if (IS_ERR_OR_NULL(ring))
 		return;
 
-	drm_sched_fini(&ring->sched);
+	if (ring->sched_initialized)
+		drm_sched_fini(&ring->sched);
 
 	msm_fence_context_free(ring->fctx);
 
drivers/gpu/drm/msm/msm_ringbuffer.h +1 -0
index 28ca8c9f7463d..3631ec283c6e5 100644
@@ -56,6 +56,7 @@ struct msm_ringbuffer {
 	 * The job scheduler for this ring.
 	 */
 	struct drm_gpu_scheduler sched;
+	bool sched_initialized;
 
 	/*
 	 * List of in-flight submits on this ring.  Protected by submit_lock.
Signature · 1 line
2.51.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Pre-existing issues:
- [High] Missing error check for msm_fence_context_alloc() return value leads to invalid pointer dereference.
- [High] Direct access to ctx->vm without lazy initialization causes NULL pointer dereference. --

--- Patch [1]: [PATCH] drm/msm: Only fini scheduler after successful init --- commit b85d84ece3c22f19d85b416480e8fa8cc2d6160b
Author: Ruoyu Wang <[email protected]>

drm/msm: Only fini scheduler after successful init

msm_ringbuffer_new() destroys a partially initialized ring through msm_ringbuffer_destroy() when an allocation or scheduler setup step fails. If drm_sched_init() fails before it finishes initializing the scheduler, the failure path still calls drm_sched_fini() which assumes the state was fully initialized. This patch tracks successful scheduler initialization and calls drm_sched_fini() only after drm_sched_init() returned 0.

[ ... ]

Quoted 4 lines
drivers/gpu/drm/msm/msm_ringbuffer.c +0 -0
index 0d14c31bd4e47..af2b0edc4d586 100644

[ ... ]

drivers/gpu/drm/msm/msm_ringbuffer.c void msm_ringbuffer_destroy(struct msm_ringbuffer *ring) · line 138

Quoted 9 lines
drivers/gpu/drm/msm/msm_ringbuffer.c +2 -1
@@ -132,7 +132,8 @@ void msm_ringbuffer_destroy(struct msm_ringbuffer *ring)
 	if (IS_ERR_OR_NULL(ring))
 		return;
 
-	drm_sched_fini(&ring->sched);
+	if (ring->sched_initialized)
+		drm_sched_fini(&ring->sched);
 
 	msm_fence_context_free(ring->fctx);

[Severity: High]
This isn't a regression introduced by this patch, but there is a missing error check for the msm_fence_context_alloc() return value.

drivers/gpu/drm/msm/msm_ringbuffer.c:msm_ringbuffer_new() {
    ...
	snprintf(name, sizeof(name), "gpu-ring-%d", ring->id);

	ring->fctx = msm_fence_context_alloc(gpu->dev, &ring->memptrs->fence, name);

	return ring;
}

msm_fence_context_alloc() can return ERR_PTR(-ENOMEM) on memory allocation failure. The error pointer is assigned to ring->fctx without an IS_ERR() validation, and the new ring object is returned as if successful.

When the ring is later destroyed here, msm_ringbuffer_destroy() passes the error pointer to msm_fence_context_free(), which calls kfree() on it.

Does this need an IS_ERR() check to prevent an invalid memory access?

[ ... ]

[Severity: High]
This is also a pre-existing regression, but could this lead to a NULL pointer dereference?

drivers/gpu/drm/msm/msm_gem.c:msm_gem_new_handle() {
    ...
	if (flags & MSM_BO_NO_SHARE) {
		struct msm_context *ctx = file->driver_priv;
		struct drm_gem_object *r_obj = drm_gpuvm_resv_obj(ctx->vm);
    ...
}

The MSM driver lazily initializes virtual memory address spaces. If userspace calls an ioctl that triggers msm_gem_new_handle() with the MSM_BO_NO_SHARE flag before the VM is initialized, ctx->vm will be NULL.

Passing this NULL pointer to drm_gpuvm_resv_obj(ctx->vm) results in dereferencing NULL to access the r_obj field.

Should this use the msm_context_vm() accessor instead to ensure the VM is initialized before access?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1