drm/msm: A couple lazy-vm fixes
Rob Clark
maintainerv2 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 (3):
drm/msm: Fix barriers accessing ctx vm drm/msm: Validate lazy VM is created in GEM_SUBMIT drm/msm: Validate lazy VM in GEM_NEW
drivers/gpu/drm/msm/msm_drv.c | 13 +++++++------ drivers/gpu/drm/msm/msm_gem.c | 7 ++++++- drivers/gpu/drm/msm/msm_gem_submit.c | 9 +++++---- 3 files changed, 18 insertions(+), 11 deletions(-)
Signature · 1 line
2.54.0
Rob Clark
maintainerPATCH v2 1/3 drm/msm: Fix barriers accessing ctx vm
Don't rely on store ordering to protect us from caller seeing a partially initialized vm.
--- drivers/gpu/drm/msm/msm_drv.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-)
drivers/gpu/drm/msm/msm_drv.c +7 -6
@@ -224,18 +224,19 @@ struct drm_gpuvm *msm_context_vm(struct drm_device *dev, struct msm_context *ctx {static DEFINE_MUTEX (init_lock);struct msm_drm_private *priv = dev->dev_private; +struct drm_gpuvm *vm =smp_load_acquire (&ctx->vm);/* Once ctx->vm is created it is valid for the lifetime of the context: */ -if (ctx->vm) -return ctx->vm; +if (vm) +return vm; + +guard (mutex)(&init_lock); -mutex_lock (&init_lock);if (!ctx->vm) { - ctx->vm =msm_gpu_create_private_vm ( + vm =msm_gpu_create_private_vm ( priv->gpu, current, !ctx->userspace_managed_vm); - +smp_store_release (&ctx->vm, vm); } -mutex_unlock (&init_lock);return ctx->vm; }
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH v2 1/3] drm/msm: Fix barriers accessing ctx vm
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] Direct access to uninitialized ctx->vm in adreno_set_param bypasses validation, allowing VM_BIND on global VMs. --
commit be0b70b2e7a9f55182d48578fb53358469060fcb
Author: Rob Clark <[email protected]>
drm/msm: Fix barriers accessing ctx vm
This commit adds load-acquire and store-release barriers when accessing and lazily initializing ctx->vm in msm_context_vm(). This prevents callers from seeing a partially initialized virtual memory state.
Quoted 5 lines
drivers/gpu/drm/msm/msm_drv.c +0
-0
drivers/gpu/drm/msm/msm_drv.c struct drm_gpuvm *msm_context_vm(struct drm_device *dev, struct msm_context *ctx · line 231
Quoted 10 lines
drivers/gpu/drm/msm/msm_drv.c +3
-2@@ -224,18 +224,19 @@ struct drm_gpuvm *msm_context_vm(struct drm_device *dev, struct msm_context *ctx {static DEFINE_MUTEX (init_lock);struct msm_drm_private *priv = dev->dev_private; +struct drm_gpuvm *vm =smp_load_acquire (&ctx->vm);/* Once ctx->vm is created it is valid for the lifetime of the context: */ -if (ctx->vm) -return ctx->vm; +if (vm) +return vm;
[Severity: High]
This isn't a bug introduced by this patch, but seeing the fixes to ctx->vm lazy initialization here points to a related problem in adreno_set_param().
Looking at adreno_set_param() in drivers/gpu/drm/msm/adreno/adreno_gpu.c, there is this check when enabling VM_BIND:
if (ctx->vm == gpu->vm)
return UERR(EINVAL, drm, "requires per-process pgtables");Since ctx->vm starts as NULL and is lazily initialized, couldn't this check fail to catch global VMs (since NULL != gpu->vm) if msm_context_vm() hasn't been called yet?
If unprivileged userspace opts into VM_BIND mode before the VM is initialized, could this bypass the validation check on platforms without per-process pagetables, potentially allowing arbitrary mapping in the global GPU address space?
Should this be using msm_context_vm(dev, ctx) instead to ensure the VM is instantiated before checking?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Rob Clark
maintainerPATCH v2 2/3 drm/msm: Validate lazy VM is created in GEM_SUBMIT
In reply to Rob ClarkOtherwise a GEM_SUBMIT ioctl before any BOs are mapped could cause a NPE.
--- drivers/gpu/drm/msm/msm_gem_submit.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/gpu/drm/msm/msm_gem_submit.c +5 -4
@@ -30,7 +30,7 @@*/ static struct msm_gem_submit *submit_create (struct drm_device *dev, -struct msm_gpu *gpu, +struct msm_gpu *gpu,struct drm_gpuvm *vm,struct msm_gpu_submitqueue *queue,uint32_t nr_bos,uint32_t nr_cmds,u64 drm_client_id) {
@@ -66,7 +66,7 @@ static struct msm_gem_submit *submit_create(struct drm_device *dev,kref_init (&submit->ref); submit->dev = dev; - submit->vm =msm_context_vm (dev, queue->ctx); + submit->vm = vm; submit->gpu = gpu; submit->cmd = (void *)&submit->bos[nr_bos]; submit->queue = queue;
@@ -554,6 +554,7 @@ int msm_ioctl_gem_submit(struct drm_device *dev, void *data,struct msm_drm_private *priv = dev->dev_private;struct drm_msm_gem_submit *args = data;struct msm_context *ctx = file->driver_priv; +struct drm_gpuvm *vm =msm_context_vm (dev, ctx);struct msm_gem_submit *submit =NULL ;struct msm_gpu *gpu = priv->gpu;struct msm_gpu_submitqueue *queue;
@@ -572,7 +573,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 (vm &&to_msm_vm (vm)->unusable)return UERR (EPIPE , dev,"context is unusable" );/* for now, we just have 3d pipe.. eventually this would need to
@@ -609,7 +610,7 @@ int msm_ioctl_gem_submit(struct drm_device *dev, void *data, } } - submit =submit_create (dev, gpu, queue, args->nr_bos, args->nr_cmds, + submit =submit_create (dev, gpu, vm, queue, args->nr_bos, args->nr_cmds, file->client_id);if (IS_ERR (submit)) { ret =PTR_ERR (submit);
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH v2 2/3] 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:
- [Critical] The MSM_PARAM_EN_VM_BIND ioctl validation compares an uninitialized context VM against the global VM, failing to prevent VM_BIND enablement on GPUs without per-process page tables. --
commit 0fcb5ea6b90013aec15c2f186ee0bbf02e4e4818
Author: Rob Clark <[email protected]>
drm/msm: Validate lazy VM is created in GEM_SUBMIT
Otherwise a GEM_SUBMIT ioctl before any BOs are mapped could cause a NPE.
Quoted 4 lines
drivers/gpu/drm/msm/msm_gem_submit.c +0
-0
[ ... ]
drivers/gpu/drm/msm/msm_gem_submit.c int msm_ioctl_gem_submit(struct drm_device *dev, void *data, · line 577
Quoted 7 lines
drivers/gpu/drm/msm/msm_gem_submit.c +1
-1@@ -572,7 +573,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 (vm &&to_msm_vm (vm)->unusable)return UERR (EPIPE , dev,"context is unusable" );
[Severity: Critical]
This is a pre-existing issue, but since we are fixing lazy VM access here, is there a similar issue in adreno_set_param() when handling MSM_PARAM_EN_VM_BIND?
Looking at drivers/gpu/drm/msm/adreno/adreno_gpu.c:
adreno_set_param()
{
...
case MSM_PARAM_EN_VM_BIND:
/* We can only support VM_BIND with per-process pgtables: */
if (ctx->vm == gpu->vm)
return UERR(EINVAL, drm, "requires per-process pgtables");Because ctx->vm can be NULL prior to lazy initialization, this check evaluates to false, allowing the restriction to be bypassed.
Could this allow an unprivileged user to enable userspace management (VM_BIND) on older GPUs that fall back to the globally shared GPU VM, and overwrite page tables shared by other processes?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Rob Clark
maintainerPATCH v2 3/3 drm/msm: Validate lazy VM in GEM_NEW
In reply to Rob ClarkOtherwise creating a _NO_SHARE BO before any BOs are mapped could cause a NPE.
--- drivers/gpu/drm/msm/msm_gem.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/gpu/drm/msm/msm_gem.c +6 -1
@@ -1147,7 +1147,12 @@ 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); + +if (!vm) +return UERR (EINVAL , dev,"not supported with shared VM" ); + +struct drm_gem_object *r_obj =drm_gpuvm_resv_obj (vm);drm_gem_object_get (r_obj);
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH v2 3/3] drm/msm: Validate lazy VM in GEM_NEW
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] The newly allocated GEM object 'obj' is leaked on the error path if the VM is not found or…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] The newly allocated GEM object 'obj' is leaked on the error path if the VM is not found or unsupported. --
commit ebb450a0150beb70fa2ad61c737f157a48b8cb86
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.
drivers/gpu/drm/msm/msm_gem.c int msm_gem_new_handle(struct drm_device *dev, struct drm_file *file, · line 1153
Quoted 13 lines
drivers/gpu/drm/msm/msm_gem.c +4
-1@@ -1147,7 +1147,12 @@ 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); + +if (!vm) +return UERR (EINVAL , dev,"not supported with shared VM" );
^^^^
[Severity: High]
Does this code leak the newly allocated obj?
Looking earlier in msm_gem_new_handle(), obj is allocated:
obj = msm_gem_new(dev, size, flags); if (IS_ERR(obj)) return PTR_ERR(obj);
If vm is NULL, we return directly here without calling drm_gem_object_put(obj) to release the reference.
Quoted 5 lines
drivers/gpu/drm/msm/msm_gem.c +2
-0+ +struct drm_gem_object *r_obj =drm_gpuvm_resv_obj (vm);drm_gem_object_get (r_obj);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3