Skip to contentSkip to conversation

drm/msm: A couple lazy-vm fixes

PATCH Superseded Rob Clark 2 participants 5 messages Jun 7

Subsystems (3)

v1 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Rob Clark

author maintainer
#0 lore raw

cover drm/msm: A couple lazy-vm fixes

The VM is created lazily to give userspace a chance to opt-in to VM_BIND. But Sashiko noticed a couple paths that were not handling this properly.

Rob Clark (2):
drm/msm: Validate lazy VM is created in GEM_SUBMIT drm/msm: Validate lazy VM in GEM_NEW

 drivers/gpu/drm/msm/msm_gem.c        | 3 ++-
 drivers/gpu/drm/msm/msm_gem_submit.c | 2 +-
 2 files changed, 3 insertions(+), 2 deletions(-)
Signature · 1 line
2.54.0
author maintainer
#1 lore raw

PATCH 1/2 drm/msm: Validate lazy VM is created in GEM_SUBMIT

Otherwise a GEM_SUBMIT ioctl before any BOs are mapped could cause a NPE.

Fixes 6a4d287a1ae6 ("drm/msm: Mark VM as unusable on GPU hangs")
---
 drivers/gpu/drm/msm/msm_gem_submit.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/msm/msm_gem_submit.c +1 -1
index 26ea8a28be47..af564acf1f0d 100644
@@ -572,7 +572,7 @@ int msm_ioctl_gem_submit(struct drm_device *dev, void *data,
 	if (args->pad)
 		return -EINVAL;
 
-	if (to_msm_vm(ctx->vm)->unusable)
+	if (ctx->vm && to_msm_vm(ctx->vm)->unusable)
 		return UERR(EPIPE, dev, "context is unusable");
 
 	/* for now, we just have 3d pipe.. eventually this would need to
Signature · 1 line
2.54.0
#2 lore raw

Re: [PATCH 1/2] drm/msm: Validate lazy VM is created in GEM_SUBMIT

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

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

Pre-existing issues:
- [High] MSM VM Lazy Initialization Violation: Direct access to lazily initialized ctx->vm without accessor. --

commit 1e90d5d8db7bb987e468148877a0109dc4324252
Author: Rob Clark <[email protected]>

drm/msm: Validate lazy VM is created in GEM_SUBMIT

This commit adds a NULL check for the context VM in the GEM_SUBMIT ioctl to prevent a null pointer exception when submitting before any buffer objects are mapped.

drivers/gpu/drm/msm/msm_gem_submit.c int msm_ioctl_gem_submit(struct drm_device *dev, void *data, · line 575

Quoted 10 lines
drivers/gpu/drm/msm/msm_gem_submit.c +1 -1
index 26ea8a28be474..af564acf1f0df 100644
@@ -572,7 +572,7 @@ int msm_ioctl_gem_submit(struct drm_device *dev, void *data,
 	if (args->pad)
 		return -EINVAL;
 
-	if (to_msm_vm(ctx->vm)->unusable)
+	if (ctx->vm && to_msm_vm(ctx->vm)->unusable)

[Severity: High]
This is a pre-existing issue, but should this code use the msm_context_vm() accessor instead of directly accessing ctx->vm here in msm_ioctl_gem_submit?

According to the DRM subsystem guidelines, accessing ctx->vm directly in ioctl entry points before the VM is guaranteed to exist can be unsafe. Because ctx->vm is initialized lazily and published without memory barriers, a concurrent VM initialization could cause this unlocked read to observe a non-NULL pointer but read uninitialized memory for the unusable field. Might this lead to spurious EPIPE failures for valid submits?

Quoted 3 lines
drivers/gpu/drm/msm/msm_gem_submit.c +0 -0
 		return UERR(EPIPE, dev, "context is unusable");
 
 	/* for now, we just have 3d pipe.. eventually this would need to
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author maintainer
#3 lore raw

PATCH 2/2 drm/msm: Validate lazy VM in GEM_NEW

In reply to Rob Clark

Otherwise creating a _NO_SHARE BO before any BOs are mapped could cause a NPE.

Fixes b58e12a66e47 ("drm/msm: Add _NO_SHARE flag")
---
 drivers/gpu/drm/msm/msm_gem.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/msm/msm_gem.c +2 -1
index 2cb3ab04f125..ef14ee66c6c3 100644
@@ -1148,7 +1148,8 @@ int msm_gem_new_handle(struct drm_device *dev, struct drm_file *file,
 
 	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);
+		struct drm_gpuvm *vm = msm_context_vm(dev, ctx);
+		struct drm_gem_object *r_obj = drm_gpuvm_resv_obj(vm);
 
 		drm_gem_object_get(r_obj);
 
Signature · 1 line
2.54.0
#4 lore raw

Re: [PATCH 2/2] drm/msm: Validate lazy VM in GEM_NEW

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:
- [Critical] Calling DRM_IOCTL_MSM_GEM_NEW with the MSM_BO_NO_SHARE flag on a device without a GPU causes a NULL pointer dereference.
- [High] Lockless fast-path reading of ctx->vm lacks acquire/release memory barriers, allowing concurrent threads to observe and access a partially initialized drm_gpuvm structure. --

commit e440c951a727aa924c22337cdf0290e20b62ff2e
Author: Rob Clark <[email protected]>

drm/msm: Validate lazy VM in GEM_NEW

Otherwise creating a _NO_SHARE BO before any BOs are mapped could cause a NPE.

Fixes b58e12a66e47 ("drm/msm: Add _NO_SHARE flag")

drivers/gpu/drm/msm/msm_gem.c int msm_gem_new_handle(struct drm_device *dev, struct drm_file *file, · line 1151

Quoted 10 lines
drivers/gpu/drm/msm/msm_gem.c +1 -1
index 2cb3ab04f1250..ef14ee66c6c30 100644
@@ -1148,7 +1148,8 @@ int msm_gem_new_handle(struct drm_device *dev, struct drm_file *file,
 
 	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);
+		struct drm_gpuvm *vm = msm_context_vm(dev, ctx);

[Severity: High]
This isn't a bug introduced by this patch, but does the implementation of msm_context_vm() lack necessary memory barriers?

Looking at msm_context_vm() in drivers/gpu/drm/msm/msm_drv.c:

	if (ctx->vm)
		return ctx->vm;

	mutex_lock(&init_lock);
	if (!ctx->vm) {
		ctx->vm = msm_gpu_create_private_vm(...);
	}
	mutex_unlock(&init_lock);

Since the fast-path reads ctx->vm without smp_load_acquire() and the write happens without smp_store_release(), can a concurrent ioctl thread observe a non-NULL ctx->vm pointer before the underlying drm_gpuvm structure is fully initialized (such as setting vm->r_obj) on weakly-ordered architectures?

drivers/gpu/drm/msm/msm_gem.c int msm_gem_new_handle(struct drm_device *dev, struct drm_file *file, · line 1152

Quoted 1 line
drivers/gpu/drm/msm/msm_gem.c +1 -0
+		struct drm_gem_object *r_obj = drm_gpuvm_resv_obj(vm);

[Severity: Critical]
This is a pre-existing issue, but does msm_context_vm() return NULL on display-only configurations where the GPU fails to probe (priv->gpu == NULL)?

If msm_context_vm() returns NULL, wouldn't passing it to drm_gpuvm_resv_obj(vm) cause an immediate NULL pointer dereference since the macro unconditionally expands to ((vm)->r_obj) here?

Quoted 2 lines
drivers/gpu/drm/msm/msm_gem.c +0 -0
 		drm_gem_object_get(r_obj);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2