Dynamic drm_pagemaps and Initial multi-device SVM
Subsystems (4)
Matthew Brost
maintainerRe: [PATCH 03/15] drm/pagemap, drm/xe: Manage drm_pagemap provider lifetimes
In reply to Thomas HellströmQuoted 30 lines · Thomas Hellström
Quoted 7 lines · Matthew Brost
Quoted 5 lines · Thomas Hellström
Even if the
drm_pagemapprovider has released its reference on thedrm_pagemap, references may be held by still active pages. Ensure that we hold a reference on the provider drm device and modules for as long as we might need to use thedrm_pagemapops.Quoted 7 lines
Just to make sure I’m understanding this correctly — this is intended to
guard against the devm action [1] running while a device is still holding references to another device’s pages, right?[1]
https://elixir.bootlin.com/linux/v6.17.5/source/kernel/resource.c#L1993Actually removing the
dev_pagemapand its region is allowed while another device holds a reference on the *drm_pagemap*. For example if you have two devices. Device 0 executes from the memory of device 1. Suddenly you feel like offlining / unbinding device 1. When you execute unbind, the driver evicts all SVM bos and thereby frees all device- private pages. But device 0 still has a reference to thedrm_pagemap, even if it's unusable: Any VRAM migration trying to use thedrm_pagemapwill error with-ENODEV, so depending on how the driver handles that, it will continue executing out of another memory region. At this point it would've been possible without this code to rmmod thedrm_pagemapprovider device module, and its drm device would've been freed without this code, and whendrm_pagemap_put()eventually is called, things go boom. So the commit message is a bit misleading.
Quoted 8 lines
In the case where we only have pages left, the last page should be freed from the device remove callback where bos are evicted. At that point, the provider drm device is still alive as the devm callbacks haven't executed yet. Also a rmmod wold typically cause the devm callbacks to execute so that should also be safe without this patch. At least if the page freeing doesn't trigger any async callbacks that aren't waited on before removal.
I believe I get it...
Quoted 5 lines
So yeah, I need to update the commit message a bit. We should also craft an IGT that unbinds device 1 while device 0 is executing out of its memory and verify that execution completes with correct results anyway.
Yes, this is weird corner we certainly should test out.
Matt
Quoted 298 lines
/Thomas
Quoted 294 lines
Quoted 230 lines
Note that in theory, the
drm_gpusvm_helpermodule may be unloaded as soon as the finalmodule_put()of the provider driver module is executed, so we need to add amodule_exit()function that waits for the work item executing themodule_put()has completed.Signed-off-by Thomas Hellström <[email protected]>---
drivers/gpu/drm/drm_pagemap.c | 101
++++++++++++++++++++++++++++++++--
drivers/gpu/drm/xe/xe_svm.c | 15 ++++-
include/drm/drm_pagemap.h | 10 +++-3 files changed, 117 insertions(+), 9 deletions(-)drivers/gpu/drm/drm_pagemap.c +1
-0@@ -8,6 +8,7 @@ #include <linux/pagemap.h> #include <drm/drm_drv.h> #include <drm/drm_pagemap.h> +#include <drm/drm_print.h>
/**
* DOC: Overviewdrivers/gpu/drm/drm_pagemap.c +0
-0@@ -544,16 +545,92 @@ static int drm_pagemap_migrate_populate_ram_pfn(struct vm_area_struct *vas, return -ENOMEM; }drivers/gpu/drm/drm_pagemap.c +3
-0+static void drm_pagemap_dev_unhold_work (struct work_struct *work); +static LLIST_HEAD (drm_pagemap_unhold_list); +static DECLARE_WORK(drm_pagemap_work,
drm_pagemap_dev_unhold_work);drivers/gpu/drm/drm_pagemap.c +3
-0+ +/** +* struct drm_pagemap_dev_hold - Struct to aid in drm_device release.
+ * @link: Link intodrm_pagemap_unhold_listfor deferred reference releases.
+ * @drm: drm device to put.
+ *
+ * When a structdrm_pagemapis released, we also need to release the
+ * reference it holds on the drm device. However, typically that needs
+ * to be done separately from a system-wide workqueue.
+ * Each time a structdrm_pagemapis initialized
+ * (or re-initialized if cached) therefore allocate a separate
+ *drm_pagemap_dev_holditem, from which we put the drm device and
+ * associated module.
+ */drivers/gpu/drm/drm_pagemap.c +5
-0+struct drm_pagemap_dev_hold { +struct llist_node link; +struct drm_device *drm; +}; +static void
drm_pagemap_release(struct kref *ref){
structdrm_pagemap*dpagemap =container_of(ref, typeof(*dpagemap), ref);drivers/gpu/drm/drm_pagemap.c +1
-1- + struct drm_pagemap_dev_hold *dev_hold = dpagemap-Quoted 1 line
dev_hold;drivers/gpu/drm/drm_pagemap.c +3
-0+ +/* +* We know the pagemap provider is alive at this point, since
drivers/gpu/drm/drm_pagemap.c +1
-0+* the struct drm_pagemap_dev_hold holds a reference to the
drivers/gpu/drm/drm_pagemap.c +3
-0+* pagemap provider drm_device and its module. +*/ + dpagemap->dev_hold =NULL ;kfree(dpagemap);
drivers/gpu/drm/drm_pagemap.c +4
-0+llist_add (&dev_hold->link, &drm_pagemap_unhold_list); +schedule_work (&drm_pagemap_work); +/* +* Here, either the provider device is still alive, since if called from
drivers/gpu/drm/drm_pagemap.c +1
-0+* page_free(), the caller is holding a reference on the
dev_pagemap,drivers/gpu/drm/drm_pagemap.c +1
-0+* or if called from drm_pagemap_put(), the direct caller is still alive.
drivers/gpu/drm/drm_pagemap.c +7
-0+* This ensures we can't race with THIS module unload. +*/ +} + +static void drm_pagemap_dev_unhold_work (struct work_struct *work) +{ + struct llist_node *node =
llist_del_all(&drm_pagemap_unhold_list);drivers/gpu/drm/drm_pagemap.c +4
-0+struct drm_pagemap_dev_hold *dev_hold, *next; + +/* +* Deferred release of drm_pagemap provider device and module.
drivers/gpu/drm/drm_pagemap.c +8
-0+* THIS module is kept alive during the release by the +* flush_work() in the drm_pagemap_exit() function. +*/ +llist_for_each_entry_safe (dev_hold, next, node, link) { +struct drm_device *drm = dev_hold->drm; +struct module *module = drm->driver->fops->owner; + + drm_dbg(drm, "Releasing reference on providerdevice and module.\n");
drivers/gpu/drm/drm_pagemap.c +22
-0+drm_dev_put (drm); +module_put (module); +kfree (dev_hold); + } +} + +static struct drm_pagemap_dev_hold * +drm_pagemap_dev_hold (struct drm_pagemap *dpagemap) +{ +struct drm_pagemap_dev_hold *dev_hold; +struct drm_device *drm = dpagemap->drm; + + dev_hold =kzalloc (sizeof (*dev_hold),GFP_KERNEL ); +if (!dev_hold) +return ERR_PTR (-ENOMEM ); + +init_llist_node (&dev_hold->link); + dev_hold->drm = drm; + (void )try_module_get (drm->driver->fops->owner); +drm_dev_get (drm); + +return dev_hold;}
/**
*drm_pagemap_create()- Create a structdrm_pagemap.
- * @dev: Pointer to a struct device providing the device-private memory.
+ * @drm: Pointer to a structdrm_deviceproviding the device- private memory.
* @pagemap: Pointer to a pre-setup structdev_pagemapproviding the struct pages.
* @ops: Pointer to the structdrm_pagemap_ops.
*drivers/gpu/drm/drm_pagemap.c +0
-0@@ -563,20 +640,28 @@ static void drm_pagemap_release(struct kref*ref) * Error pointer on error. */ struct
drm_pagemap*drivers/gpu/drm/drm_pagemap.c +1
-1-drm_pagemap_create(struct device *dev, +drm_pagemap_create(struct drm_device *drm,struct
dev_pagemap*pagemap,
const structdrm_pagemap_ops*ops)
{
structdrm_pagemap*dpagemap =kzalloc(sizeof(*dpagemap), GFP_KERNEL);drivers/gpu/drm/drm_pagemap.c +1
-0+struct drm_pagemap_dev_hold *dev_hold;
if (!dpagemap)
returnERR_PTR(-ENOMEM);
kref_init(&dpagemap->ref);drivers/gpu/drm/drm_pagemap.c +1
-1- dpagemap->dev = dev; + dpagemap->drm = drm;
dpagemap->ops= ops;
dpagemap->pagemap= pagemap;drivers/gpu/drm/drm_pagemap.c +7
-0+ dev_hold =drm_pagemap_dev_hold (dpagemap); +if (IS_ERR (dev_hold)) { +kfree (dpagemap); +return ERR_CAST (dev_hold); + } + dpagemap->dev_hold = dev_hold; +return dpagemap;
}
EXPORT_SYMBOL(drm_pagemap_create);drivers/gpu/drm/drm_pagemap.c +0
-0@@ -937,3 +1022,11 @@ int drm_pagemap_populate_mm(structdrm_pagemap *dpagemap, return err; }EXPORT_SYMBOL(drm_pagemap_populate_mm);
drivers/gpu/drm/drm_pagemap.c +8
-0+ +static void drm_pagemap_exit (void ) +{ +flush_work (&drm_pagemap_work); +if (WARN_ON (!llist_empty (&drm_pagemap_unhold_list))) +disable_work_sync (&drm_pagemap_work); +} +module_exit (drm_pagemap_exit);drivers/gpu/drm/xe/xe_svm.c +1
-1@@ -1437,7 +1437,7 @@ xe_drm_pagemap_device_map(struct drm_pagemap *dpagemap, unsigned int order, enum dma_data_direction dir) { -struct device *pgmap_dev = dpagemap->dev; +struct device *pgmap_dev = dpagemap->drm->dev; enum drm_interconnect_protocol prot; dma_addr_t addr;drivers/gpu/drm/xe/xe_svm.c +0
-0@@ -1457,6 +1457,14 @@ static const struct drm_pagemap_ops xe_drm_pagemap_ops = { .populate_mm = xe_drm_pagemap_populate_mm, };drivers/gpu/drm/xe/xe_svm.c +8
-0+static void xe_devm_release (void *data) +{ +struct xe_vram_region *vr = data; + +drm_pagemap_put (vr->dpagemap); + vr->dpagemap =NULL ; +} +/**
*xe_devm_add: Remap and provide memmap backing for device memory
* @tile: tile that the memory region belongs todrivers/gpu/drm/xe/xe_svm.c +0
-0@@ -1482,7 +1490,7 @@ int xe_devm_add(struct xe_tile *tile, struct xe_vram_region *vr) return ret; }drivers/gpu/drm/xe/xe_svm.c +1
-1- vr->dpagemap = drm_pagemap_create(dev, &vr->pagemap, + vr->dpagemap = drm_pagemap_create(&xe->drm, &vr->pagemap,
&xe_drm_pagemap_ops); if (IS_ERR(vr->dpagemap)) {
drm_err(&xe->drm, "Failed to createdrm_pagemaptile %d memory: %pe\n",drivers/gpu/drm/xe/xe_svm.c int xe_devm_add(struct xe_tile *tile, struct · line 1504
Quoted 7 lines
drivers/gpu/drm/xe/xe_svm.c +3
-0@@ -1490,6 +1498,9 @@ int xe_devm_add(struct xe_tile *tile, struct xe_vram_region *vr) ret = PTR_ERR(vr->dpagemap); goto out_no_dpagemap; } + ret =devm_add_action_or_reset (dev, xe_devm_release, vr); +if (ret) + goto out_no_dpagemap;I mentioned this in first patch that this was missing, maybe move this
part to the first patch even though this will get removed a bit later.Matt
Quoted 49 lines
vr->pagemap.type=MEMORY_DEVICE_PRIVATE;
vr->pagemap.range.start=res->start;include/drm/drm_pagemap.h +0
-0@@ -9,6 +9,7 @@ #define NR_PAGES(order) (1U << (order))
structdrm_pagemap;
+structdrm_pagemap_dev_hold;
structdrm_pagemap_zdd;
struct device;include/drm/drm_pagemap.h +5
-2@@ -130,14 +131,17 @@ struct drm_pagemap_ops {* used for device p2p handshaking. * @ops: The struct drm_pagemap_ops. * @ref: Reference count. -* @dev: The struct drevice owning the device-private memory. +* @drm: The struct drm device owning the device-private memory. * @pagemap: Pointer to the underlying dev_pagemap. +* @dev_hold: Pointer to a struct drm_pagemap_dev_hold for +* device referencing. */ struct drm_pagemap { const struct drm_pagemap_ops *ops; struct kref ref; -struct device *dev; +struct drm_device *drm; struct dev_pagemap *pagemap; +struct drm_pagemap_dev_hold *dev_hold; };
structdrm_pagemap_devmem;include/drm/drm_pagemap.h +0
-0@@ -206,7 +210,7 @@ struct drm_pagemap_devmem_ops { unsigned long npages); };include/drm/drm_pagemap.h +1
-1-struct drm_pagemap *drm_pagemap_create(struct device *dev, +struct drm_pagemap *drm_pagemap_create(struct drm_device *drm,struct
dev_pagemap*pagemap,
const structdrm_pagemap_ops*ops);Signature · 1 line
2.51.0
Re: [PATCH 09/15] drm/xe: Pass a drm_pagemap pointer around with the memory advise attributes
In reply to Thomas HellströmQuoted 5 lines · Thomas Hellström
As a consequence, struct
xe_vma_mem_attr()can't simply be assigned or freed without taking the reference count of individual members into account. Also add helpers to do that.Signed-off-by Thomas Hellström <[email protected]>
Quoted 178 lines
--- drivers/gpu/drm/xe/xe_svm.c | 2 +- drivers/gpu/drm/xe/xe_vm.c | 36 +++++++++++++++++++++++++----- drivers/gpu/drm/xe/xe_vm.h | 1 + drivers/gpu/drm/xe/xe_vm_madvise.c | 1 + drivers/gpu/drm/xe/xe_vm_types.h | 9 ++++++++ 5 files changed, 43 insertions(+), 6 deletions(-)drivers/gpu/drm/xe/xe_svm.c +1
-1@@ -330,7 +330,7 @@ static int xe_svm_range_set_default_attr(struct xe_vm *vm, u64 range_start, u64if (xe_vma_start (vma) == range_start &&xe_vma_end (vma) == range_end) { default_attr.pat_index = vma->attr.default_pat_index; default_attr.default_pat_index = vma->attr.default_pat_index; - vma->attr = default_attr; +xe_vma_mem_attr_copy (&vma->attr, &default_attr); }else {vm_dbg (&vm->xe->drm,"Split VMA start=0x%016llx, vma_end=0x%016llx" , range_start, range_end);drivers/gpu/drm/xe/xe_vm.c +31
-5@@ -964,6 +964,27 @@ static void xe_vma_free(struct xe_vma *vma)kfree (vma); } +static void xe_vma_mem_attr_fini (struct xe_vma_mem_attr *attr) +{ +drm_pagemap_put (attr->preferred_loc.dpagemap); +} + +/** +* xe_vma_mem_attr_copy() - copy an xe_vma_mem_attr structure. +* @to: Destination. +* @from: Source. +* +* Copies an xe_vma_mem_attr structure taking care to get reference +* counting of individual members right. +*/ +void xe_vma_mem_attr_copy (struct xe_vma_mem_attr *to,struct xe_vma_mem_attr *from) +{ +xe_vma_mem_attr_fini (to); + *to = *from; +if (to->preferred_loc.dpagemap) +drm_pagemap_get (to->preferred_loc.dpagemap); +} + static struct xe_vma *xe_vma_create(struct xe_vm *vm, struct xe_bo *bo, u64 bo_offset_or_userptr,@@ -1014,8 +1035,7 @@ static struct xe_vma *xe_vma_create(struct xe_vm *vm,if (vm->xe->info.has_atomic_enable_pte_bit) vma->gpuva.flags |=XE_VMA_ATOMIC_PTE_BIT ; - vma->attr = *attr; - +xe_vma_mem_attr_copy (&vma->attr, attr);if (bo) {struct drm_gpuvm_bo *vm_bo;@@ -1023,6 +1043,7 @@ static struct xe_vma *xe_vma_create(struct xe_vm *vm, vm_bo =drm_gpuvm_bo_obtain (vma->gpuva.vm, &bo->ttm.base);if (IS_ERR (vm_bo)) { +xe_vma_mem_attr_fini (&vma->attr);xe_vma_free (vma);return ERR_CAST (vm_bo); }@@ -1042,6 +1063,7 @@ static struct xe_vma *xe_vma_create(struct xe_vm *vm, err =xe_userptr_setup (uvma,xe_vma_userptr (vma), size);if (err) { +xe_vma_mem_attr_fini (&vma->attr);xe_vma_free (vma);return ERR_PTR (err); }@@ -1057,6 +1079,8 @@ static void xe_vma_destroy_late(struct xe_vma *vma) {struct xe_vm *vm =xe_vma_vm (vma); +xe_vma_mem_attr_fini (&vma->attr); +if (vma->ufence) {xe_sync_ufence_put (vma->ufence); vma->ufence =NULL ;@@ -4221,7 +4245,7 @@ static int xe_vm_alloc_vma(struct xe_vm *vm,struct drm_gpuva_op *__op;unsigned int vma_flags =0 ;bool remap_op = false; -struct xe_vma_mem_attr tmp_attr; +struct xe_vma_mem_attr tmp_attr = {};u16 default_pat;int err;@@ -4314,7 +4338,7 @@ static int xe_vm_alloc_vma(struct xe_vm *vm,* VMA, so they can be assigned to newly MAP created vma. */ if (is_madvise) - tmp_attr = vma->attr; +xe_vma_mem_attr_copy (&tmp_attr, &vma->attr);xe_vma_destroy (gpuva_to_vma (op->base.remap.unmap->va),NULL ); }else if (__op->op ==DRM_GPUVA_OP_MAP ) {@@ -4324,12 +4348,13 @@ static int xe_vm_alloc_vma(struct xe_vm *vm,* copy them to new vma. */ if (is_madvise) - vma->attr = tmp_attr; +xe_vma_mem_attr_copy (&vma->attr, &tmp_attr); } }xe_vm_unlock (vm);drm_gpuva_ops_free (&vm->gpuvm, ops); +xe_vma_mem_attr_fini (&tmp_attr);return 0 ; unwind_ops:@@ -4387,3 +4412,4 @@ int xe_vm_alloc_cpu_addr_mirror_vma(struct xe_vm *vm, uint64_t start, uint64_t rreturn xe_vm_alloc_vma (vm, &map_req, false); } +drivers/gpu/drm/xe/xe_vm.h +1
-0@@ -411,4 +411,5 @@ static inline struct drm_exec *xe_vm_validation_exec(struct xe_vm *vm)#define xe_vm_has_valid_gpu_mapping (tile, tile_present, tile_invalidated) \ ((READ_ONCE(tile_present) & ~READ_ONCE(tile_invalidated)) & BIT((tile)->id)) +void xe_vma_mem_attr_copy (struct xe_vma_mem_attr *to,struct xe_vma_mem_attr *from);#endif drivers/gpu/drm/xe/xe_vm_madvise.c +1
-0@@ -95,6 +95,7 @@ static void madvise_preferred_mem_loc(struct xe_device *xe, struct xe_vm *vm,*/ vmas[i]->attr.preferred_loc.migration_policy = op->preferred_mem_loc.migration_policy; + vmas[i]->attr.preferred_loc.dpagemap =NULL ; } } }drivers/gpu/drm/xe/xe_vm_types.h +9
-0@@ -20,6 +20,8 @@#include "xe_range_fence.h" #include "xe_userptr.h" +struct drm_pagemap ; +struct xe_bo ;struct xe_svm_range ;struct xe_sync_entry ;@@ -65,6 +67,13 @@ struct xe_vma_mem_attr {* closest device memory respectively. */ u32 devmem_fd; +/** +* @preferred_loc.dpagemap: Reference-counted pointer to the drm_pagemap preferred +* for migration on a SVM page-fault. The pointer is protected by the +* vm lock, and is %NULL if @devmem_fd should be consulted for special +* values. +*/ +struct drm_pagemap *dpagemap; } preferred_loc;/** Signature · 1 line
2.51.0