drm/gpuvm: Locking helpers
Subsystems (4)
cover drm/gpuvm: Locking helpers
First patch is just some cleanup. The second patch adds helpers for drivers to deal with "invisible" unmapped BO locking. Ie. a VM_BIND ioctl won't explicitly list BOs associated with unmapped/remapped VAs making locking all the BOs involved in a VM_BIND ioclt harder than it needs to be. The helpers added solves that.
Rob Clark (2):
drm/gpuvm: Fix doc comments
drm/gpuvm: Add locking helpers
drivers/gpu/drm/drm_gpuvm.c | 87 +++++++++++++++++++++++++++++++++++-- include/drm/drm_gpuvm.h | 14 ++++-- 2 files changed, 95 insertions(+), 6 deletions(-)
Signature · 1 line
2.49.0
PATCH 1/2 drm/gpuvm: Fix doc comments
Correctly summerize drm_gpuvm_sm_map/unmap, and fix the parameter order and names. Just something I noticed in passing.
--- drivers/gpu/drm/drm_gpuvm.c | 6 +++--- include/drm/drm_gpuvm.h | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-)
drivers/gpu/drm/drm_gpuvm.c +3 -3
@@ -2299,13 +2299,13 @@ __drm_gpuvm_sm_unmap(struct drm_gpuvm *gpuvm, }/** -* drm_gpuvm_sm_map() - creates the &drm_gpuva_op split/merge steps +* drm_gpuvm_sm_map() - calls the &drm_gpuva_op split/merge steps * @gpuvm: the &drm_gpuvm representing the GPU VA space +* @priv: pointer to a driver private data structure * @req_addr: the start address of the new mapping * @req_range: the range of the new mapping * @req_obj: the &drm_gem_object to map * @req_offset: the offset within the &drm_gem_object -* @priv: pointer to a driver private data structure * * This function iterates the given range of the GPU VA space. It utilizes the * &drm_gpuvm_ops to call back into the driver providing the split and merge
@@ -2349,7 +2349,7 @@ drm_gpuvm_sm_map(struct drm_gpuvm *gpuvm, void *priv,EXPORT_SYMBOL_GPL (drm_gpuvm_sm_map);/** -* drm_gpuvm_sm_unmap() - creates the &drm_gpuva_ops to split on unmap +* drm_gpuvm_sm_unmap() - calls the &drm_gpuva_ops to split on unmap * @gpuvm: the &drm_gpuvm representing the GPU VA space * @priv: pointer to a driver private data structure * @req_addr: the start address of the range to unmap
include/drm/drm_gpuvm.h +3 -3
@@ -1205,11 +1205,11 @@ struct drm_gpuvm_ops { };int drm_gpuvm_sm_map (struct drm_gpuvm *gpuvm,void *priv, -u64 addr,u64 range, -struct drm_gem_object *obj,u64 offset); +u64 req_addr,u64 req_range, +struct drm_gem_object *req_obj,u64 req_offset);int drm_gpuvm_sm_unmap (struct drm_gpuvm *gpuvm,void *priv, -u64 addr,u64 range); +u64 req_addr,u64 req_range); void drm_gpuva_map(struct drm_gpuvm *gpuvm, struct drm_gpuva *va,
Signature · 1 line
2.49.0
Re: [PATCH 1/2] drm/gpuvm: Fix doc comments
Quoted 30 lines · Rob Clark
Correctly summerize drm_gpuvm_sm_map/unmap, and fix the parameter order and names. Just something I noticed in passing.
Signed-off-by Rob Clark <[email protected]>--- drivers/gpu/drm/drm_gpuvm.c | 6 +++--- include/drm/drm_gpuvm.h | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-)drivers/gpu/drm/drm_gpuvm.c +2
-2@@ -2299,13 +2299,13 @@ __drm_gpuvm_sm_unmap(struct drm_gpuvm *gpuvm, }/** -* drm_gpuvm_sm_map() - creates the &drm_gpuva_op split/merge steps +* drm_gpuvm_sm_map() - calls the &drm_gpuva_op split/merge steps * @gpuvm: the &drm_gpuvm representing the GPU VA space +* @priv: pointer to a driver private data structure * @req_addr: the start address of the new mapping * @req_range: the range of the new mapping * @req_obj: the &drm_gem_object to map * @req_offset: the offset within the &drm_gem_object -* @priv: pointer to a driver private data structure * * This function iterates the given range of the GPU VA space. It utilizes the * &drm_gpuvm_ops to call back into the driver providing the split and merge
drivers/gpu/drm/drm_gpuvm.c drm_gpuvm_sm_map(struct drm_gpuvm *gpuvm, void *priv, · line 2355
Quoted 8 lines
drivers/gpu/drm/drm_gpuvm.c +1
-1@@ -2349,7 +2349,7 @@ drm_gpuvm_sm_map(struct drm_gpuvm *gpuvm, void *priv,EXPORT_SYMBOL_GPL (drm_gpuvm_sm_map);/** -* drm_gpuvm_sm_unmap() - creates the &drm_gpuva_ops to split on unmap +* drm_gpuvm_sm_unmap() - calls the &drm_gpuva_ops to split on unmap * @gpuvm: the &drm_gpuvm representing the GPU VA space * @priv: pointer to a driver private data structure * @req_addr: the start address of the range to unmap
Thanks for fixing this!
Quoted 6 lines
include/drm/drm_gpuvm.h +0
-0@@ -1205,11 +1205,11 @@ struct drm_gpuvm_ops { };
include/drm/drm_gpuvm.h struct drm_gpuvm_ops { · line 1212
Quoted 10 lines
include/drm/drm_gpuvm.h +3
-3int drm_gpuvm_sm_map (struct drm_gpuvm *gpuvm,void *priv, -u64 addr,u64 range, -struct drm_gem_object *obj,u64 offset); +u64 req_addr,u64 req_range, +struct drm_gem_object *req_obj,u64 req_offset);int drm_gpuvm_sm_unmap (struct drm_gpuvm *gpuvm,void *priv, -u64 addr,u64 range); +u64 req_addr,u64 req_range);
Not sure we need to change the argument names though, I'd rather leave them as they are.
PATCH 2/2 drm/gpuvm: Add locking helpers
In reply to Rob ClarkFor UNMAP/REMAP steps we could be needing to lock objects that are not explicitly listed in the VM_BIND ioctl in order to tear-down unmapped VAs. These helpers handle locking/preparing the needed objects.
Note that these functions do not strictly require the VM changes to be applied before the next drm_gpuvm_sm_map_lock()/_unmap_lock() call. In the case that VM changes from an earlier drm_gpuvm_sm_map()/_unmap() call result in a differing sequence of steps when the VM changes are actually applied, it will be the same set of GEM objects involved, so the locking is still correct.
--- drivers/gpu/drm/drm_gpuvm.c | 81 +++++++++++++++++++++++++++++++++++++ include/drm/drm_gpuvm.h | 8 ++++ 2 files changed, 89 insertions(+)
drivers/gpu/drm/drm_gpuvm.c +81 -0
@@ -2390,6 +2390,87 @@ drm_gpuvm_sm_unmap(struct drm_gpuvm *gpuvm, void *priv, }EXPORT_SYMBOL_GPL (drm_gpuvm_sm_unmap); +static int +drm_gpuva_sm_step_lock (struct drm_gpuva_op *op,void *priv) +{ +struct drm_exec *exec = priv; + +switch (op->op) { +case DRM_GPUVA_OP_REMAP : +if (op->remap.unmap->va->gem.obj) +return drm_exec_lock_obj (exec, op->remap.unmap->va->gem.obj); +return 0 ; +case DRM_GPUVA_OP_UNMAP : +if (op->unmap.va->gem.obj) +return drm_exec_lock_obj (exec, op->unmap.va->gem.obj); +return 0 ; +default : +return 0 ; + } +} + +static const struct drm_gpuvm_ops lock_ops = { + .sm_step_map = drm_gpuva_sm_step_lock, + .sm_step_remap = drm_gpuva_sm_step_lock, + .sm_step_unmap = drm_gpuva_sm_step_lock, +}; + +/** +* drm_gpuvm_sm_map_lock() - locks the objects touched by a drm_gpuvm_sm_map() +* @gpuvm: the &drm_gpuvm representing the GPU VA space +* @exec: the &drm_exec locking context +* @num_fences: for newly mapped objects, the # of fences to reserve +* @req_addr: the start address of the range to unmap +* @req_range: the range of the mappings to unmap +* @req_obj: the &drm_gem_object to map +* @req_offset: the offset within the &drm_gem_object +* +* This function locks (drm_exec_lock_obj()) objects that will be unmapped/ +* remapped, and locks+prepares (drm_exec_prepare_object()) objects that +* will be newly mapped. +* +* Returns: 0 on success or a negative error code +*/ +int +drm_gpuvm_sm_map_lock (struct drm_gpuvm *gpuvm, +struct drm_exec *exec,unsigned int num_fences, +u64 req_addr,u64 req_range, +struct drm_gem_object *req_obj,u64 req_offset) +{ +if (req_obj) { +int ret =drm_exec_prepare_obj (exec, req_obj, num_fences); +if (ret) +return ret; + } + +return __drm_gpuvm_sm_map (gpuvm, &lock_ops, exec, + req_addr, req_range, + req_obj, req_offset); + +} +EXPORT_SYMBOL_GPL (drm_gpuvm_sm_map_lock); + +/** +* drm_gpuvm_sm_unmap_lock() - locks the objects touched by drm_gpuvm_sm_unmap() +* @gpuvm: the &drm_gpuvm representing the GPU VA space +* @exec: the &drm_exec locking context +* @req_addr: the start address of the range to unmap +* @req_range: the range of the mappings to unmap +* +* This function locks (drm_exec_lock_obj()) objects that will be unmapped/ +* remapped by drm_gpuvm_sm_unmap(). +* +* Returns: 0 on success or a negative error code +*/ +int +drm_gpuvm_sm_unmap_lock (struct drm_gpuvm *gpuvm,struct drm_exec *exec, +u64 req_addr,u64 req_range) +{ +return __drm_gpuvm_sm_unmap (gpuvm, &lock_ops, exec, + req_addr, req_range); +} +EXPORT_SYMBOL_GPL (drm_gpuvm_sm_unmap_lock); +static struct drm_gpuva_op *gpuva_op_alloc (struct drm_gpuvm *gpuvm) {
include/drm/drm_gpuvm.h +8 -0
@@ -1211,6 +1211,14 @@ int drm_gpuvm_sm_map(struct drm_gpuvm *gpuvm, void *priv,int drm_gpuvm_sm_unmap (struct drm_gpuvm *gpuvm,void *priv,u64 req_addr,u64 req_range); +int drm_gpuvm_sm_map_lock (struct drm_gpuvm *gpuvm, +struct drm_exec *exec,unsigned int num_fences, +u64 req_addr,u64 req_range, +struct drm_gem_object *obj,u64 offset); + +int drm_gpuvm_sm_unmap_lock (struct drm_gpuvm *gpuvm,struct drm_exec *exec, +u64 req_addr,u64 req_range); +void drm_gpuva_map (struct drm_gpuvm *gpuvm,struct drm_gpuva *va,struct drm_gpuva_op_map *op);
Signature · 1 line
2.49.0
Re: [PATCH 2/2] drm/gpuvm: Add locking helpers
Quoted 5 lines · Rob Clark
For UNMAP/REMAP steps we could be needing to lock objects that are not explicitly listed in the
VM_BINDioctl in order to tear-down unmapped VAs. These helpers handle locking/preparing the needed objects.
Quoted 8 lines
Note that these functions do not strictly require the VM changes to be applied before the next drm_gpuvm_sm_map_lock()/_unmap_lock() call. In the case that VM changes from an earlier drm_gpuvm_sm_map()/_unmap() call result in a differing sequence of steps when the VM changes are actually applied, it will be the same set of GEM objects involved, so the locking is still correct.
Signed-off-by Rob Clark <[email protected]>
And if it wasn't clear/obvious, the expected usage is something that
looks like: https://gitlab.freedesktop.org/robclark/msm/-/blob/sparse-newer/drivers/gpu/drm/msm/msm_gem_vma.c?ref_type=heads#L1150
ie. the caller handles the drm_exec_until_all_locked bit
BR,
-R
Quoted 119 lines
--- drivers/gpu/drm/drm_gpuvm.c | 81 +++++++++++++++++++++++++++++++++++++ include/drm/drm_gpuvm.h | 8 ++++ 2 files changed, 89 insertions(+)drivers/gpu/drm/drm_gpuvm.c +81
-0@@ -2390,6 +2390,87 @@ drm_gpuvm_sm_unmap(struct drm_gpuvm *gpuvm, void *priv, }EXPORT_SYMBOL_GPL (drm_gpuvm_sm_unmap); +static int +drm_gpuva_sm_step_lock (struct drm_gpuva_op *op,void *priv) +{ +struct drm_exec *exec = priv; + +switch (op->op) { +case DRM_GPUVA_OP_REMAP : +if (op->remap.unmap->va->gem.obj) +return drm_exec_lock_obj (exec, op->remap.unmap->va->gem.obj); +return 0 ; +case DRM_GPUVA_OP_UNMAP : +if (op->unmap.va->gem.obj) +return drm_exec_lock_obj (exec, op->unmap.va->gem.obj); +return 0 ; +default : +return 0 ; + } +} + +static const struct drm_gpuvm_ops lock_ops = { + .sm_step_map = drm_gpuva_sm_step_lock, + .sm_step_remap = drm_gpuva_sm_step_lock, + .sm_step_unmap = drm_gpuva_sm_step_lock, +}; + +/** +* drm_gpuvm_sm_map_lock() - locks the objects touched by a drm_gpuvm_sm_map() +* @gpuvm: the &drm_gpuvm representing the GPU VA space +* @exec: the &drm_exec locking context +* @num_fences: for newly mapped objects, the # of fences to reserve +* @req_addr: the start address of the range to unmap +* @req_range: the range of the mappings to unmap +* @req_obj: the &drm_gem_object to map +* @req_offset: the offset within the &drm_gem_object +* +* This function locks (drm_exec_lock_obj()) objects that will be unmapped/ +* remapped, and locks+prepares (drm_exec_prepare_object()) objects that +* will be newly mapped. +* +* Returns: 0 on success or a negative error code +*/ +int +drm_gpuvm_sm_map_lock (struct drm_gpuvm *gpuvm, +struct drm_exec *exec,unsigned int num_fences, +u64 req_addr,u64 req_range, +struct drm_gem_object *req_obj,u64 req_offset) +{ +if (req_obj) { +int ret =drm_exec_prepare_obj (exec, req_obj, num_fences); +if (ret) +return ret; + } + +return __drm_gpuvm_sm_map (gpuvm, &lock_ops, exec, + req_addr, req_range, + req_obj, req_offset); + +} +EXPORT_SYMBOL_GPL (drm_gpuvm_sm_map_lock); + +/** +* drm_gpuvm_sm_unmap_lock() - locks the objects touched by drm_gpuvm_sm_unmap() +* @gpuvm: the &drm_gpuvm representing the GPU VA space +* @exec: the &drm_exec locking context +* @req_addr: the start address of the range to unmap +* @req_range: the range of the mappings to unmap +* +* This function locks (drm_exec_lock_obj()) objects that will be unmapped/ +* remapped by drm_gpuvm_sm_unmap(). +* +* Returns: 0 on success or a negative error code +*/ +int +drm_gpuvm_sm_unmap_lock (struct drm_gpuvm *gpuvm,struct drm_exec *exec, +u64 req_addr,u64 req_range) +{ +return __drm_gpuvm_sm_unmap (gpuvm, &lock_ops, exec, + req_addr, req_range); +} +EXPORT_SYMBOL_GPL (drm_gpuvm_sm_unmap_lock); +static struct drm_gpuva_op *gpuva_op_alloc (struct drm_gpuvm *gpuvm) {include/drm/drm_gpuvm.h +8
-1@@ -1211,6 +1211,14 @@ int drm_gpuvm_sm_map(struct drm_gpuvm *gpuvm, void *priv,int drm_gpuvm_sm_unmap (struct drm_gpuvm *gpuvm,void *priv,u64 req_addr,u64 req_range); +int drm_gpuvm_sm_map_lock (struct drm_gpuvm *gpuvm, +struct drm_exec *exec,unsigned int num_fences, +u64 req_addr,u64 req_range, +struct drm_gem_object *obj,u64 offset); + +int drm_gpuvm_sm_unmap_lock (struct drm_gpuvm *gpuvm,struct drm_exec *exec, +u64 req_addr,u64 req_range); +void drm_gpuva_map (struct drm_gpuvm *gpuvm,struct drm_gpuva *va,struct drm_gpuva_op_map *op); --2.49.0
Re: [PATCH 2/2] drm/gpuvm: Add locking helpers
In reply to Rob ClarkQuoted 3 lines · Rob Clark
For UNMAP/REMAP steps we could be needing to lock objects that are not explicitly listed in the
VM_BINDioctl in order to tear-down unmapped VAs. These helpers handle locking/preparing the needed objects.
Yes, that's a common use-case. I think drivers typically iterate through their drm_gpuva_ops to lock those objects.
I had a look at you link [1] and it seems that you keep a list of ops as well by calling vm_op_enqueue() with a new struct msm_vm_op from the callbacks.
Please note that for exactly this case there is the op_alloc callback in struct drm_gpuvm_ops, such that you can allocate a custom op type (i.e. struct msm_vm_op) that embedds a struct drm_gpuva_op.
Given that, I think the proposed locking helpers here would make more sense to operate on struct drm_gpuva_ops, rather than the callbacks.
Besides that, few comments below.
--
One additional unrelated note, please don't use BUG_ON() in your default op switch case. Hitting this case in a driver does not justify to panic the whole kernel. BUG() should only be used if the machine really hits an unrecoverable state. Please prefer a WARN_ON() splat instead.
Quoted 6 lines
Note that these functions do not strictly require the VM changes to be applied before the next drm_gpuvm_sm_map_lock()/_unmap_lock() call. In the case that VM changes from an earlier drm_gpuvm_sm_map()/_unmap() call result in a differing sequence of steps when the VM changes are actually applied, it will be the same set of GEM objects involved, so the locking is still correct.
I'm not sure about this part, how can we be sure that's the case?
Quoted 31 lines
Signed-off-by Rob Clark <[email protected]>--- drivers/gpu/drm/drm_gpuvm.c | 81 +++++++++++++++++++++++++++++++++++++ include/drm/drm_gpuvm.h | 8 ++++ 2 files changed, 89 insertions(+)drivers/gpu/drm/drm_gpuvm.c +17
-0@@ -2390,6 +2390,87 @@ drm_gpuvm_sm_unmap(struct drm_gpuvm *gpuvm, void *priv, }EXPORT_SYMBOL_GPL (drm_gpuvm_sm_unmap); +static int +drm_gpuva_sm_step_lock (struct drm_gpuva_op *op,void *priv) +{ +struct drm_exec *exec = priv; + +switch (op->op) { +case DRM_GPUVA_OP_REMAP : +if (op->remap.unmap->va->gem.obj) +return drm_exec_lock_obj (exec, op->remap.unmap->va->gem.obj); +return 0 ; +case DRM_GPUVA_OP_UNMAP : +if (op->unmap.va->gem.obj) +return drm_exec_lock_obj (exec, op->unmap.va->gem.obj); +return 0 ; +default : +return 0 ; + }
drivers/gpu/drm/drm_gpuvm.c drm_gpuvm_sm_unmap(struct drm_gpuvm *gpuvm, void *priv, · line 2419
Quoted 10 lines
drivers/gpu/drm/drm_gpuvm.c +10
-0+} + +static const struct drm_gpuvm_ops lock_ops = { + .sm_step_map = drm_gpuva_sm_step_lock, + .sm_step_remap = drm_gpuva_sm_step_lock, + .sm_step_unmap = drm_gpuva_sm_step_lock, +}; + +/** +* drm_gpuvm_sm_map_lock() - locks the objects touched by a drm_gpuvm_sm_map()
I think we should name this drm_gpuvm_sm_map_exec_lock() since it only makes sense to call this from some drm_exec loop.
Quoted 15 lines
drivers/gpu/drm/drm_gpuvm.c +15
-0+* @gpuvm: the &drm_gpuvm representing the GPU VA space +* @exec: the &drm_exec locking context +* @num_fences: for newly mapped objects, the # of fences to reserve +* @req_addr: the start address of the range to unmap +* @req_range: the range of the mappings to unmap +* @req_obj: the &drm_gem_object to map +* @req_offset: the offset within the &drm_gem_object +* +* This function locks (drm_exec_lock_obj()) objects that will be unmapped/ +* remapped, and locks+prepares (drm_exec_prepare_object()) objects that +* will be newly mapped. +* +* Returns: 0 on success or a negative error code +*/ +int
drivers/gpu/drm/drm_gpuvm.c drm_gpuvm_sm_unmap(struct drm_gpuvm *gpuvm, void *priv, · line 2444
Quoted 10 lines
drivers/gpu/drm/drm_gpuvm.c +10
-0+drm_gpuvm_sm_map_lock(struct drm_gpuvm *gpuvm, + struct drm_exec *exec, unsigned int num_fences, + u64 req_addr, u64 req_range, + struct drm_gem_object *req_obj, u64 req_offset) +{ + if (req_obj) { + int ret = drm_exec_prepare_obj(exec, req_obj, num_fences); +if (ret) +return ret; + }
Let's move this to drm_gpuva_sm_step_lock().
Quoted 7 lines
drivers/gpu/drm/drm_gpuvm.c +7
-0+ +return __drm_gpuvm_sm_map (gpuvm, &lock_ops, exec, + req_addr, req_range, + req_obj, req_offset); + +} +EXPORT_SYMBOL_GPL (drm_gpuvm_sm_map_lock);
Re: [PATCH 2/2] drm/gpuvm: Add locking helpers
Quoted 9 lines · Danilo Krummrich
Quoted 3 lines · Rob Clark
For UNMAP/REMAP steps we could be needing to lock objects that are not explicitly listed in the
VM_BINDioctl in order to tear-down unmapped VAs. These helpers handle locking/preparing the needed objects.Yes, that's a common use-case. I think drivers typically iterate through their
drm_gpuva_opsto lock those objects.
Quoted 6 lines
I had a look at you link [1] and it seems that you keep a list of ops as well by calling
vm_op_enqueue()with a new structmsm_vm_opfrom the callbacks.Please note that for exactly this case there is the
op_alloccallback in structdrm_gpuvm_ops, such that you can allocate a custom op type (i.e. structmsm_vm_op) that embedds a structdrm_gpuva_op.
I did use drm_gpuvm_sm_xyz_ops_create() in an earlier iteration of my VM_BIND series, but it wasn't quite what I was after. I wanted to apply the VM updates immediately to avoid issues with a later map/unmap overlapping an earlier map, which drm_gpuvm_sm_xyz_ops_create() doesn't really handle. I'm not even sure why this isn't a problem for other drivers unless userspace is providing some guarantees. Once I realized I only wanted to defer the application of the pgtable changes, but keep all the locking/allocation/etc in the synchronous part of the ioctl, vm_op_enqueue() was the natural solution.
Quoted 2 lines
Given that, I think the proposed locking helpers here would make more sense to operate on struct
drm_gpuva_ops, rather than the callbacks.
Hmm, perhaps, but that requires building an otherwise unneeded ops list. So the current approach seemed simpler, and something that would work regardless of whether the driver was using an ops list.
An alternative I was considering was letting the driver pass it's own drm_gpuvm_ops struct where it could implement the locking callback. But locking is enough of a common thing that this approach seemed cleaner.
Quoted 10 lines
Besides that, few comments below.
--
One additional unrelated note, please don't use
BUG_ON()in your default op switch case. Hitting this case in a driver does not justify to panic the whole kernel.BUG()should only be used if the machine really hits an unrecoverable state. Please prefer aWARN_ON()splat instead.
ok, fair
Quoted 8 lines
Quoted 6 lines
Note that these functions do not strictly require the VM changes to be applied before the next drm_gpuvm_sm_map_lock()/_unmap_lock() call. In the case that VM changes from an earlier drm_gpuvm_sm_map()/_unmap() call result in a differing sequence of steps when the VM changes are actually applied, it will be the same set of GEM objects involved, so the locking is still correct.
I'm not sure about this part, how can we be sure that's the case?
I could be not imaginative enough here, so it is certainly worth a second opinion. And why I explicitly called it out in the commit msg. But my reasoning is that any new op in the second pass that actually applies the VM updates which results from overlapping with a previous update in the current VM_BIND will only involve GEM objects from that earlier update, which are already locked.
Quoted 36 lines
Quoted 36 lines
Signed-off-by Rob Clark <[email protected]>--- drivers/gpu/drm/drm_gpuvm.c | 81 +++++++++++++++++++++++++++++++++++++ include/drm/drm_gpuvm.h | 8 ++++ 2 files changed, 89 insertions(+)drivers/gpu/drm/drm_gpuvm.c +22
-0@@ -2390,6 +2390,87 @@ drm_gpuvm_sm_unmap(struct drm_gpuvm *gpuvm, void *priv, }EXPORT_SYMBOL_GPL (drm_gpuvm_sm_unmap); +static int +drm_gpuva_sm_step_lock (struct drm_gpuva_op *op,void *priv) +{ +struct drm_exec *exec = priv; + +switch (op->op) { +case DRM_GPUVA_OP_REMAP : +if (op->remap.unmap->va->gem.obj) +return drm_exec_lock_obj (exec, op->remap.unmap->va->gem.obj); +return 0 ; +case DRM_GPUVA_OP_UNMAP : +if (op->unmap.va->gem.obj) +return drm_exec_lock_obj (exec, op->unmap.va->gem.obj); +return 0 ; +default : +return 0 ; + } +} + +static const struct drm_gpuvm_ops lock_ops = { + .sm_step_map = drm_gpuva_sm_step_lock, + .sm_step_remap = drm_gpuva_sm_step_lock,
Quoted 8 lines
drivers/gpu/drm/drm_gpuvm.c drm_gpuvm_sm_unmap(struct drm_gpuvm *gpuvm, void *priv, · line 2419
Quoted 5 lines
drivers/gpu/drm/drm_gpuvm.c +5
-0+ .sm_step_unmap = drm_gpuva_sm_step_lock, +}; + +/** +* drm_gpuvm_sm_map_lock() - locks the objects touched by a drm_gpuvm_sm_map() I think we should name this
drm_gpuvm_sm_map_exec_lock()since it only makes sense to call this from somedrm_execloop.
fair
Quoted 19 lines
Quoted 19 lines
drivers/gpu/drm/drm_gpuvm.c +19
-0+* @gpuvm: the &drm_gpuvm representing the GPU VA space +* @exec: the &drm_exec locking context +* @num_fences: for newly mapped objects, the # of fences to reserve +* @req_addr: the start address of the range to unmap +* @req_range: the range of the mappings to unmap +* @req_obj: the &drm_gem_object to map +* @req_offset: the offset within the &drm_gem_object +* +* This function locks (drm_exec_lock_obj()) objects that will be unmapped/ +* remapped, and locks+prepares (drm_exec_prepare_object()) objects that +* will be newly mapped. +* +* Returns: 0 on success or a negative error code +*/ +int +drm_gpuvm_sm_map_lock (struct drm_gpuvm *gpuvm, +struct drm_exec *exec,unsigned int num_fences, +u64 req_addr,u64 req_range, +struct drm_gem_object *req_obj,u64 req_offset)
Quoted 8 lines
drivers/gpu/drm/drm_gpuvm.c drm_gpuvm_sm_unmap(struct drm_gpuvm *gpuvm, void *priv, · line 2444
Quoted 6 lines
drivers/gpu/drm/drm_gpuvm.c +6
-0+{ +if (req_obj) { +int ret =drm_exec_prepare_obj (exec, req_obj, num_fences); +if (ret) +return ret; + }Let's move this to
drm_gpuva_sm_step_lock().
My reasoning to handle this special case here is that it avoided passing num_fences down to the callback (which would necessitate having an args struct). I can change this if you feel strongly about it, but this seemed simpler to me.
BR,
-R
Re: [PATCH 2/2] drm/gpuvm: Add locking helpers
Quoted 17 lines · Rob Clark
Quoted 9 lines · Danilo Krummrich
Quoted 3 lines · Rob Clark
For UNMAP/REMAP steps we could be needing to lock objects that are not explicitly listed in the
VM_BINDioctl in order to tear-down unmapped VAs. These helpers handle locking/preparing the needed objects.Yes, that's a common use-case. I think drivers typically iterate through their
drm_gpuva_opsto lock those objects.Quoted 6 lines
I had a look at you link [1] and it seems that you keep a list of ops as well by calling
vm_op_enqueue()with a new structmsm_vm_opfrom the callbacks.Please note that for exactly this case there is the
op_alloccallback in structdrm_gpuvm_ops, such that you can allocate a custom op type (i.e. structmsm_vm_op) that embedds a structdrm_gpuva_op.
Quoted 7 lines
I did use
drm_gpuvm_sm_xyz_ops_create()in an earlier iteration of myVM_BINDseries, but it wasn't quite what I was after. I wanted to apply the VM updates immediately to avoid issues with a later map/unmap overlapping an earlier map, whichdrm_gpuvm_sm_xyz_ops_create()doesn't really handle. I'm not even sure why this isn't a problem for other drivers unless userspace is providing some guarantees.
The drm_gpuva_ops are usually used in a pattern like this.
vm_bind {
for_each_vm_bind_operation {
drm_gpuva_for_each_op {
// modify drm_gpuvm's interval tree
// pre-allocate memory
// lock and prepare objects
}
}
drm_sched_entity_push_job();
}
run_job {
for_each_vm_bind_operation {
drm_gpuva_for_each_op {
// modify page tables
}
}
}
run_job {
for_each_vm_bind_operation {
drm_gpuva_for_each_op {
// free page table structures, if any
// free unused pre-allocated memory
}
}
}What did you do instead to get map/unmap overlapping? Even more interesting, what are you doing now?
Quoted 4 lines
Once I realized I only wanted to defer the application of the pgtable changes, but keep all the locking/allocation/etc in the synchronous part of the ioctl,
vm_op_enqueue()was the natural solution.
But vm_op_enqueue() creates exactly this list of operations you would get from drm_gpuvm_sm_{map,unmap}_ops_create(), just manually, no?
<snip>
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Note that these functions do not strictly require the VM changes to be applied before the next drm_gpuvm_sm_map_lock()/_unmap_lock() call. In the case that VM changes from an earlier drm_gpuvm_sm_map()/_unmap() call result in a differing sequence of steps when the VM changes are actually applied, it will be the same set of GEM objects involved, so the locking is still correct.
Quoted 8 lines
Quoted 1 line
I'm not sure about this part, how can we be sure that's the case?
I could be not imaginative enough here, so it is certainly worth a second opinion. And why I explicitly called it out in the commit msg. But my reasoning is that any new op in the second pass that actually applies the VM updates which results from overlapping with a previous update in the current
VM_BINDwill only involve GEM objects from that earlier update, which are already locked.
Yeah, it's probably fine, since, as you say, the only additional object can be the req_obj from the previous iteration.
Re: [PATCH 2/2] drm/gpuvm: Add locking helpers
Quoted 58 lines · Danilo Krummrich
Quoted 17 lines · Rob Clark
Quoted 9 lines · Danilo Krummrich
Quoted 3 lines · Rob Clark
For UNMAP/REMAP steps we could be needing to lock objects that are not explicitly listed in the
VM_BINDioctl in order to tear-down unmapped VAs. These helpers handle locking/preparing the needed objects.Yes, that's a common use-case. I think drivers typically iterate through their
drm_gpuva_opsto lock those objects.Quoted 6 lines
I had a look at you link [1] and it seems that you keep a list of ops as well by calling
vm_op_enqueue()with a new structmsm_vm_opfrom the callbacks.Please note that for exactly this case there is the
op_alloccallback in structdrm_gpuvm_ops, such that you can allocate a custom op type (i.e. structmsm_vm_op) that embedds a structdrm_gpuva_op.Quoted 7 lines
I did use
drm_gpuvm_sm_xyz_ops_create()in an earlier iteration of myVM_BINDseries, but it wasn't quite what I was after. I wanted to apply the VM updates immediately to avoid issues with a later map/unmap overlapping an earlier map, whichdrm_gpuvm_sm_xyz_ops_create()doesn't really handle. I'm not even sure why this isn't a problem for other drivers unless userspace is providing some guarantees.The
drm_gpuva_opsare usually used in a pattern like this.vm_bind { for_each_vm_bind_operation { drm_gpuva_for_each_op { // modify drm_gpuvm's interval tree // pre-allocate memory // lock and prepare objects } } drm_sched_entity_push_job(); } run_job { for_each_vm_bind_operation { drm_gpuva_for_each_op { // modify page tables } } } run_job { for_each_vm_bind_operation { drm_gpuva_for_each_op { // free page table structures, if any // free unused pre-allocated memory } } }
Quoted 2 lines
What did you do instead to get map/unmap overlapping? Even more interesting, what are you doing now?
From what I can tell, the drivers using drm_gpva_for_each_op()/etc are doing drm_gpuva_remove() while iterating the ops list.. drm_gpuvm_sm_xyz_ops_create() itself does not modify the VM. So this can only really work if you perform one MAP or UNMAP at a time. Or at least if you process the VM modifying part of the ops list before proceeding to the next op.
Quoted 8 lines
Quoted 5 lines
Once I realized I only wanted to defer the application of the pgtable changes, but keep all the locking/allocation/etc in the synchronous part of the ioctl,
vm_op_enqueue()was the natural solution.But
vm_op_enqueue()creates exactly this list of operations you would get from drm_gpuvm_sm_{map,unmap}_ops_create(), just manually, no?
Only if each job only has a single VM_BIND MAP or UNMAP or if you process the ops immediately.
OTOH vm_op_enqueue() generates the list of pgtable updates to perform for a list of MAP/UNMAP ioctl ops. I guess it would be an equivalent of combining drm_gpuvm_sm_xyz_ops_create() plus the actual driver callbacks in a single pass.
BR,
-R
Quoted 21 lines
<snip>
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Note that these functions do not strictly require the VM changes to be applied before the next drm_gpuvm_sm_map_lock()/_unmap_lock() call. In the case that VM changes from an earlier drm_gpuvm_sm_map()/_unmap() call result in a differing sequence of steps when the VM changes are actually applied, it will be the same set of GEM objects involved, so the locking is still correct.
Quoted 8 lines
Quoted 1 line
I'm not sure about this part, how can we be sure that's the case?
I could be not imaginative enough here, so it is certainly worth a second opinion. And why I explicitly called it out in the commit msg. But my reasoning is that any new op in the second pass that actually applies the VM updates which results from overlapping with a previous update in the current
VM_BINDwill only involve GEM objects from that earlier update, which are already locked.Yeah, it's probably fine, since, as you say, the only additional object can be the
req_objfrom the previous iteration.
Re: [PATCH 2/2] drm/gpuvm: Add locking helpers
Quoted 28 lines · Rob Clark
Quoted 27 lines · Danilo Krummrich
Quoted 24 lines · Rob Clark
Quoted 9 lines · Danilo Krummrich
Quoted 3 lines · Rob Clark
For UNMAP/REMAP steps we could be needing to lock objects that are not explicitly listed in the
VM_BINDioctl in order to tear-down unmapped VAs. These helpers handle locking/preparing the needed objects.Yes, that's a common use-case. I think drivers typically iterate through their
drm_gpuva_opsto lock those objects.Quoted 6 lines
I had a look at you link [1] and it seems that you keep a list of ops as well by calling
vm_op_enqueue()with a new structmsm_vm_opfrom the callbacks.Please note that for exactly this case there is the
op_alloccallback in structdrm_gpuvm_ops, such that you can allocate a custom op type (i.e. structmsm_vm_op) that embedds a structdrm_gpuva_op.I did use
drm_gpuvm_sm_xyz_ops_create()in an earlier iteration of myVM_BINDseries, but it wasn't quite what I was after. I wanted to apply the VM updates immediately to avoid issues with a later map/unmap overlapping an earlier map, whichdrm_gpuvm_sm_xyz_ops_create()doesn't really handle. I'm not even sure why this isn't a problem for other drivers unless userspace is providing some guarantees.
Quoted 4 lines
Quoted 4 lines
The
drm_gpuva_opsare usually used in a pattern like this.vm_bind { for_each_vm_bind_operation {
drm_gpuvm_sm_xyz_ops_create();
Quoted 30 lines
Quoted 27 lines
drm_gpuva_for_each_op { // modify drm_gpuvm's interval tree // pre-allocate memory // lock and prepare objects } } drm_sched_entity_push_job(); } run_job { for_each_vm_bind_operation { drm_gpuva_for_each_op { // modify page tables } } } run_job { for_each_vm_bind_operation { drm_gpuva_for_each_op { // free page table structures, if any // free unused pre-allocated memory } } }Quoted 2 lines
What did you do instead to get map/unmap overlapping? Even more interesting, what are you doing now?
Quoted 6 lines
From what I can tell, the drivers using drm_gpva_for_each_op()/etc are doing
drm_gpuva_remove()while iterating the ops list..drm_gpuvm_sm_xyz_ops_create()itself does not modify the VM. So this can only really work if you perform one MAP or UNMAP at a time. Or at least if you process the VM modifying part of the ops list before proceeding to the next op.
(Added the drm_gpuvm_sm_xyz_ops_create() step above.)
I went through the code you posted [1] and conceptually you're implementing exactly the pattern I described above, i.e. you do:
vm_bind {
for_each_vm_bind_operation {
drm_gpuvm_sm_xyz_exec_lock();
}
for_each_vm_bind_operation {
drm_gpuvm_sm_xyz() {
// modify drm_gpuvm's interval tree
// create custom ops
}
}
drm_sched_entity_push_job();
}
run_job {
for_each_vm_bind_operation {
for_each_custom_op() {
// do stuff
}
}
}However, GPUVM intends to solve your use-case with the following, semantically identical, approach.
vm_bind {
for_each_vm_bind_operation {
drm_gpuvm_sm_xyz_ops_create();
drm_gpuva_for_each_op {
// modify drm_gpuvm's interval tree
// lock and prepare objects (1)
}
}
drm_sched_entity_push_job();
}
run_job {
for_each_vm_bind_operation {
drm_gpuva_for_each_op() {
// do stuff
}
}
}(Note that GPUVM already supports to extend the existing OP structures; you should take advantage of that.)
Hence, the helper we really want is to lock and prepare the objects at (1). I.e. a helper that takes a pointer to a struct drm_gpuva_op and locks / validates the corresponding objects.
[1] https://gitlab.freedesktop.org/robclark/msm/-/blob/sparse-newer/drivers/gpu/drm/msm/msm_gem_vma.c
Re: [PATCH 2/2] drm/gpuvm: Add locking helpers
Quoted 92 lines · Danilo Krummrich
Quoted 28 lines · Rob Clark
Quoted 27 lines · Danilo Krummrich
Quoted 24 lines · Rob Clark
Quoted 9 lines · Danilo Krummrich
Quoted 3 lines · Rob Clark
For UNMAP/REMAP steps we could be needing to lock objects that are not explicitly listed in the
VM_BINDioctl in order to tear-down unmapped VAs. These helpers handle locking/preparing the needed objects.Yes, that's a common use-case. I think drivers typically iterate through their
drm_gpuva_opsto lock those objects.Quoted 6 lines
I had a look at you link [1] and it seems that you keep a list of ops as well by calling
vm_op_enqueue()with a new structmsm_vm_opfrom the callbacks.Please note that for exactly this case there is the
op_alloccallback in structdrm_gpuvm_ops, such that you can allocate a custom op type (i.e. structmsm_vm_op) that embedds a structdrm_gpuva_op.I did use
drm_gpuvm_sm_xyz_ops_create()in an earlier iteration of myVM_BINDseries, but it wasn't quite what I was after. I wanted to apply the VM updates immediately to avoid issues with a later map/unmap overlapping an earlier map, whichdrm_gpuvm_sm_xyz_ops_create()doesn't really handle. I'm not even sure why this isn't a problem for other drivers unless userspace is providing some guarantees.Quoted 4 lines
Quoted 4 lines
The
drm_gpuva_opsare usually used in a pattern like this.vm_bind { for_each_vm_bind_operation {drm_gpuvm_sm_xyz_ops_create();Quoted 30 lines
Quoted 27 lines
drm_gpuva_for_each_op { // modify drm_gpuvm's interval tree // pre-allocate memory // lock and prepare objects } } drm_sched_entity_push_job(); } run_job { for_each_vm_bind_operation { drm_gpuva_for_each_op { // modify page tables } } } run_job { for_each_vm_bind_operation { drm_gpuva_for_each_op { // free page table structures, if any // free unused pre-allocated memory } } }Quoted 2 lines
What did you do instead to get map/unmap overlapping? Even more interesting, what are you doing now?
Quoted 6 lines
From what I can tell, the drivers using drm_gpva_for_each_op()/etc are doing
drm_gpuva_remove()while iterating the ops list..drm_gpuvm_sm_xyz_ops_create()itself does not modify the VM. So this can only really work if you perform one MAP or UNMAP at a time. Or at least if you process the VM modifying part of the ops list before proceeding to the next op.(Added the
drm_gpuvm_sm_xyz_ops_create()step above.)I went through the code you posted [1] and conceptually you're implementing exactly the pattern I described above, i.e. you do:
vm_bind { for_each_vm_bind_operation { drm_gpuvm_sm_xyz_exec_lock(); } for_each_vm_bind_operation { drm_gpuvm_sm_xyz() { // modify drm_gpuvm's interval tree // create custom ops } } drm_sched_entity_push_job(); }
Quoted 7 lines
run_job {
for_each_vm_bind_operation {
for_each_custom_op() {
// do stuff
}
}
}Close, but by the time we get to run_job there is just a single list of ops covering all the vm_bind operations:
run_job {
for_each_custom_op() {
// do stuff
}
}rather than a list of va ops per vm_bind op.
Quoted 10 lines
However, GPUVM intends to solve your use-case with the following, semantically identical, approach.
vm_bind { for_each_vm_bind_operation { drm_gpuvm_sm_xyz_ops_create(); drm_gpuva_for_each_op { // modify drm_gpuvm's interval tree // lock and prepare objects (1)
I currently decouple lock+pin from VM modification to avoid an error path that leaves the VM partially modified. Once you add this back in, the va-ops approach isn't simpler, IMHO.
Quoted 14 lines
}
}
drm_sched_entity_push_job();
}
run_job {
for_each_vm_bind_operation {
drm_gpuva_for_each_op() {
// do stuff
}
}
}Quoted 6 lines
(Note that GPUVM already supports to extend the existing OP structures; you should take advantage of that.)
Hence, the helper we really want is to lock and prepare the objects at (1). I.e. a helper that takes a pointer to a struct
drm_gpuva_opand locks / validates the corresponding objects.
I still prefer that we don't _require_ using va-ops. But if it makes it more useful for other drivers I could add a helper which exec_lock's based on a list of va-ops instead.
BR,
-R
Re: [PATCH 2/2] drm/gpuvm: Add locking helpers
Quoted 120 lines · Rob Clark
Quoted 92 lines · Danilo Krummrich
Quoted 28 lines · Rob Clark
Quoted 27 lines · Danilo Krummrich
Quoted 24 lines · Rob Clark
Quoted 9 lines · Danilo Krummrich
Quoted 3 lines · Rob Clark
For UNMAP/REMAP steps we could be needing to lock objects that are not explicitly listed in the
VM_BINDioctl in order to tear-down unmapped VAs. These helpers handle locking/preparing the needed objects.Yes, that's a common use-case. I think drivers typically iterate through their
drm_gpuva_opsto lock those objects.Quoted 6 lines
I had a look at you link [1] and it seems that you keep a list of ops as well by calling
vm_op_enqueue()with a new structmsm_vm_opfrom the callbacks.Please note that for exactly this case there is the
op_alloccallback in structdrm_gpuvm_ops, such that you can allocate a custom op type (i.e. structmsm_vm_op) that embedds a structdrm_gpuva_op.I did use
drm_gpuvm_sm_xyz_ops_create()in an earlier iteration of myVM_BINDseries, but it wasn't quite what I was after. I wanted to apply the VM updates immediately to avoid issues with a later map/unmap overlapping an earlier map, whichdrm_gpuvm_sm_xyz_ops_create()doesn't really handle. I'm not even sure why this isn't a problem for other drivers unless userspace is providing some guarantees.Quoted 4 lines
Quoted 4 lines
The
drm_gpuva_opsare usually used in a pattern like this.vm_bind { for_each_vm_bind_operation {drm_gpuvm_sm_xyz_ops_create();Quoted 30 lines
Quoted 27 lines
drm_gpuva_for_each_op { // modify drm_gpuvm's interval tree // pre-allocate memory // lock and prepare objects } } drm_sched_entity_push_job(); } run_job { for_each_vm_bind_operation { drm_gpuva_for_each_op { // modify page tables } } } run_job { for_each_vm_bind_operation { drm_gpuva_for_each_op { // free page table structures, if any // free unused pre-allocated memory } } }Quoted 2 lines
What did you do instead to get map/unmap overlapping? Even more interesting, what are you doing now?
Quoted 6 lines
From what I can tell, the drivers using drm_gpva_for_each_op()/etc are doing
drm_gpuva_remove()while iterating the ops list..drm_gpuvm_sm_xyz_ops_create()itself does not modify the VM. So this can only really work if you perform one MAP or UNMAP at a time. Or at least if you process the VM modifying part of the ops list before proceeding to the next op.(Added the
drm_gpuvm_sm_xyz_ops_create()step above.)I went through the code you posted [1] and conceptually you're implementing exactly the pattern I described above, i.e. you do:
vm_bind { for_each_vm_bind_operation { drm_gpuvm_sm_xyz_exec_lock(); } for_each_vm_bind_operation { drm_gpuvm_sm_xyz() { // modify drm_gpuvm's interval tree // create custom ops } } drm_sched_entity_push_job(); }Quoted 7 lines
run_job { for_each_vm_bind_operation { for_each_custom_op() { // do stuff } } }Close, but by the time we get to
run_jobthere is just a single list of ops covering all thevm_bindoperations:run_job { for_each_custom_op() { // do stuff } }rather than a list of va ops per
vm_bindop.Quoted 7 lines
However, GPUVM intends to solve your use-case with the following, semantically identical, approach.
vm_bind { for_each_vm_bind_operation { drm_gpuvm_sm_xyz_ops_create();
Quoted 7 lines
Quoted 3 lines
drm_gpuva_for_each_op { // modify drm_gpuvm's interval tree // lock and prepare objects (1)I currently decouple lock+pin from VM modification to avoid an error path that leaves the VM partially modified. Once you add this back in, the va-ops approach isn't simpler, IMHO.
Oh, actually scratch that.. using va-ops, it is not even possible to decouple locking/prepare from VM modifications. So using DRM_EXEC_INTERRUPTIBLE_WAIT, for ex, with va-ops list would be an actively bad idea.
BR,
-R
Quoted 30 lines
Quoted 14 lines
} } drm_sched_entity_push_job(); } run_job { for_each_vm_bind_operation { drm_gpuva_for_each_op() { // do stuff } } }Quoted 6 lines
(Note that GPUVM already supports to extend the existing OP structures; you should take advantage of that.)
Hence, the helper we really want is to lock and prepare the objects at (1). I.e. a helper that takes a pointer to a struct
drm_gpuva_opand locks / validates the corresponding objects.I still prefer that we don't _require_ using va-ops. But if it makes it more useful for other drivers I could add a helper which exec_lock's based on a list of va-ops instead.
BR,
-R
Re: [PATCH 2/2] drm/gpuvm: Add locking helpers
Quoted 125 lines · Rob Clark
Quoted 124 lines · Rob Clark
Quoted 92 lines · Danilo Krummrich
Quoted 28 lines · Rob Clark
Quoted 27 lines · Danilo Krummrich
Quoted 24 lines · Rob Clark
Quoted 9 lines · Danilo Krummrich
Quoted 3 lines · Rob Clark
For UNMAP/REMAP steps we could be needing to lock objects that are not explicitly listed in the
VM_BINDioctl in order to tear-down unmapped VAs. These helpers handle locking/preparing the needed objects.Yes, that's a common use-case. I think drivers typically iterate through their
drm_gpuva_opsto lock those objects.Quoted 6 lines
I had a look at you link [1] and it seems that you keep a list of ops as well by calling
vm_op_enqueue()with a new structmsm_vm_opfrom the callbacks.Please note that for exactly this case there is the
op_alloccallback in structdrm_gpuvm_ops, such that you can allocate a custom op type (i.e. structmsm_vm_op) that embedds a structdrm_gpuva_op.I did use
drm_gpuvm_sm_xyz_ops_create()in an earlier iteration of myVM_BINDseries, but it wasn't quite what I was after. I wanted to apply the VM updates immediately to avoid issues with a later map/unmap overlapping an earlier map, whichdrm_gpuvm_sm_xyz_ops_create()doesn't really handle. I'm not even sure why this isn't a problem for other drivers unless userspace is providing some guarantees.Quoted 4 lines
Quoted 4 lines
The
drm_gpuva_opsare usually used in a pattern like this.vm_bind { for_each_vm_bind_operation {drm_gpuvm_sm_xyz_ops_create();Quoted 30 lines
Quoted 27 lines
drm_gpuva_for_each_op { // modify drm_gpuvm's interval tree // pre-allocate memory // lock and prepare objects } } drm_sched_entity_push_job(); } run_job { for_each_vm_bind_operation { drm_gpuva_for_each_op { // modify page tables } } } run_job { for_each_vm_bind_operation { drm_gpuva_for_each_op { // free page table structures, if any // free unused pre-allocated memory } } }Quoted 2 lines
What did you do instead to get map/unmap overlapping? Even more interesting, what are you doing now?
Quoted 6 lines
From what I can tell, the drivers using drm_gpva_for_each_op()/etc are doing
drm_gpuva_remove()while iterating the ops list..drm_gpuvm_sm_xyz_ops_create()itself does not modify the VM. So this can only really work if you perform one MAP or UNMAP at a time. Or at least if you process the VM modifying part of the ops list before proceeding to the next op.(Added the
drm_gpuvm_sm_xyz_ops_create()step above.)I went through the code you posted [1] and conceptually you're implementing exactly the pattern I described above, i.e. you do:
vm_bind { for_each_vm_bind_operation { drm_gpuvm_sm_xyz_exec_lock(); } for_each_vm_bind_operation { drm_gpuvm_sm_xyz() { // modify drm_gpuvm's interval tree // create custom ops } } drm_sched_entity_push_job(); }Quoted 7 lines
run_job { for_each_vm_bind_operation { for_each_custom_op() { // do stuff } } }Close, but by the time we get to
run_jobthere is just a single list of ops covering all thevm_bindoperations:run_job { for_each_custom_op() { // do stuff } }rather than a list of va ops per
vm_bindop.Quoted 10 lines
However, GPUVM intends to solve your use-case with the following, semantically identical, approach.
vm_bind { for_each_vm_bind_operation { drm_gpuvm_sm_xyz_ops_create(); drm_gpuva_for_each_op { // modify drm_gpuvm's interval tree // lock and prepare objects (1)
Quoted 8 lines
Quoted 3 lines
I currently decouple lock+pin from VM modification to avoid an error path that leaves the VM partially modified. Once you add this back in, the va-ops approach isn't simpler, IMHO.
Oh, actually scratch that.. using va-ops, it is not even possible to decouple locking/prepare from VM modifications. So using
DRM_EXEC_INTERRUPTIBLE_WAIT, for ex, with va-ops list would be an actively bad idea.
Well, you would need to unwind the VM modifications. I think so far this hasn't been an issue for drivers, since they have to unwind VM modifications for other reasons anyways.
Do you never need to unwind for other reasons than locking dma_resv and preparing GEM objects? Are you really sure there's nothing else in the critical path?
If there really isn't anything, I agree that those helpers have value and we should add them. So, if we do so, please document in detail the conditions under which drm_gpuvm_sm_{map,unmap}_exec_lock() can be called for multiple VM_BIND ops *without* updating GPUVM's interval tree intermediately, including an example.
Re: [PATCH 2/2] drm/gpuvm: Add locking helpers
Quoted 131 lines · Danilo Krummrich
Quoted 129 lines · Rob Clark
Quoted 127 lines · Rob Clark
Quoted 92 lines · Danilo Krummrich
Quoted 28 lines · Rob Clark
Quoted 27 lines · Danilo Krummrich
Quoted 24 lines · Rob Clark
Quoted 9 lines · Danilo Krummrich
Quoted 3 lines · Rob Clark
For UNMAP/REMAP steps we could be needing to lock objects that are not explicitly listed in the
VM_BINDioctl in order to tear-down unmapped VAs. These helpers handle locking/preparing the needed objects.Yes, that's a common use-case. I think drivers typically iterate through their
drm_gpuva_opsto lock those objects.Quoted 6 lines
I had a look at you link [1] and it seems that you keep a list of ops as well by calling
vm_op_enqueue()with a new structmsm_vm_opfrom the callbacks.Please note that for exactly this case there is the
op_alloccallback in structdrm_gpuvm_ops, such that you can allocate a custom op type (i.e. structmsm_vm_op) that embedds a structdrm_gpuva_op.I did use
drm_gpuvm_sm_xyz_ops_create()in an earlier iteration of myVM_BINDseries, but it wasn't quite what I was after. I wanted to apply the VM updates immediately to avoid issues with a later map/unmap overlapping an earlier map, whichdrm_gpuvm_sm_xyz_ops_create()doesn't really handle. I'm not even sure why this isn't a problem for other drivers unless userspace is providing some guarantees.Quoted 4 lines
Quoted 4 lines
The
drm_gpuva_opsare usually used in a pattern like this.vm_bind { for_each_vm_bind_operation {drm_gpuvm_sm_xyz_ops_create();Quoted 30 lines
Quoted 27 lines
drm_gpuva_for_each_op { // modify drm_gpuvm's interval tree // pre-allocate memory // lock and prepare objects } } drm_sched_entity_push_job(); } run_job { for_each_vm_bind_operation { drm_gpuva_for_each_op { // modify page tables } } } run_job { for_each_vm_bind_operation { drm_gpuva_for_each_op { // free page table structures, if any // free unused pre-allocated memory } } }Quoted 2 lines
What did you do instead to get map/unmap overlapping? Even more interesting, what are you doing now?
Quoted 6 lines
From what I can tell, the drivers using drm_gpva_for_each_op()/etc are doing
drm_gpuva_remove()while iterating the ops list..drm_gpuvm_sm_xyz_ops_create()itself does not modify the VM. So this can only really work if you perform one MAP or UNMAP at a time. Or at least if you process the VM modifying part of the ops list before proceeding to the next op.(Added the
drm_gpuvm_sm_xyz_ops_create()step above.)I went through the code you posted [1] and conceptually you're implementing exactly the pattern I described above, i.e. you do:
vm_bind { for_each_vm_bind_operation { drm_gpuvm_sm_xyz_exec_lock(); } for_each_vm_bind_operation { drm_gpuvm_sm_xyz() { // modify drm_gpuvm's interval tree // create custom ops } } drm_sched_entity_push_job(); }Quoted 7 lines
run_job { for_each_vm_bind_operation { for_each_custom_op() { // do stuff } } }Close, but by the time we get to
run_jobthere is just a single list of ops covering all thevm_bindoperations:run_job { for_each_custom_op() { // do stuff } }rather than a list of va ops per
vm_bindop.Quoted 10 lines
However, GPUVM intends to solve your use-case with the following, semantically identical, approach.
vm_bind { for_each_vm_bind_operation { drm_gpuvm_sm_xyz_ops_create(); drm_gpuva_for_each_op { // modify drm_gpuvm's interval tree // lock and prepare objects (1)I currently decouple lock+pin from VM modification to avoid an error path that leaves the VM partially modified. Once you add this back in, the va-ops approach isn't simpler, IMHO.
Quoted 8 lines
Quoted 4 lines
Oh, actually scratch that.. using va-ops, it is not even possible to decouple locking/prepare from VM modifications. So using
DRM_EXEC_INTERRUPTIBLE_WAIT, for ex, with va-ops list would be an actively bad idea.Well, you would need to unwind the VM modifications. I think so far this hasn't been an issue for drivers, since they have to unwind VM modifications for other reasons anyways.
Only thing that can fail at this point are the drm_gpuvm_sm_xyz() calls[1]. Which should only be for small kmalloc's which should not fail. But all the "usual" errors (bad params from userspace, interrupted locking, etc) are dealt with before we begin to modify the VM. If anything does fail after we start modifying the VM we mark the vm as unusable, similar to a gpu fault.
[1] ok, I should put some drm_gpuvm_range_valid() checks earlier in the ioctl, while parsing out and validating args/flags/etc. I overlooked this.
Quoted 9 lines
Do you never need to unwind for other reasons than locking
dma_resvand preparing GEM objects? Are you really sure there's nothing else in the critical path?If there really isn't anything, I agree that those helpers have value and we should add them. So, if we do so, please document in detail the conditions under which drm_gpuvm_sm_{map,unmap}_exec_lock() can be called for multiple
VM_BINDops *without* updating GPUVM's interval tree intermediately, including an example.
Ok.. in the function headerdoc comment block? Or did you have somewhere else in mind?
BR,
-R
Re: [PATCH 2/2] drm/gpuvm: Add locking helpers
Quoted 137 lines · Rob Clark
Quoted 136 lines · Danilo Krummrich
Quoted 133 lines · Rob Clark
Quoted 120 lines · Rob Clark
Quoted 92 lines · Danilo Krummrich
Quoted 28 lines · Rob Clark
Quoted 27 lines · Danilo Krummrich
Quoted 24 lines · Rob Clark
Quoted 9 lines · Danilo Krummrich
Quoted 3 lines · Rob Clark
For UNMAP/REMAP steps we could be needing to lock objects that are not explicitly listed in the
VM_BINDioctl in order to tear-down unmapped VAs. These helpers handle locking/preparing the needed objects.Yes, that's a common use-case. I think drivers typically iterate through their
drm_gpuva_opsto lock those objects.Quoted 6 lines
I had a look at you link [1] and it seems that you keep a list of ops as well by calling
vm_op_enqueue()with a new structmsm_vm_opfrom the callbacks.Please note that for exactly this case there is the
op_alloccallback in structdrm_gpuvm_ops, such that you can allocate a custom op type (i.e. structmsm_vm_op) that embedds a structdrm_gpuva_op.I did use
drm_gpuvm_sm_xyz_ops_create()in an earlier iteration of myVM_BINDseries, but it wasn't quite what I was after. I wanted to apply the VM updates immediately to avoid issues with a later map/unmap overlapping an earlier map, whichdrm_gpuvm_sm_xyz_ops_create()doesn't really handle. I'm not even sure why this isn't a problem for other drivers unless userspace is providing some guarantees.Quoted 4 lines
Quoted 4 lines
The
drm_gpuva_opsare usually used in a pattern like this.vm_bind { for_each_vm_bind_operation {drm_gpuvm_sm_xyz_ops_create();Quoted 30 lines
Quoted 27 lines
drm_gpuva_for_each_op { // modify drm_gpuvm's interval tree // pre-allocate memory // lock and prepare objects } } drm_sched_entity_push_job(); } run_job { for_each_vm_bind_operation { drm_gpuva_for_each_op { // modify page tables } } } run_job { for_each_vm_bind_operation { drm_gpuva_for_each_op { // free page table structures, if any // free unused pre-allocated memory } } }Quoted 2 lines
What did you do instead to get map/unmap overlapping? Even more interesting, what are you doing now?
Quoted 6 lines
From what I can tell, the drivers using drm_gpva_for_each_op()/etc are doing
drm_gpuva_remove()while iterating the ops list..drm_gpuvm_sm_xyz_ops_create()itself does not modify the VM. So this can only really work if you perform one MAP or UNMAP at a time. Or at least if you process the VM modifying part of the ops list before proceeding to the next op.(Added the
drm_gpuvm_sm_xyz_ops_create()step above.)I went through the code you posted [1] and conceptually you're implementing exactly the pattern I described above, i.e. you do:
vm_bind { for_each_vm_bind_operation { drm_gpuvm_sm_xyz_exec_lock(); } for_each_vm_bind_operation { drm_gpuvm_sm_xyz() { // modify drm_gpuvm's interval tree // create custom ops } } drm_sched_entity_push_job(); }Quoted 7 lines
run_job { for_each_vm_bind_operation { for_each_custom_op() { // do stuff } } }Close, but by the time we get to
run_jobthere is just a single list of ops covering all thevm_bindoperations:run_job { for_each_custom_op() { // do stuff } }rather than a list of va ops per
vm_bindop.Quoted 7 lines
However, GPUVM intends to solve your use-case with the following, semantically identical, approach.
vm_bind { for_each_vm_bind_operation { drm_gpuvm_sm_xyz_ops_create();Quoted 7 lines
Quoted 3 lines
drm_gpuva_for_each_op { // modify drm_gpuvm's interval tree // lock and prepare objects (1)I currently decouple lock+pin from VM modification to avoid an error path that leaves the VM partially modified. Once you add this back in, the va-ops approach isn't simpler, IMHO.
Oh, actually scratch that.. using va-ops, it is not even possible to decouple locking/prepare from VM modifications. So using
DRM_EXEC_INTERRUPTIBLE_WAIT, for ex, with va-ops list would be an actively bad idea.
Quoted 7 lines
Quoted 3 lines
Well, you would need to unwind the VM modifications. I think so far this hasn't been an issue for drivers, since they have to unwind VM modifications for other reasons anyways.
Only thing that can fail at this point are the
drm_gpuvm_sm_xyz()calls[1]. Which should only be for small kmalloc's which should not fail.
But what happens *if* they fail? How do you handle this? If you don't unwind all preceeding changes to the GPUVM's interval tree at this point your VM is broken.
Glancing at the code, it seems that you're allocating the GPUVA ops. And if that fails you just return the error, leaving the VM in a broken state.
What we could do is to implement a helper that calculates the worst case number of ops and pre-allocate them, but that's not exactly trivial.
drm_gpuvm_sm_{map,unmap}_exec_lock() only really makes sense if we can prevent the need to ever unwind, so that's a precondition.
Alternatively, you can also decide to accept that your VM is dead if you can't allocate the GPUVA ops, I guess. In this case you'd really have to shut it down though, otherwise you likely risk faults in your PT management code.
Quoted 13 lines
But all the "usual" errors (bad params from userspace, interrupted locking, etc) are dealt with before we begin to modify the VM. If anything does fail after we start modifying the VM we mark the vm as unusable, similar to a gpu fault.
[1] ok, I should put some
drm_gpuvm_range_valid()checks earlier in the ioctl, while parsing out and validating args/flags/etc. I overlooked this.Quoted 4 lines
Do you never need to unwind for other reasons than locking
dma_resvand preparing GEM objects? Are you really sure there's nothing else in the critical path?
Quoted 8 lines
Quoted 5 lines
If there really isn't anything, I agree that those helpers have value and we should add them. So, if we do so, please document in detail the conditions under which drm_gpuvm_sm_{map,unmap}_exec_lock() can be called for multiple
VM_BINDops *without* updating GPUVM's interval tree intermediately, including an example.Ok.. in the function headerdoc comment block? Or did you have somewhere else in mind?
Yeah, in the function doc-comment.
Re: [PATCH 2/2] drm/gpuvm: Add locking helpers
Quoted 143 lines · Danilo Krummrich
Quoted 141 lines · Rob Clark
Quoted 139 lines · Danilo Krummrich
Quoted 125 lines · Rob Clark
Quoted 124 lines · Rob Clark
Quoted 92 lines · Danilo Krummrich
Quoted 28 lines · Rob Clark
Quoted 27 lines · Danilo Krummrich
Quoted 24 lines · Rob Clark
Quoted 9 lines · Danilo Krummrich
Quoted 3 lines · Rob Clark
For UNMAP/REMAP steps we could be needing to lock objects that are not explicitly listed in the
VM_BINDioctl in order to tear-down unmapped VAs. These helpers handle locking/preparing the needed objects.Yes, that's a common use-case. I think drivers typically iterate through their
drm_gpuva_opsto lock those objects.Quoted 6 lines
I had a look at you link [1] and it seems that you keep a list of ops as well by calling
vm_op_enqueue()with a new structmsm_vm_opfrom the callbacks.Please note that for exactly this case there is the
op_alloccallback in structdrm_gpuvm_ops, such that you can allocate a custom op type (i.e. structmsm_vm_op) that embedds a structdrm_gpuva_op.I did use
drm_gpuvm_sm_xyz_ops_create()in an earlier iteration of myVM_BINDseries, but it wasn't quite what I was after. I wanted to apply the VM updates immediately to avoid issues with a later map/unmap overlapping an earlier map, whichdrm_gpuvm_sm_xyz_ops_create()doesn't really handle. I'm not even sure why this isn't a problem for other drivers unless userspace is providing some guarantees.Quoted 4 lines
Quoted 4 lines
The
drm_gpuva_opsare usually used in a pattern like this.vm_bind { for_each_vm_bind_operation {drm_gpuvm_sm_xyz_ops_create();Quoted 30 lines
Quoted 27 lines
drm_gpuva_for_each_op { // modify drm_gpuvm's interval tree // pre-allocate memory // lock and prepare objects } } drm_sched_entity_push_job(); } run_job { for_each_vm_bind_operation { drm_gpuva_for_each_op { // modify page tables } } } run_job { for_each_vm_bind_operation { drm_gpuva_for_each_op { // free page table structures, if any // free unused pre-allocated memory } } }Quoted 2 lines
What did you do instead to get map/unmap overlapping? Even more interesting, what are you doing now?
Quoted 6 lines
From what I can tell, the drivers using drm_gpva_for_each_op()/etc are doing
drm_gpuva_remove()while iterating the ops list..drm_gpuvm_sm_xyz_ops_create()itself does not modify the VM. So this can only really work if you perform one MAP or UNMAP at a time. Or at least if you process the VM modifying part of the ops list before proceeding to the next op.(Added the
drm_gpuvm_sm_xyz_ops_create()step above.)I went through the code you posted [1] and conceptually you're implementing exactly the pattern I described above, i.e. you do:
vm_bind { for_each_vm_bind_operation { drm_gpuvm_sm_xyz_exec_lock(); } for_each_vm_bind_operation { drm_gpuvm_sm_xyz() { // modify drm_gpuvm's interval tree // create custom ops } } drm_sched_entity_push_job(); }Quoted 7 lines
run_job { for_each_vm_bind_operation { for_each_custom_op() { // do stuff } } }Close, but by the time we get to
run_jobthere is just a single list of ops covering all thevm_bindoperations:run_job { for_each_custom_op() { // do stuff } }rather than a list of va ops per
vm_bindop.Quoted 10 lines
However, GPUVM intends to solve your use-case with the following, semantically identical, approach.
vm_bind { for_each_vm_bind_operation { drm_gpuvm_sm_xyz_ops_create(); drm_gpuva_for_each_op { // modify drm_gpuvm's interval tree // lock and prepare objects (1)Quoted 8 lines
Quoted 3 lines
I currently decouple lock+pin from VM modification to avoid an error path that leaves the VM partially modified. Once you add this back in, the va-ops approach isn't simpler, IMHO.
Oh, actually scratch that.. using va-ops, it is not even possible to decouple locking/prepare from VM modifications. So using
DRM_EXEC_INTERRUPTIBLE_WAIT, for ex, with va-ops list would be an actively bad idea.Well, you would need to unwind the VM modifications. I think so far this hasn't been an issue for drivers, since they have to unwind VM modifications for other reasons anyways.
Quoted 6 lines
Quoted 3 lines
Only thing that can fail at this point are the
drm_gpuvm_sm_xyz()calls[1]. Which should only be for small kmalloc's which should not fail.But what happens *if* they fail? How do you handle this? If you don't unwind all preceeding changes to the GPUVM's interval tree at this point your VM is broken.
Small GFP_KERNEL allocations will recurse into shrinker to reclaim memory before failing. _If_ they fail, things are in a pretty bad shape already, the best thing to do is mark the VM unusable to signal mesa to close the dev file to tear everything down.
Quoted 5 lines
Glancing at the code, it seems that you're allocating the GPUVA ops. And if that fails you just return the error, leaving the VM in a broken state.
What we could do is to implement a helper that calculates the worst case number of ops and pre-allocate them, but that's not exactly trivial.
No, we shouldn't add this complexity for something that cannot happen (or if it does happen, you are in a state where nothing you do other than tear it all down can improve things).
Quoted 6 lines
drm_gpuvm_sm_{map,unmap}_exec_lock() only really makes sense if we can prevent the need to ever unwind, so that's a precondition.
Alternatively, you can also decide to accept that your VM is dead if you can't allocate the GPUVA ops, I guess. In this case you'd really have to shut it down though, otherwise you likely risk faults in your PT management code.
Correct, we never free backing pages while there is still a mapping to the GPU.. that is the golden rule!
Quoted 19 lines
Quoted 19 lines
But all the "usual" errors (bad params from userspace, interrupted locking, etc) are dealt with before we begin to modify the VM. If anything does fail after we start modifying the VM we mark the vm as unusable, similar to a gpu fault.
[1] ok, I should put some
drm_gpuvm_range_valid()checks earlier in the ioctl, while parsing out and validating args/flags/etc. I overlooked this.Quoted 9 lines
Do you never need to unwind for other reasons than locking
dma_resvand preparing GEM objects? Are you really sure there's nothing else in the critical path?If there really isn't anything, I agree that those helpers have value and we should add them. So, if we do so, please document in detail the conditions under which drm_gpuvm_sm_{map,unmap}_exec_lock() can be called for multiple
VM_BINDops *without* updating GPUVM's interval tree intermediately, including an example.
Quoted 4 lines
Quoted 2 lines
Ok.. in the function headerdoc comment block? Or did you have somewhere else in mind?
Yeah, in the function doc-comment.
ack
BR,
-R