drm/amdgpu: update the functions to use amdgpu version of hmm
Subsystems (3)
PATCH v4 1/2 drm/amdgpu: update the functions to use amdgpu version of hmm
At times we need a bo reference for hmm and for that add a new struct amdgpu_hmm_range which will hold an optional bo member and hmm_range.
Use amdgpu_hmm_range instead of hmm_range and let the bo as an optional argument for the caller if they want to the bo reference to be taken or they want to handle that explicitly.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h | 2 +- .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 6 ++-- drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.h | 2 +- drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 6 ++-- drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 4 +-- drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c | 36 +++++++++++++------ drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.h | 19 ++++++---- drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 6 ++-- drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h | 7 ++-- drivers/gpu/drm/amd/amdkfd/kfd_migrate.c | 1 - drivers/gpu/drm/amd/amdkfd/kfd_migrate.h | 1 - drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 14 ++++---- drivers/gpu/drm/amd/amdkfd/kfd_svm.h | 1 - 13 files changed, 61 insertions(+), 44 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +1 -1
@@ -71,7 +71,7 @@ struct kgd_mem {struct mutex lock;struct amdgpu_bo *bo;struct dma_buf *dmabuf; -struct hmm_range *range; +struct amdgpu_hmm_range *range;struct list_head attachments;/* protected by amdkfd_process_info.lock */ struct list_head validate_list;
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +3 -3
@@ -1057,7 +1057,7 @@ static int init_user_pages(struct kgd_mem *mem, uint64_t user_addr,struct amdkfd_process_info *process_info = mem->process_info;struct amdgpu_bo *bo = mem->bo;struct ttm_operation_ctx ctx = { true, false }; -struct hmm_range *range; +struct amdgpu_hmm_range *range;int ret =0 ;mutex_lock (&process_info->lock);
@@ -1089,7 +1089,7 @@ static int init_user_pages(struct kgd_mem *mem, uint64_t user_addr,return 0 ; } - range =amdgpu_hmm_range_alloc (); + range =amdgpu_hmm_range_alloc (NULL );if (unlikely (!range)) { ret = -ENOMEM ; goto unregister_out;
@@ -2574,7 +2574,7 @@ static int update_invalid_user_pages(struct amdkfd_process_info *process_info, } } - mem->range =amdgpu_hmm_range_alloc (); + mem->range =amdgpu_hmm_range_alloc (NULL );if (unlikely (!mem->range))return -ENOMEM ;/* Get updated user pages */
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.h +1 -1
@@ -38,7 +38,7 @@ struct amdgpu_bo_list_entry {struct amdgpu_bo *bo;struct amdgpu_bo_va *bo_va;uint32_t priority; -struct hmm_range *range; +struct amdgpu_hmm_range *range;bool user_invalidated; };
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +3 -3
@@ -29,7 +29,6 @@#include <linux/pagemap.h> #include <linux/sync_file.h> #include <linux/dma-buf.h> -#include <linux/hmm.h> #include <drm/amdgpu_drm.h> #include <drm/drm_syncobj.h>
@@ -886,7 +885,7 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,bool userpage_invalidated = false;struct amdgpu_bo *bo = e->bo; - e->range =amdgpu_hmm_range_alloc (); + e->range =amdgpu_hmm_range_alloc (NULL );if (unlikely (!e->range))return -ENOMEM ;
@@ -895,7 +894,8 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p, goto out_free_user_pages;for (i =0 ; i < bo->tbo.ttm->num_pages; i++) { -if (bo->tbo.ttm->pages[i] !=hmm_pfn_to_page (e->range->hmm_pfns[i])) { +if (bo->tbo.ttm->pages[i] != +hmm_pfn_to_page (e->range->hmm_range.hmm_pfns[i])) { userpage_invalidated = true;break ; }
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +2 -2
@@ -530,7 +530,7 @@ int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data,struct drm_amdgpu_gem_userptr *args = data;struct amdgpu_fpriv *fpriv = filp->driver_priv;struct drm_gem_object *gobj; -struct hmm_range *range; +struct amdgpu_hmm_range *range;struct amdgpu_bo *bo;uint32_t handle;int r;
@@ -571,7 +571,7 @@ int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data, goto release_object;if (args->flags &AMDGPU_GEM_USERPTR_VALIDATE ) { - range =amdgpu_hmm_range_alloc (); + range =amdgpu_hmm_range_alloc (NULL );if (unlikely (!range))return -ENOMEM ; r =amdgpu_ttm_tt_get_user_pages (bo, range);
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +25 -11
@@ -168,12 +168,13 @@ void amdgpu_hmm_unregister(struct amdgpu_bo *bo)int amdgpu_hmm_range_get_pages (struct mmu_interval_notifier *notifier,uint64_t start,uint64_t npages,bool readonly,void *owner, -struct hmm_range *hmm_range) +struct amdgpu_hmm_range *range) {unsigned long end;unsigned long timeout;unsigned long *pfns;int r =0 ; +struct hmm_range *hmm_range = &range->hmm_range; pfns =kvmalloc_array (npages,sizeof (*pfns),GFP_KERNEL );if (unlikely (!pfns)) {
@@ -226,25 +227,38 @@ int amdgpu_hmm_range_get_pages(struct mmu_interval_notifier *notifier,return r; } -bool amdgpu_hmm_range_valid (struct hmm_range *hmm_range) +bool amdgpu_hmm_range_valid (struct amdgpu_hmm_range *range) { -if (!hmm_range) +if (!range)return false; -return !mmu_interval_read_retry (hmm_range->notifier, - hmm_range->notifier_seq); +return !mmu_interval_read_retry (range->hmm_range.notifier, + range->hmm_range.notifier_seq); } -struct hmm_range *amdgpu_hmm_range_alloc (void ) +struct amdgpu_hmm_range *amdgpu_hmm_range_alloc (struct amdgpu_bo *bo) { -return kzalloc (sizeof (struct hmm_range ),GFP_KERNEL ); +struct amdgpu_hmm_range *range; + + range =kzalloc (sizeof (struct amdgpu_hmm_range ),GFP_KERNEL ); +if (!range) +return NULL ; + +if (bo) + range->bo =amdgpu_bo_ref (bo); + +return range; } -void amdgpu_hmm_range_free (struct hmm_range *hmm_range) +void amdgpu_hmm_range_free (struct amdgpu_hmm_range *range) { -if (!hmm_range) +if (!range)return ; -kvfree (hmm_range->hmm_pfns); -kfree (hmm_range); +kvfree (range->hmm_range.hmm_pfns); + +if (range->bo) +amdgpu_bo_unref (&range->bo); + +kfree (range); }
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.h +12 -7
@@ -31,15 +31,20 @@#include <linux/interval_tree.h> #include <linux/mmu_notifier.h> +struct amdgpu_hmm_range { +struct hmm_range hmm_range; +struct amdgpu_bo *bo; +}; +int amdgpu_hmm_range_get_pages (struct mmu_interval_notifier *notifier,uint64_t start,uint64_t npages,bool readonly,void *owner, -struct hmm_range *hmm_range); +struct amdgpu_hmm_range *range);#if defined(CONFIG_HMM_MIRROR ) -bool amdgpu_hmm_range_valid (struct hmm_range *hmm_range); -struct hmm_range *amdgpu_hmm_range_alloc (void ); -void amdgpu_hmm_range_free (struct hmm_range *hmm_range); +bool amdgpu_hmm_range_valid (struct amdgpu_hmm_range *range); +struct amdgpu_hmm_range *amdgpu_hmm_range_alloc (struct amdgpu_bo *bo); +void amdgpu_hmm_range_free (struct amdgpu_hmm_range *range);int amdgpu_hmm_register (struct amdgpu_bo *bo,unsigned long addr);void amdgpu_hmm_unregister (struct amdgpu_bo *bo);#else
@@ -52,17 +57,17 @@ static inline int amdgpu_hmm_register(struct amdgpu_bo *bo, unsigned long addr)static inline void amdgpu_hmm_unregister (struct amdgpu_bo *bo) {} -static inline bool amdgpu_hmm_range_valid (struct hmm_range *hmm_range) +static inline bool amdgpu_hmm_range_valid (struct amdgpu_hmm_range *range) {return false; } -static inline struct hmm_range *amdgpu_hmm_range_alloc (void ) +static inline struct amdgpu_hmm_range *amdgpu_hmm_range_alloc (struct amdgpu_bo *bo) {return NULL ; } -static inline void amdgpu_hmm_range_free (struct hmm_range *hmm_range) {} +static inline void amdgpu_hmm_range_free (struct amdgpu_hmm_range *range) {}#endif #endif
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +3 -3
@@ -709,7 +709,7 @@ struct amdgpu_ttm_tt {* that range is a valid memory and it is freed too. */ int amdgpu_ttm_tt_get_user_pages (struct amdgpu_bo *bo, -struct hmm_range *range) +struct amdgpu_hmm_range *range) {struct ttm_tt *ttm = bo->tbo.ttm;struct amdgpu_ttm_tt *gtt =ttm_to_amdgpu_ttm_tt (ttm);
@@ -762,12 +762,12 @@ int amdgpu_ttm_tt_get_user_pages(struct amdgpu_bo *bo,* that backs user memory and will ultimately be mapped into the device * address space. */ -void amdgpu_ttm_tt_set_user_pages (struct ttm_tt *ttm,struct hmm_range *range) +void amdgpu_ttm_tt_set_user_pages (struct ttm_tt *ttm,struct amdgpu_hmm_range *range) {unsigned long i;for (i =0 ; i < ttm->num_pages; ++i) - ttm->pages[i] = range ?hmm_pfn_to_page (range->hmm_pfns[i]) :NULL ; + ttm->pages[i] = range ?hmm_pfn_to_page (range->hmm_range.hmm_pfns[i]) :NULL ; }/*
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +4 -3
@@ -28,6 +28,7 @@#include <drm/gpu_scheduler.h> #include <drm/ttm/ttm_placement.h> #include "amdgpu_vram_mgr.h" +#include "amdgpu_hmm.h" #define AMDGPU_PL_GDS (TTM_PL_PRIV + 0)#define AMDGPU_PL_GWS (TTM_PL_PRIV + 1)
@@ -191,16 +192,16 @@ uint64_t amdgpu_ttm_domain_start(struct amdgpu_device *adev, uint32_t type);#if IS_ENABLED (CONFIG_DRM_AMDGPU_USERPTR )int amdgpu_ttm_tt_get_user_pages (struct amdgpu_bo *bo, -struct hmm_range *range); +struct amdgpu_hmm_range *range);#else static inline int amdgpu_ttm_tt_get_user_pages (struct amdgpu_bo *bo, -struct hmm_range *range) +struct amdgpu_hmm_range *range) {return -EPERM ; }#endif -void amdgpu_ttm_tt_set_user_pages (struct ttm_tt *ttm,struct hmm_range *range); +void amdgpu_ttm_tt_set_user_pages (struct ttm_tt *ttm,struct amdgpu_hmm_range *range);int amdgpu_ttm_tt_get_userptr (const struct ttm_buffer_object *tbo,uint64_t *user_addr); int amdgpu_ttm_tt_set_userptr(struct ttm_buffer_object *bo,
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c +0 -1
@@ -21,7 +21,6 @@* OTHER DEALINGS IN THE SOFTWARE. */ #include <linux/types.h> -#include <linux/hmm.h> #include <linux/dma-direction.h> #include <linux/dma-mapping.h> #include <linux/migrate.h>
drivers/gpu/drm/amd/amdkfd/kfd_migrate.h +0 -1
@@ -31,7 +31,6 @@#include <linux/list.h> #include <linux/mutex.h> #include <linux/sched/mm.h> -#include <linux/hmm.h> #include "kfd_priv.h" #include "kfd_svm.h"
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +7 -7
@@ -1698,7 +1698,7 @@ static int svm_range_validate_and_map(struct mm_struct *mm, start = map_start <<PAGE_SHIFT ; end = (map_last +1 ) <<PAGE_SHIFT ;for (addr = start; !r && addr < end; ) { -struct hmm_range *hmm_range =NULL ; +struct amdgpu_hmm_range *range =NULL ;unsigned long map_start_vma;unsigned long map_last_vma;struct vm_area_struct *vma;
@@ -1737,13 +1737,13 @@ static int svm_range_validate_and_map(struct mm_struct *mm, }WRITE_ONCE (p->svms.faulting_task, current); - hmm_range =amdgpu_hmm_range_alloc (); + range =amdgpu_hmm_range_alloc (NULL ); r =amdgpu_hmm_range_get_pages (&prange->notifier, addr, npages, readonly, owner, - hmm_range); + range);WRITE_ONCE (p->svms.faulting_task,NULL );if (r) { -amdgpu_hmm_range_free (hmm_range); +amdgpu_hmm_range_free (range);pr_debug ("failed %d to get svm range pages\n" , r); } }else {
@@ -1753,7 +1753,7 @@ static int svm_range_validate_and_map(struct mm_struct *mm,if (!r) { offset = (addr >>PAGE_SHIFT ) - prange->start; r =svm_range_dma_map (prange, ctx->bitmap, offset, npages, - hmm_range->hmm_pfns); + range->hmm_range.hmm_pfns);if (r)pr_debug ("failed %d to dma map range\n" , r); }
@@ -1764,12 +1764,12 @@ static int svm_range_validate_and_map(struct mm_struct *mm,* Overrride return value to TRY AGAIN only if prior returns * were successful */ -if (hmm_range && !amdgpu_hmm_range_valid (hmm_range) && !r) { +if (range && !amdgpu_hmm_range_valid (range) && !r) {pr_debug ("hmm update the range, need validate again\n" ); r = -EAGAIN ; }/* Free the hmm range */ -amdgpu_hmm_range_free (hmm_range); +amdgpu_hmm_range_free (range);if (!r && !list_empty (&prange->child_list)) {
drivers/gpu/drm/amd/amdkfd/kfd_svm.h +0 -1
@@ -31,7 +31,6 @@#include <linux/list.h> #include <linux/mutex.h> #include <linux/sched/mm.h> -#include <linux/hmm.h> #include "amdgpu.h" #include "kfd_priv.h"
Signature · 1 line
2.34.1
PATCH v4 2/2 drm/amdgpu/userqueue: validate userptrs for userqueues
userptrs could be changed by the user at any time and hence while locking all the bos before GPU start processing validate all the userptr bos.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 74 +++++++++++++++++++++++ 1 file changed, 74 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +74 -0
@@ -29,6 +29,7 @@#include "amdgpu.h" #include "amdgpu_vm.h" #include "amdgpu_userq.h" +#include "amdgpu_hmm.h" #include "amdgpu_userq_fence.h" u32 amdgpu_userq_get_supported_ip_mask (struct amdgpu_device *adev)
@@ -758,12 +759,21 @@ static int amdgpu_userq_vm_validate(struct amdgpu_userq_mgr *uq_mgr) {struct amdgpu_fpriv *fpriv =uq_mgr_to_fpriv (uq_mgr); +bool invalidated = false, new_addition = false; +struct ttm_operation_ctx ctx = { true, false };struct amdgpu_device *adev = uq_mgr->adev; +struct amdgpu_hmm_range *range;struct amdgpu_vm *vm = &fpriv->vm; +unsigned long key =0 , tmp_key;struct amdgpu_bo_va *bo_va; +struct amdgpu_bo *bo;struct drm_exec exec; +struct xarray xa;int ret; +xa_init (&xa); + +retry_lock:drm_exec_init (&exec,DRM_EXEC_IGNORE_DUPLICATES ,0 ); drm_exec_until_all_locked(&exec) { ret =amdgpu_vm_lock_pd (vm, &exec,1 );
@@ -794,6 +804,63 @@ amdgpu_userq_vm_validate(struct amdgpu_userq_mgr *uq_mgr)if (ret) goto unlock_all; +if (invalidated) { +xa_for_each (&xa, tmp_key, range) { + bo = range->bo; +amdgpu_bo_placement_from_domain (bo,AMDGPU_GEM_DOMAIN_CPU ); + ret =ttm_bo_validate (&bo->tbo, &bo->placement, &ctx); +if (ret) + goto unlock_all; + +amdgpu_ttm_tt_set_user_pages (bo->tbo.ttm, range); + } + invalidated = false; + } + + key =0 ; +/* Validate User Ptr BOs */ +list_for_each_entry (bo_va, &vm->done, base.vm_status) { + bo = bo_va->base.bo; + +if (!amdgpu_ttm_tt_is_userptr (bo->tbo.ttm)) +continue ; + + range =xa_load (&xa, key); +if (range && range->bo != bo) { +xa_erase (&xa, key); +amdgpu_hmm_range_free (range); + range =NULL ; + } + +if (!range) { + range =amdgpu_hmm_range_alloc (bo); +if (!range) { + ret = -ENOMEM ; + goto unlock_all; + } + +xa_store (&xa, key, range,GFP_KERNEL ); + new_addition = true; + } + key++; + } + +if (new_addition) { +drm_exec_fini (&exec); +xa_for_each (&xa, tmp_key, range) { +if (!range) +continue ; + bo = range->bo; + ret =amdgpu_ttm_tt_get_user_pages (bo, range); +if (ret) + goto unlock_all; + } + + invalidated = true; + new_addition = false; + goto retry_lock; + } + ret =amdgpu_vm_update_pdes (adev, vm, false);if (ret) goto unlock_all;
@@ -813,6 +880,13 @@ amdgpu_userq_vm_validate(struct amdgpu_userq_mgr *uq_mgr) unlock_all:drm_exec_fini (&exec); +xa_for_each (&xa, tmp_key, range) { +if (!range) +continue ; + bo = range->bo; +amdgpu_hmm_range_free (range); + } +xa_destroy (&xa);return ret; }
Signature · 1 line
2.34.1
Christian König
maintainerRe: [Patch v4 2/2] drm/amdgpu/userqueue: validate userptrs for userqueues
Quoted 22 lines · Sunil Khatri
userptrs could be changed by the user at any time and hence while locking all the bos before GPU start processing validate all the userptr bos.
Signed-off-by Sunil Khatri <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 74 +++++++++++++++++++++++ 1 file changed, 74 insertions(+)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1
-0@@ -29,6 +29,7 @@#include "amdgpu.h" #include "amdgpu_vm.h" #include "amdgpu_userq.h" +#include "amdgpu_hmm.h" #include "amdgpu_userq_fence.h" u32 amdgpu_userq_get_supported_ip_mask (struct amdgpu_device *adev)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c static int · line 762
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1
-0@@ -758,12 +759,21 @@ static int amdgpu_userq_vm_validate(struct amdgpu_userq_mgr *uq_mgr) {struct amdgpu_fpriv *fpriv =uq_mgr_to_fpriv (uq_mgr); +bool invalidated = false, new_addition = false;
That could potentially be only a single variable, but that is up to you.
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c static int · line 768
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +3
-0+struct ttm_operation_ctx ctx = { true, false };struct amdgpu_device *adev = uq_mgr->adev; +struct amdgpu_hmm_range *range;struct amdgpu_vm *vm = &fpriv->vm; +unsigned long key =0 , tmp_key;struct amdgpu_bo_va *bo_va;
Initialising key here is superflous as far as I can see and some automated tools started to complain about that IIRC.
Quoted 18 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +7
-0+struct amdgpu_bo *bo;struct drm_exec exec; +struct xarray xa;int ret; +xa_init (&xa); + +retry_lock:drm_exec_init (&exec,DRM_EXEC_IGNORE_DUPLICATES ,0 ); drm_exec_until_all_locked(&exec) { ret =amdgpu_vm_lock_pd (vm, &exec,1 );@@ -794,6 +804,63 @@ amdgpu_userq_vm_validate(struct amdgpu_userq_mgr *uq_mgr)if (ret) goto unlock_all; +if (invalidated) { +xa_for_each (&xa, tmp_key, range) {
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_vm_validate(struct amdgpu_userq_mgr *uq_mgr) · line 819
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +10
-0+ bo = range->bo; +amdgpu_bo_placement_from_domain (bo,AMDGPU_GEM_DOMAIN_CPU ); + ret =ttm_bo_validate (&bo->tbo, &bo->placement, &ctx); +if (ret) + goto unlock_all; + +amdgpu_ttm_tt_set_user_pages (bo->tbo.ttm, range); + } + invalidated = false; + }
Please make sure that this step comes before we validated all the BOs, otherwise we won't go from CPU->GTT placement again and the GPU would crash on access.
Apart from that the logic looks good to me.
You might want to think about moving the loops into new functions in amdgpu_vm.c.
Regards,
Christian.
Quoted 62 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +45
-0+ + key =0 ; +/* Validate User Ptr BOs */ +list_for_each_entry (bo_va, &vm->done, base.vm_status) { + bo = bo_va->base.bo; + +if (!amdgpu_ttm_tt_is_userptr (bo->tbo.ttm)) +continue ; + + range =xa_load (&xa, key); +if (range && range->bo != bo) { +xa_erase (&xa, key); +amdgpu_hmm_range_free (range); + range =NULL ; + } + +if (!range) { + range =amdgpu_hmm_range_alloc (bo); +if (!range) { + ret = -ENOMEM ; + goto unlock_all; + } + +xa_store (&xa, key, range,GFP_KERNEL ); + new_addition = true; + } + key++; + } + +if (new_addition) { +drm_exec_fini (&exec); +xa_for_each (&xa, tmp_key, range) { +if (!range) +continue ; + bo = range->bo; + ret =amdgpu_ttm_tt_get_user_pages (bo, range); +if (ret) + goto unlock_all; + } + + invalidated = true; + new_addition = false; + goto retry_lock; + } + ret =amdgpu_vm_update_pdes (adev, vm, false);if (ret)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +7
-0goto unlock_all;@@ -813,6 +880,13 @@ amdgpu_userq_vm_validate(struct amdgpu_userq_mgr *uq_mgr) unlock_all:drm_exec_fini (&exec); +xa_for_each (&xa, tmp_key, range) { +if (!range) +continue ; + bo = range->bo; +amdgpu_hmm_range_free (range); + } +xa_destroy (&xa);return ret; }
Re: [Patch v4 2/2] drm/amdgpu/userqueue: validate userptrs for userqueues
Quoted 29 lines · Christian König
Quoted 22 lines · Sunil Khatri
userptrs could be changed by the user at any time and hence while locking all the bos before GPU start processing validate all the userptr bos.
Signed-off-by Sunil Khatri<[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 74 +++++++++++++++++++++++ 1 file changed, 74 insertions(+)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1
-0@@ -29,6 +29,7 @@#include "amdgpu.h" #include "amdgpu_vm.h" #include "amdgpu_userq.h" +#include "amdgpu_hmm.h" #include "amdgpu_userq_fence.h" u32 amdgpu_userq_get_supported_ip_mask (struct amdgpu_device *adev)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c static int · line 762
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1
-0@@ -758,12 +759,21 @@ static int amdgpu_userq_vm_validate(struct amdgpu_userq_mgr *uq_mgr) {struct amdgpu_fpriv *fpriv =uq_mgr_to_fpriv (uq_mgr); +bool invalidated = false, new_addition = false;That could potentially be only a single variable, but that is up to you.
Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c static int · line 768
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +3
-0+struct ttm_operation_ctx ctx = { true, false };struct amdgpu_device *adev = uq_mgr->adev; +struct amdgpu_hmm_range *range;struct amdgpu_vm *vm = &fpriv->vm; +unsigned long key =0 , tmp_key;struct amdgpu_bo_va *bo_va;Initialising key here is superflous as far as I can see and some automated tools started to complain about that IIRC.
Yeah i will double check this
Quoted 16 lines
Quoted 16 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5
-0+struct amdgpu_bo *bo;struct drm_exec exec; +struct xarray xa;int ret; +xa_init (&xa); + +retry_lock:drm_exec_init (&exec,DRM_EXEC_IGNORE_DUPLICATES ,0 ); drm_exec_until_all_locked(&exec) { ret =amdgpu_vm_lock_pd (vm, &exec,1 );@@ -794,6 +804,63 @@ amdgpu_userq_vm_validate(struct amdgpu_userq_mgr *uq_mgr)if (ret) goto unlock_all;
Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_vm_validate(struct amdgpu_userq_mgr *uq_mgr) · line 818
Quoted 12 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +12
-0+if (invalidated) { +xa_for_each (&xa, tmp_key, range) { + bo = range->bo; +amdgpu_bo_placement_from_domain (bo,AMDGPU_GEM_DOMAIN_CPU ); + ret =ttm_bo_validate (&bo->tbo, &bo->placement, &ctx); +if (ret) + goto unlock_all; + +amdgpu_ttm_tt_set_user_pages (bo->tbo.ttm, range); + } + invalidated = false; + }Please make sure that this step comes before we validated all the BOs, otherwise we won't go from
CPU->GTTplacement again and the GPU would crash on access.
Should keeping the loop after locking the pd sounds good or the first thing just after this drm_exec_init(&exec, DRM_EXEC_IGNORE_DUPLICATES, 0);
+ if (invalidated) {
+ xa_for_each(&xa, tmp_key, range) {
+ bo = range->bo;
+ amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_CPU);
+ ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
+ if (ret)
+ goto unlock_all;
+
+ amdgpu_ttm_tt_set_user_pages(bo->tbo.ttm, range);
+ }
+ invalidated = false;
+ }
Quoted 4 lines
Apart from that the logic looks good to me.
You might want to think about moving the loops into new functions in
amdgpu_vm.c.
I do have this in mind but i want to take that activity as separate to integrate it some way to both kernelq/userq but needs brainstorming as its not that simple and clear too.
Regards,
Sunil khatri
Quoted 66 lines
Regards,
Christian.Quoted 62 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +52
-0+ + key =0 ; +/* Validate User Ptr BOs */ +list_for_each_entry (bo_va, &vm->done, base.vm_status) { + bo = bo_va->base.bo; + +if (!amdgpu_ttm_tt_is_userptr (bo->tbo.ttm)) +continue ; + + range =xa_load (&xa, key); +if (range && range->bo != bo) { +xa_erase (&xa, key); +amdgpu_hmm_range_free (range); + range =NULL ; + } + +if (!range) { + range =amdgpu_hmm_range_alloc (bo); +if (!range) { + ret = -ENOMEM ; + goto unlock_all; + } + +xa_store (&xa, key, range,GFP_KERNEL ); + new_addition = true; + } + key++; + } + +if (new_addition) { +drm_exec_fini (&exec); +xa_for_each (&xa, tmp_key, range) { +if (!range) +continue ; + bo = range->bo; + ret =amdgpu_ttm_tt_get_user_pages (bo, range); +if (ret) + goto unlock_all; + } + + invalidated = true; + new_addition = false; + goto retry_lock; + } + ret =amdgpu_vm_update_pdes (adev, vm, false);if (ret) goto unlock_all;@@ -813,6 +880,13 @@ amdgpu_userq_vm_validate(struct amdgpu_userq_mgr *uq_mgr) unlock_all:drm_exec_fini (&exec); +xa_for_each (&xa, tmp_key, range) { +if (!range) +continue ; + bo = range->bo; +amdgpu_hmm_range_free (range); + } +xa_destroy (&xa);return ret; }
Christian König
maintainerQuoted 174 lines · Sunil Khatri
At times we need a bo reference for hmm and for that add a new struct
amdgpu_hmm_rangewhich will hold an optional bo member andhmm_range.Use
amdgpu_hmm_rangeinstead ofhmm_rangeand let the bo as an optional argument for the caller if they want to the bo reference to be taken or they want to handle that explicitly.Signed-off-by Sunil Khatri <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h | 2 +- .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 6 ++-- drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.h | 2 +- drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 6 ++-- drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 4 +-- drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c | 36 +++++++++++++------ drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.h | 19 ++++++---- drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 6 ++-- drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h | 7 ++-- drivers/gpu/drm/amd/amdkfd/kfd_migrate.c | 1 - drivers/gpu/drm/amd/amdkfd/kfd_migrate.h | 1 - drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 14 ++++---- drivers/gpu/drm/amd/amdkfd/kfd_svm.h | 1 - 13 files changed, 61 insertions(+), 44 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +1
-1@@ -71,7 +71,7 @@ struct kgd_mem {struct mutex lock;struct amdgpu_bo *bo;struct dma_buf *dmabuf; -struct hmm_range *range; +struct amdgpu_hmm_range *range;struct list_head attachments;/* protected by amdkfd_process_info.lock */ struct list_head validate_list;drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +3
-3@@ -1057,7 +1057,7 @@ static int init_user_pages(struct kgd_mem *mem, uint64_t user_addr,struct amdkfd_process_info *process_info = mem->process_info;struct amdgpu_bo *bo = mem->bo;struct ttm_operation_ctx ctx = { true, false }; -struct hmm_range *range; +struct amdgpu_hmm_range *range;int ret =0 ;mutex_lock (&process_info->lock);@@ -1089,7 +1089,7 @@ static int init_user_pages(struct kgd_mem *mem, uint64_t user_addr,return 0 ; } - range =amdgpu_hmm_range_alloc (); + range =amdgpu_hmm_range_alloc (NULL );if (unlikely (!range)) { ret = -ENOMEM ; goto unregister_out;@@ -2574,7 +2574,7 @@ static int update_invalid_user_pages(struct amdkfd_process_info *process_info, } } - mem->range =amdgpu_hmm_range_alloc (); + mem->range =amdgpu_hmm_range_alloc (NULL );if (unlikely (!mem->range))return -ENOMEM ;/* Get updated user pages */ drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.h +1
-1@@ -38,7 +38,7 @@ struct amdgpu_bo_list_entry {struct amdgpu_bo *bo;struct amdgpu_bo_va *bo_va;uint32_t priority; -struct hmm_range *range; +struct amdgpu_hmm_range *range;bool user_invalidated; };drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +3
-3@@ -29,7 +29,6 @@#include <linux/pagemap.h> #include <linux/sync_file.h> #include <linux/dma-buf.h> -#include <linux/hmm.h> #include <drm/amdgpu_drm.h> #include <drm/drm_syncobj.h> @@ -886,7 +885,7 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,bool userpage_invalidated = false;struct amdgpu_bo *bo = e->bo; - e->range =amdgpu_hmm_range_alloc (); + e->range =amdgpu_hmm_range_alloc (NULL );if (unlikely (!e->range))return -ENOMEM ;@@ -895,7 +894,8 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p, goto out_free_user_pages;for (i =0 ; i < bo->tbo.ttm->num_pages; i++) { -if (bo->tbo.ttm->pages[i] !=hmm_pfn_to_page (e->range->hmm_pfns[i])) { +if (bo->tbo.ttm->pages[i] != +hmm_pfn_to_page (e->range->hmm_range.hmm_pfns[i])) { userpage_invalidated = true;break ; }drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +2
-2@@ -530,7 +530,7 @@ int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data,struct drm_amdgpu_gem_userptr *args = data;struct amdgpu_fpriv *fpriv = filp->driver_priv;struct drm_gem_object *gobj; -struct hmm_range *range; +struct amdgpu_hmm_range *range;struct amdgpu_bo *bo;uint32_t handle;int r;@@ -571,7 +571,7 @@ int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data, goto release_object;if (args->flags &AMDGPU_GEM_USERPTR_VALIDATE ) { - range =amdgpu_hmm_range_alloc (); + range =amdgpu_hmm_range_alloc (NULL );if (unlikely (!range))return -ENOMEM ; r =amdgpu_ttm_tt_get_user_pages (bo, range);drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +7
-6@@ -168,12 +168,13 @@ void amdgpu_hmm_unregister(struct amdgpu_bo *bo)int amdgpu_hmm_range_get_pages (struct mmu_interval_notifier *notifier,uint64_t start,uint64_t npages,bool readonly,void *owner, -struct hmm_range *hmm_range) +struct amdgpu_hmm_range *range) {unsigned long end;unsigned long timeout;unsigned long *pfns;int r =0 ; +struct hmm_range *hmm_range = &range->hmm_range; pfns =kvmalloc_array (npages,sizeof (*pfns),GFP_KERNEL );if (unlikely (!pfns)) {@@ -226,25 +227,38 @@ int amdgpu_hmm_range_get_pages(struct mmu_interval_notifier *notifier,return r; } -bool amdgpu_hmm_range_valid (struct hmm_range *hmm_range) +bool amdgpu_hmm_range_valid (struct amdgpu_hmm_range *range) { -if (!hmm_range) +if (!range)return false; -return !mmu_interval_read_retry (hmm_range->notifier, - hmm_range->notifier_seq); +return !mmu_interval_read_retry (range->hmm_range.notifier, + range->hmm_range.notifier_seq); } -struct hmm_range *amdgpu_hmm_range_alloc (void ) +struct amdgpu_hmm_range *amdgpu_hmm_range_alloc (struct amdgpu_bo *bo)
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c int amdgpu_hmm_range_get_pages(struct mmu_interval_notifier *notifier, · line 248
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +8
-1{ -return kzalloc (sizeof (struct hmm_range ),GFP_KERNEL ); +struct amdgpu_hmm_range *range; + + range =kzalloc (sizeof (struct amdgpu_hmm_range ),GFP_KERNEL ); +if (!range) +return NULL ; + +if (bo) + range->bo =amdgpu_bo_ref (bo);
The functions amdgpu_bo_ref() and ...
Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +3
-1+ +return range; } -void amdgpu_hmm_range_free (struct hmm_range *hmm_range) +void amdgpu_hmm_range_free (struct amdgpu_hmm_range *range) {
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c int amdgpu_hmm_range_get_pages(struct mmu_interval_notifier *notifier, · line 261
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +5
-3-if (!hmm_range) +if (!range)return ; -kvfree (hmm_range->hmm_pfns); -kfree (hmm_range); +kvfree (range->hmm_range.hmm_pfns); + +if (range->bo) +amdgpu_bo_unref (&range->bo);
... amdgpu_bo_unref() have intergrated NULL checks. So checking it here is superflous.
It would also be helpful to have some kerneldoc on amdgpu_hmm_range_alloc() and amdgpu_hmm_range_free(), but that is not a must have.
With that fixed the patch is Reviewed-by: Christian König <[email protected]>
Regards,
Christian.
Quoted 203 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +2
-0+ +kfree (range); }drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.h +12
-7@@ -31,15 +31,20 @@#include <linux/interval_tree.h> #include <linux/mmu_notifier.h> +struct amdgpu_hmm_range { +struct hmm_range hmm_range; +struct amdgpu_bo *bo; +}; +int amdgpu_hmm_range_get_pages (struct mmu_interval_notifier *notifier,uint64_t start,uint64_t npages,bool readonly,void *owner, -struct hmm_range *hmm_range); +struct amdgpu_hmm_range *range);#if defined(CONFIG_HMM_MIRROR ) -bool amdgpu_hmm_range_valid (struct hmm_range *hmm_range); -struct hmm_range *amdgpu_hmm_range_alloc (void ); -void amdgpu_hmm_range_free (struct hmm_range *hmm_range); +bool amdgpu_hmm_range_valid (struct amdgpu_hmm_range *range); +struct amdgpu_hmm_range *amdgpu_hmm_range_alloc (struct amdgpu_bo *bo); +void amdgpu_hmm_range_free (struct amdgpu_hmm_range *range);int amdgpu_hmm_register (struct amdgpu_bo *bo,unsigned long addr);void amdgpu_hmm_unregister (struct amdgpu_bo *bo);#else @@ -52,17 +57,17 @@ static inline int amdgpu_hmm_register(struct amdgpu_bo *bo, unsigned long addr)static inline void amdgpu_hmm_unregister (struct amdgpu_bo *bo) {} -static inline bool amdgpu_hmm_range_valid (struct hmm_range *hmm_range) +static inline bool amdgpu_hmm_range_valid (struct amdgpu_hmm_range *range) {return false; } -static inline struct hmm_range *amdgpu_hmm_range_alloc (void ) +static inline struct amdgpu_hmm_range *amdgpu_hmm_range_alloc (struct amdgpu_bo *bo) {return NULL ; } -static inline void amdgpu_hmm_range_free (struct hmm_range *hmm_range) {} +static inline void amdgpu_hmm_range_free (struct amdgpu_hmm_range *range) {}#endif #endif drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +3
-3@@ -709,7 +709,7 @@ struct amdgpu_ttm_tt {* that range is a valid memory and it is freed too. */ int amdgpu_ttm_tt_get_user_pages (struct amdgpu_bo *bo, -struct hmm_range *range) +struct amdgpu_hmm_range *range) {struct ttm_tt *ttm = bo->tbo.ttm;struct amdgpu_ttm_tt *gtt =ttm_to_amdgpu_ttm_tt (ttm);@@ -762,12 +762,12 @@ int amdgpu_ttm_tt_get_user_pages(struct amdgpu_bo *bo,* that backs user memory and will ultimately be mapped into the device * address space. */ -void amdgpu_ttm_tt_set_user_pages (struct ttm_tt *ttm,struct hmm_range *range) +void amdgpu_ttm_tt_set_user_pages (struct ttm_tt *ttm,struct amdgpu_hmm_range *range) {unsigned long i;for (i =0 ; i < ttm->num_pages; ++i) - ttm->pages[i] = range ?hmm_pfn_to_page (range->hmm_pfns[i]) :NULL ; + ttm->pages[i] = range ?hmm_pfn_to_page (range->hmm_range.hmm_pfns[i]) :NULL ; }/* drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +4
-3@@ -28,6 +28,7 @@#include <drm/gpu_scheduler.h> #include <drm/ttm/ttm_placement.h> #include "amdgpu_vram_mgr.h" +#include "amdgpu_hmm.h" #define AMDGPU_PL_GDS (TTM_PL_PRIV + 0)#define AMDGPU_PL_GWS (TTM_PL_PRIV + 1)@@ -191,16 +192,16 @@ uint64_t amdgpu_ttm_domain_start(struct amdgpu_device *adev, uint32_t type);#if IS_ENABLED (CONFIG_DRM_AMDGPU_USERPTR )int amdgpu_ttm_tt_get_user_pages (struct amdgpu_bo *bo, -struct hmm_range *range); +struct amdgpu_hmm_range *range);#else static inline int amdgpu_ttm_tt_get_user_pages (struct amdgpu_bo *bo, -struct hmm_range *range) +struct amdgpu_hmm_range *range) {return -EPERM ; }#endif -void amdgpu_ttm_tt_set_user_pages (struct ttm_tt *ttm,struct hmm_range *range); +void amdgpu_ttm_tt_set_user_pages (struct ttm_tt *ttm,struct amdgpu_hmm_range *range);int amdgpu_ttm_tt_get_userptr (const struct ttm_buffer_object *tbo,uint64_t *user_addr); int amdgpu_ttm_tt_set_userptr(struct ttm_buffer_object *bo,drivers/gpu/drm/amd/amdkfd/kfd_migrate.c +0
-1@@ -21,7 +21,6 @@* OTHER DEALINGS IN THE SOFTWARE. */ #include <linux/types.h> -#include <linux/hmm.h> #include <linux/dma-direction.h> #include <linux/dma-mapping.h> #include <linux/migrate.h> drivers/gpu/drm/amd/amdkfd/kfd_migrate.h +0
-1@@ -31,7 +31,6 @@#include <linux/list.h> #include <linux/mutex.h> #include <linux/sched/mm.h> -#include <linux/hmm.h> #include "kfd_priv.h" #include "kfd_svm.h" drivers/gpu/drm/amd/amdkfd/kfd_svm.c +7
-7@@ -1698,7 +1698,7 @@ static int svm_range_validate_and_map(struct mm_struct *mm, start = map_start <<PAGE_SHIFT ; end = (map_last +1 ) <<PAGE_SHIFT ;for (addr = start; !r && addr < end; ) { -struct hmm_range *hmm_range =NULL ; +struct amdgpu_hmm_range *range =NULL ;unsigned long map_start_vma;unsigned long map_last_vma;struct vm_area_struct *vma;@@ -1737,13 +1737,13 @@ static int svm_range_validate_and_map(struct mm_struct *mm, }WRITE_ONCE (p->svms.faulting_task, current); - hmm_range =amdgpu_hmm_range_alloc (); + range =amdgpu_hmm_range_alloc (NULL ); r =amdgpu_hmm_range_get_pages (&prange->notifier, addr, npages, readonly, owner, - hmm_range); + range);WRITE_ONCE (p->svms.faulting_task,NULL );if (r) { -amdgpu_hmm_range_free (hmm_range); +amdgpu_hmm_range_free (range);pr_debug ("failed %d to get svm range pages\n" , r); } }else {@@ -1753,7 +1753,7 @@ static int svm_range_validate_and_map(struct mm_struct *mm,if (!r) { offset = (addr >>PAGE_SHIFT ) - prange->start; r =svm_range_dma_map (prange, ctx->bitmap, offset, npages, - hmm_range->hmm_pfns); + range->hmm_range.hmm_pfns);if (r)pr_debug ("failed %d to dma map range\n" , r); }@@ -1764,12 +1764,12 @@ static int svm_range_validate_and_map(struct mm_struct *mm,* Overrride return value to TRY AGAIN only if prior returns * were successful */ -if (hmm_range && !amdgpu_hmm_range_valid (hmm_range) && !r) { +if (range && !amdgpu_hmm_range_valid (range) && !r) {pr_debug ("hmm update the range, need validate again\n" ); r = -EAGAIN ; }/* Free the hmm range */ -amdgpu_hmm_range_free (hmm_range); +amdgpu_hmm_range_free (range);if (!r && !list_empty (&prange->child_list)) {drivers/gpu/drm/amd/amdkfd/kfd_svm.h +0
-1@@ -31,7 +31,6 @@#include <linux/list.h> #include <linux/mutex.h> #include <linux/sched/mm.h> -#include <linux/hmm.h> #include "amdgpu.h" #include "kfd_priv.h"
Quoted 198 lines · Christian König
Quoted 174 lines · Sunil Khatri
At times we need a bo reference for hmm and for that add a new struct
amdgpu_hmm_rangewhich will hold an optional bo member andhmm_range.Use
amdgpu_hmm_rangeinstead ofhmm_rangeand let the bo as an optional argument for the caller if they want to the bo reference to be taken or they want to handle that explicitly.Signed-off-by Sunil Khatri <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h | 2 +- .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 6 ++-- drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.h | 2 +- drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 6 ++-- drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 4 +-- drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c | 36 +++++++++++++------ drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.h | 19 ++++++---- drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 6 ++-- drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h | 7 ++-- drivers/gpu/drm/amd/amdkfd/kfd_migrate.c | 1 - drivers/gpu/drm/amd/amdkfd/kfd_migrate.h | 1 - drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 14 ++++---- drivers/gpu/drm/amd/amdkfd/kfd_svm.h | 1 - 13 files changed, 61 insertions(+), 44 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +1
-1@@ -71,7 +71,7 @@ struct kgd_mem {struct mutex lock;struct amdgpu_bo *bo;struct dma_buf *dmabuf; -struct hmm_range *range; +struct amdgpu_hmm_range *range;struct list_head attachments;/* protected by amdkfd_process_info.lock */ struct list_head validate_list;drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +3
-3@@ -1057,7 +1057,7 @@ static int init_user_pages(struct kgd_mem *mem, uint64_t user_addr,struct amdkfd_process_info *process_info = mem->process_info;struct amdgpu_bo *bo = mem->bo;struct ttm_operation_ctx ctx = { true, false }; -struct hmm_range *range; +struct amdgpu_hmm_range *range;int ret =0 ;mutex_lock (&process_info->lock);@@ -1089,7 +1089,7 @@ static int init_user_pages(struct kgd_mem *mem, uint64_t user_addr,return 0 ; } - range =amdgpu_hmm_range_alloc (); + range =amdgpu_hmm_range_alloc (NULL );if (unlikely (!range)) { ret = -ENOMEM ; goto unregister_out;@@ -2574,7 +2574,7 @@ static int update_invalid_user_pages(struct amdkfd_process_info *process_info, } } - mem->range =amdgpu_hmm_range_alloc (); + mem->range =amdgpu_hmm_range_alloc (NULL );if (unlikely (!mem->range))return -ENOMEM ;/* Get updated user pages */ drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.h +1
-1@@ -38,7 +38,7 @@ struct amdgpu_bo_list_entry {struct amdgpu_bo *bo;struct amdgpu_bo_va *bo_va;uint32_t priority; -struct hmm_range *range; +struct amdgpu_hmm_range *range;bool user_invalidated; };drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +3
-3@@ -29,7 +29,6 @@#include <linux/pagemap.h> #include <linux/sync_file.h> #include <linux/dma-buf.h> -#include <linux/hmm.h> #include <drm/amdgpu_drm.h> #include <drm/drm_syncobj.h> @@ -886,7 +885,7 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,bool userpage_invalidated = false;struct amdgpu_bo *bo = e->bo; - e->range =amdgpu_hmm_range_alloc (); + e->range =amdgpu_hmm_range_alloc (NULL );if (unlikely (!e->range))return -ENOMEM ;@@ -895,7 +894,8 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p, goto out_free_user_pages;for (i =0 ; i < bo->tbo.ttm->num_pages; i++) { -if (bo->tbo.ttm->pages[i] !=hmm_pfn_to_page (e->range->hmm_pfns[i])) { +if (bo->tbo.ttm->pages[i] != +hmm_pfn_to_page (e->range->hmm_range.hmm_pfns[i])) { userpage_invalidated = true;break ; }drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +2
-2@@ -530,7 +530,7 @@ int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data,struct drm_amdgpu_gem_userptr *args = data;struct amdgpu_fpriv *fpriv = filp->driver_priv;struct drm_gem_object *gobj; -struct hmm_range *range; +struct amdgpu_hmm_range *range;struct amdgpu_bo *bo;uint32_t handle;int r;@@ -571,7 +571,7 @@ int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data, goto release_object;if (args->flags &AMDGPU_GEM_USERPTR_VALIDATE ) { - range =amdgpu_hmm_range_alloc (); + range =amdgpu_hmm_range_alloc (NULL );if (unlikely (!range))return -ENOMEM ; r =amdgpu_ttm_tt_get_user_pages (bo, range);drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +7
-6@@ -168,12 +168,13 @@ void amdgpu_hmm_unregister(struct amdgpu_bo *bo)int amdgpu_hmm_range_get_pages (struct mmu_interval_notifier *notifier,uint64_t start,uint64_t npages,bool readonly,void *owner, -struct hmm_range *hmm_range) +struct amdgpu_hmm_range *range) {unsigned long end;unsigned long timeout;unsigned long *pfns;int r =0 ; +struct hmm_range *hmm_range = &range->hmm_range; pfns =kvmalloc_array (npages,sizeof (*pfns),GFP_KERNEL );if (unlikely (!pfns)) {@@ -226,25 +227,38 @@ int amdgpu_hmm_range_get_pages(struct mmu_interval_notifier *notifier,return r; } -bool amdgpu_hmm_range_valid (struct hmm_range *hmm_range) +bool amdgpu_hmm_range_valid (struct amdgpu_hmm_range *range) { -if (!hmm_range) +if (!range)return false; -return !mmu_interval_read_retry (hmm_range->notifier, - hmm_range->notifier_seq); +return !mmu_interval_read_retry (range->hmm_range.notifier, + range->hmm_range.notifier_seq); } -struct hmm_range *amdgpu_hmm_range_alloc (void ) +struct amdgpu_hmm_range *amdgpu_hmm_range_alloc (struct amdgpu_bo *bo)drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c int amdgpu_hmm_range_get_pages(struct mmu_interval_notifier *notifier, · line 248
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +8
-1{ -return kzalloc (sizeof (struct hmm_range ),GFP_KERNEL ); +struct amdgpu_hmm_range *range; + + range =kzalloc (sizeof (struct amdgpu_hmm_range ),GFP_KERNEL ); +if (!range) +return NULL ; + +if (bo) + range->bo =amdgpu_bo_ref (bo);The functions
amdgpu_bo_ref()and ...Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +4
-2+ +return range; } -void amdgpu_hmm_range_free (struct hmm_range *hmm_range) +void amdgpu_hmm_range_free (struct amdgpu_hmm_range *range) { -if (!hmm_range) +if (!range)return ;
Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c int amdgpu_hmm_range_get_pages(struct mmu_interval_notifier *notifier, · line 261
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +4
-2-kvfree (hmm_range->hmm_pfns); -kfree (hmm_range); +kvfree (range->hmm_range.hmm_pfns); + +if (range->bo) +amdgpu_bo_unref (&range->bo);...
amdgpu_bo_unref()have intergrated NULL checks. So checking it here is superflous.
Sure will do that.
Quoted 2 lines
It would also be helpful to have some kerneldoc on
amdgpu_hmm_range_alloc()andamdgpu_hmm_range_free(), but that is not a must have.
I guess that will be in a new patch set. will see how it is done and if i can use and existing one to update that.
Regards
Sunil Khatri
Quoted 209 lines
With that fixed the patch is Reviewed-by: Christian König <[email protected]>
Regards,
Christian.Quoted 203 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +2
-0+ +kfree (range); }drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.h +12
-7@@ -31,15 +31,20 @@#include <linux/interval_tree.h> #include <linux/mmu_notifier.h> +struct amdgpu_hmm_range { +struct hmm_range hmm_range; +struct amdgpu_bo *bo; +}; +int amdgpu_hmm_range_get_pages (struct mmu_interval_notifier *notifier,uint64_t start,uint64_t npages,bool readonly,void *owner, -struct hmm_range *hmm_range); +struct amdgpu_hmm_range *range);#if defined(CONFIG_HMM_MIRROR ) -bool amdgpu_hmm_range_valid (struct hmm_range *hmm_range); -struct hmm_range *amdgpu_hmm_range_alloc (void ); -void amdgpu_hmm_range_free (struct hmm_range *hmm_range); +bool amdgpu_hmm_range_valid (struct amdgpu_hmm_range *range); +struct amdgpu_hmm_range *amdgpu_hmm_range_alloc (struct amdgpu_bo *bo); +void amdgpu_hmm_range_free (struct amdgpu_hmm_range *range);int amdgpu_hmm_register (struct amdgpu_bo *bo,unsigned long addr);void amdgpu_hmm_unregister (struct amdgpu_bo *bo);#else @@ -52,17 +57,17 @@ static inline int amdgpu_hmm_register(struct amdgpu_bo *bo, unsigned long addr)static inline void amdgpu_hmm_unregister (struct amdgpu_bo *bo) {} -static inline bool amdgpu_hmm_range_valid (struct hmm_range *hmm_range) +static inline bool amdgpu_hmm_range_valid (struct amdgpu_hmm_range *range) {return false; } -static inline struct hmm_range *amdgpu_hmm_range_alloc (void ) +static inline struct amdgpu_hmm_range *amdgpu_hmm_range_alloc (struct amdgpu_bo *bo) {return NULL ; } -static inline void amdgpu_hmm_range_free (struct hmm_range *hmm_range) {} +static inline void amdgpu_hmm_range_free (struct amdgpu_hmm_range *range) {}#endif #endif drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +3
-3@@ -709,7 +709,7 @@ struct amdgpu_ttm_tt {* that range is a valid memory and it is freed too. */ int amdgpu_ttm_tt_get_user_pages (struct amdgpu_bo *bo, -struct hmm_range *range) +struct amdgpu_hmm_range *range) {struct ttm_tt *ttm = bo->tbo.ttm;struct amdgpu_ttm_tt *gtt =ttm_to_amdgpu_ttm_tt (ttm);@@ -762,12 +762,12 @@ int amdgpu_ttm_tt_get_user_pages(struct amdgpu_bo *bo,* that backs user memory and will ultimately be mapped into the device * address space. */ -void amdgpu_ttm_tt_set_user_pages (struct ttm_tt *ttm,struct hmm_range *range) +void amdgpu_ttm_tt_set_user_pages (struct ttm_tt *ttm,struct amdgpu_hmm_range *range) {unsigned long i;for (i =0 ; i < ttm->num_pages; ++i) - ttm->pages[i] = range ?hmm_pfn_to_page (range->hmm_pfns[i]) :NULL ; + ttm->pages[i] = range ?hmm_pfn_to_page (range->hmm_range.hmm_pfns[i]) :NULL ; }/* drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +4
-3@@ -28,6 +28,7 @@#include <drm/gpu_scheduler.h> #include <drm/ttm/ttm_placement.h> #include "amdgpu_vram_mgr.h" +#include "amdgpu_hmm.h" #define AMDGPU_PL_GDS (TTM_PL_PRIV + 0)#define AMDGPU_PL_GWS (TTM_PL_PRIV + 1)@@ -191,16 +192,16 @@ uint64_t amdgpu_ttm_domain_start(struct amdgpu_device *adev, uint32_t type);#if IS_ENABLED (CONFIG_DRM_AMDGPU_USERPTR )int amdgpu_ttm_tt_get_user_pages (struct amdgpu_bo *bo, -struct hmm_range *range); +struct amdgpu_hmm_range *range);#else static inline int amdgpu_ttm_tt_get_user_pages (struct amdgpu_bo *bo, -struct hmm_range *range) +struct amdgpu_hmm_range *range) {return -EPERM ; }#endif -void amdgpu_ttm_tt_set_user_pages (struct ttm_tt *ttm,struct hmm_range *range); +void amdgpu_ttm_tt_set_user_pages (struct ttm_tt *ttm,struct amdgpu_hmm_range *range);int amdgpu_ttm_tt_get_userptr (const struct ttm_buffer_object *tbo,uint64_t *user_addr); int amdgpu_ttm_tt_set_userptr(struct ttm_buffer_object *bo,drivers/gpu/drm/amd/amdkfd/kfd_migrate.c +0
-1@@ -21,7 +21,6 @@* OTHER DEALINGS IN THE SOFTWARE. */ #include <linux/types.h> -#include <linux/hmm.h> #include <linux/dma-direction.h> #include <linux/dma-mapping.h> #include <linux/migrate.h> drivers/gpu/drm/amd/amdkfd/kfd_migrate.h +0
-1@@ -31,7 +31,6 @@#include <linux/list.h> #include <linux/mutex.h> #include <linux/sched/mm.h> -#include <linux/hmm.h> #include "kfd_priv.h" #include "kfd_svm.h" drivers/gpu/drm/amd/amdkfd/kfd_svm.c +7
-7@@ -1698,7 +1698,7 @@ static int svm_range_validate_and_map(struct mm_struct *mm, start = map_start <<PAGE_SHIFT ; end = (map_last +1 ) <<PAGE_SHIFT ;for (addr = start; !r && addr < end; ) { -struct hmm_range *hmm_range =NULL ; +struct amdgpu_hmm_range *range =NULL ;unsigned long map_start_vma;unsigned long map_last_vma;struct vm_area_struct *vma;@@ -1737,13 +1737,13 @@ static int svm_range_validate_and_map(struct mm_struct *mm, }WRITE_ONCE (p->svms.faulting_task, current); - hmm_range =amdgpu_hmm_range_alloc (); + range =amdgpu_hmm_range_alloc (NULL ); r =amdgpu_hmm_range_get_pages (&prange->notifier, addr, npages, readonly, owner, - hmm_range); + range);WRITE_ONCE (p->svms.faulting_task,NULL );if (r) { -amdgpu_hmm_range_free (hmm_range); +amdgpu_hmm_range_free (range);pr_debug ("failed %d to get svm range pages\n" , r); } }else {@@ -1753,7 +1753,7 @@ static int svm_range_validate_and_map(struct mm_struct *mm,if (!r) { offset = (addr >>PAGE_SHIFT ) - prange->start; r =svm_range_dma_map (prange, ctx->bitmap, offset, npages, - hmm_range->hmm_pfns); + range->hmm_range.hmm_pfns);if (r)pr_debug ("failed %d to dma map range\n" , r); }@@ -1764,12 +1764,12 @@ static int svm_range_validate_and_map(struct mm_struct *mm,* Overrride return value to TRY AGAIN only if prior returns * were successful */ -if (hmm_range && !amdgpu_hmm_range_valid (hmm_range) && !r) { +if (range && !amdgpu_hmm_range_valid (range) && !r) {pr_debug ("hmm update the range, need validate again\n" ); r = -EAGAIN ; }/* Free the hmm range */ -amdgpu_hmm_range_free (hmm_range); +amdgpu_hmm_range_free (range);if (!r && !list_empty (&prange->child_list)) {drivers/gpu/drm/amd/amdkfd/kfd_svm.h +0
-1@@ -31,7 +31,6 @@#include <linux/list.h> #include <linux/mutex.h> #include <linux/sched/mm.h> -#include <linux/hmm.h> #include "amdgpu.h" #include "kfd_priv.h"
Christian König
maintainerQuoted 192 lines · Sunil
Quoted 174 lines · Christian König
Quoted 161 lines · Sunil Khatri
At times we need a bo reference for hmm and for that add a new struct
amdgpu_hmm_rangewhich will hold an optional bo member andhmm_range.Use
amdgpu_hmm_rangeinstead ofhmm_rangeand let the bo as an optional argument for the caller if they want to the bo reference to be taken or they want to handle that explicitly.Signed-off-by Sunil Khatri <[email protected]>---
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h | 2 +- .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 6 ++-- drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.h | 2 +- drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 6 ++--drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 4 +-- drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c | 36 +++++++++++++------ drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.h | 19 ++++++---- drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 6 ++-- drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h | 7 ++--drivers/gpu/drm/amd/amdkfd/kfd_migrate.c | 1 - drivers/gpu/drm/amd/amdkfd/kfd_migrate.h | 1 - drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 14 ++++---- drivers/gpu/drm/amd/amdkfd/kfd_svm.h | 1 -
13 files changed, 61 insertions(+), 44 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +1
-1@@ -71,7 +71,7 @@ struct kgd_mem { struct mutex lock; struct amdgpu_bo *bo; struct dma_buf *dmabuf; - struct hmm_range *range; + struct amdgpu_hmm_range *range; struct list_head attachments; /* protected by amdkfd_process_info.lock */ struct list_head validate_list;drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +3
-1@@ -1057,7 +1057,7 @@ static int init_user_pages(struct kgd_mem *mem, uint64_t user_addr, struct amdkfd_process_info *process_info = mem->process_info; struct amdgpu_bo *bo = mem->bo; struct ttm_operation_ctx ctx = { true, false }; - struct hmm_range *range; + struct amdgpu_hmm_range *range; int ret = 0; mutex_lock(&process_info->lock);@@ -1089,7 +1089,7 @@ static int init_user_pages(struct kgd_mem *mem, uint64_t user_addr, return 0; } - range = amdgpu_hmm_range_alloc(); + range = amdgpu_hmm_range_alloc(NULL); if (unlikely(!range)) { ret = -ENOMEM; goto unregister_out;@@ -2574,7 +2574,7 @@ static int update_invalid_user_pages(struct amdkfd_process_info *process_info, } } - mem->range = amdgpu_hmm_range_alloc(); + mem->range = amdgpu_hmm_range_alloc(NULL); if (unlikely(!mem->range)) return -ENOMEM; /* Get updated user pages */drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.h +1
-1@@ -38,7 +38,7 @@ struct amdgpu_bo_list_entry { struct amdgpu_bo *bo; struct amdgpu_bo_va *bo_va; uint32_t priority; - struct hmm_range *range; + struct amdgpu_hmm_range *range; bool user_invalidated; };diff --git
a/drivers/gpu/drm/amd/amdgpu/amdgpu_cs.cb/drivers/gpu/drm/amd/amdgpu/amdgpu_cs.cindex087e5b5497e4..87872c0282e4100644drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +1
-1@@ -29,7 +29,6 @@ #include <linux/pagemap.h> #include <linux/sync_file.h> #include <linux/dma-buf.h> -#include <linux/hmm.h> #include <drm/amdgpu_drm.h> #include <drm/drm_syncobj.h>@@ -886,7 +885,7 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p, bool userpage_invalidated = false; struct amdgpu_bo *bo = e->bo; - e->range = amdgpu_hmm_range_alloc(); + e->range = amdgpu_hmm_range_alloc(NULL); if (unlikely(!e->range)) return -ENOMEM; @@ -895,7 +894,8 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,goto
out_free_user_pages;
for (i = 0; i <bo->tbo.ttm->num_pages; i++) {
- if (bo->tbo.ttm->pages[i] !=hmm_pfn_to_page(e->range->hmm_pfns[i])) { + if (bo->tbo.ttm->pages[i] !=
+hmm_pfn_to_page(e->range->hmm_range.hmm_pfns[i])) {userpage_invalidated= true;
break;
}drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +2
-2@@ -530,7 +530,7 @@ int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data, struct drm_amdgpu_gem_userptr *args = data; struct amdgpu_fpriv *fpriv = filp->driver_priv; struct drm_gem_object *gobj; - struct hmm_range *range; + struct amdgpu_hmm_range *range; struct amdgpu_bo *bo; uint32_t handle; int r;@@ -571,7 +571,7 @@ int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data, goto release_object; if (args->flags & AMDGPU_GEM_USERPTR_VALIDATE) { - range = amdgpu_hmm_range_alloc(); + range = amdgpu_hmm_range_alloc(NULL); if (unlikely(!range)) return -ENOMEM; r = amdgpu_ttm_tt_get_user_pages(bo, range);drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +7
-3@@ -168,12 +168,13 @@ void amdgpu_hmm_unregister(struct amdgpu_bo *bo) int amdgpu_hmm_range_get_pages(struct mmu_interval_notifier *notifier, uint64_t start, uint64_t npages, bool readonly, void *owner, - struct hmm_range *hmm_range) + struct amdgpu_hmm_range *range) { unsigned long end; unsigned long timeout; unsigned long *pfns; int r = 0; + struct hmm_range *hmm_range = &range->hmm_range; pfns = kvmalloc_array(npages, sizeof(*pfns), GFP_KERNEL); if (unlikely(!pfns)) {@@ -226,25 +227,38 @@ int amdgpu_hmm_range_get_pages(struct mmu_interval_notifier *notifier, return r; } -bool amdgpu_hmm_range_valid(struct hmm_range *hmm_range) +bool amdgpu_hmm_range_valid(struct amdgpu_hmm_range *range) { - if (!hmm_range) + if (!range) return false; - return !mmu_interval_read_retry(hmm_range->notifier, - hmm_range->notifier_seq); + return !mmu_interval_read_retry(range->hmm_range.notifier, + range->hmm_range.notifier_seq); } -struct hmm_range *amdgpu_hmm_range_alloc(void) +struct amdgpu_hmm_range *amdgpu_hmm_range_alloc(struct amdgpu_bo *bo)drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c int amdgpu_hmm_range_get_pages(struct mmu_interval_notifier *notifier, · line 248
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +8
-1{ - return kzalloc(sizeof(struct hmm_range), GFP_KERNEL); + struct amdgpu_hmm_range *range; + + range = kzalloc(sizeof(struct amdgpu_hmm_range), GFP_KERNEL); + if (!range) + return NULL; + + if (bo) + range->bo = amdgpu_bo_ref(bo);The functions
amdgpu_bo_ref()and ...Quoted 16 lines
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +3
-0+ + return range; } -void amdgpu_hmm_range_free(struct hmm_range *hmm_range) +void amdgpu_hmm_range_free(struct amdgpu_hmm_range *range)drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c int amdgpu_hmm_range_get_pages(struct mmu_interval_notifier *notifier, · line 261
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +5
-2{ - if (!hmm_range) + if (!range) return; - kvfree(hmm_range->hmm_pfns); - kfree(hmm_range); + kvfree(range->hmm_range.hmm_pfns); + + if (range->bo) + amdgpu_bo_unref(&range->bo);...
amdgpu_bo_unref()have intergrated NULL checks. So checking it here is superflous.
Quoted 5 lines
Sure will do that.
Quoted 2 lines
It would also be helpful to have some kerneldoc on
amdgpu_hmm_range_alloc()andamdgpu_hmm_range_free(), but that is not a must have.I guess that will be in a new patch set. will see how it is done and if i can use and existing one to update that.
Works for me. A lot of people also prefer to add kerneldoc as separate patch at the end of their patchset, so that you can see first how all puzzle pieces fall together before you add documentattion about it.
Regards,
Christian.
Quoted 197 lines
Regards
Sunil KhatriQuoted 193 lines
With that fixed the patch is Reviewed-by: Christian König <[email protected]>
Regards,
Christian.Quoted 187 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c +2
-0+ + kfree(range); }drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.h +11
-4@@ -31,15 +31,20 @@ #include <linux/interval_tree.h> #include <linux/mmu_notifier.h> +struct amdgpu_hmm_range { + struct hmm_range hmm_range; + struct amdgpu_bo *bo; +}; + int amdgpu_hmm_range_get_pages(struct mmu_interval_notifier *notifier, uint64_t start, uint64_t npages, bool readonly, void *owner, - struct hmm_range *hmm_range); + struct amdgpu_hmm_range *range); #if defined(CONFIG_HMM_MIRROR) -bool amdgpu_hmm_range_valid (struct hmm_range *hmm_range); -struct hmm_range *amdgpu_hmm_range_alloc (void ); -void amdgpu_hmm_range_free (struct hmm_range *hmm_range); +bool amdgpu_hmm_range_valid (struct amdgpu_hmm_range *range); +struct amdgpu_hmm_range *amdgpu_hmm_range_alloc (struct amdgpu_bo *bo); +void amdgpu_hmm_range_free (struct amdgpu_hmm_range *range); int amdgpu_hmm_register(struct amdgpu_bo *bo, unsigned long addr); void amdgpu_hmm_unregister(struct amdgpu_bo *bo); #else@@ -52,17 +57,17 @@ static inline int amdgpu_hmm_register(struct amdgpu_bo *bo, unsigned long addr) static inline void amdgpu_hmm_unregister(struct amdgpu_bo *bo) {} -static inline bool amdgpu_hmm_range_valid(struct hmm_range *hmm_range) +static inline bool amdgpu_hmm_range_valid(struct amdgpu_hmm_range *range) { return false; } -static inline struct hmm_range *amdgpu_hmm_range_alloc(void) +static inline struct amdgpu_hmm_range *amdgpu_hmm_range_alloc(struct amdgpu_bo *bo) { return NULL; } -static inline void amdgpu_hmm_range_free(struct hmm_range *hmm_range) {} +static inline void amdgpu_hmm_range_free (struct amdgpu_hmm_range *range) {} #endif #endifdrivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +3
-3@@ -709,7 +709,7 @@ struct amdgpu_ttm_tt {* that range is a valid memory and it is freed too. */ int amdgpu_ttm_tt_get_user_pages(struct amdgpu_bo *bo, - struct hmm_range *range) + struct amdgpu_hmm_range *range) { struct ttm_tt *ttm = bo->tbo.ttm; struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);@@ -762,12 +762,12 @@ int amdgpu_ttm_tt_get_user_pages(struct amdgpu_bo *bo,* that backs user memory and will ultimately be mapped into the device * address space. */ -void amdgpu_ttm_tt_set_user_pages (struct ttm_tt *ttm,struct hmm_range *range) +void amdgpu_ttm_tt_set_user_pages (struct ttm_tt *ttm,struct amdgpu_hmm_range *range) { unsigned long i; for (i = 0; i < ttm->num_pages; ++i) - ttm->pages[i] = range ? hmm_pfn_to_page(range->hmm_pfns[i]) : NULL; + ttm->pages[i] = range ? hmm_pfn_to_page(range->hmm_range.hmm_pfns[i]) : NULL; } /*drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +4
-2@@ -28,6 +28,7 @@ #include <drm/gpu_scheduler.h> #include <drm/ttm/ttm_placement.h> #include "amdgpu_vram_mgr.h" +#include "amdgpu_hmm.h" #define AMDGPU_PL_GDS (TTM_PL_PRIV + 0) #define AMDGPU_PL_GWS (TTM_PL_PRIV + 1)@@ -191,16 +192,16 @@ uint64_t amdgpu_ttm_domain_start(struct amdgpu_device *adev, uint32_t type); #if IS_ENABLED(CONFIG_DRM_AMDGPU_USERPTR) int amdgpu_ttm_tt_get_user_pages(struct amdgpu_bo *bo, - struct hmm_range *range); + struct amdgpu_hmm_range *range); #else static inline int amdgpu_ttm_tt_get_user_pages(struct amdgpu_bo *bo, - struct hmm_range *range) + struct amdgpu_hmm_range *range) { return -EPERM; } #endif -void amdgpu_ttm_tt_set_user_pages(struct ttm_tt *ttm, struct hmm_range *range); +void amdgpu_ttm_tt_set_user_pages (struct ttm_tt *ttm,struct amdgpu_hmm_range *range); int amdgpu_ttm_tt_get_userptr(const struct ttm_buffer_object *tbo, uint64_t *user_addr); int amdgpu_ttm_tt_set_userptr(struct ttm_buffer_object *bo,drivers/gpu/drm/amd/amdkfd/kfd_migrate.c +0
-1@@ -21,7 +21,6 @@* OTHER DEALINGS IN THE SOFTWARE. */ #include <linux/types.h> -#include <linux/hmm.h> #include <linux/dma-direction.h> #include <linux/dma-mapping.h> #include <linux/migrate.h>drivers/gpu/drm/amd/amdkfd/kfd_migrate.h +0
-1@@ -31,7 +31,6 @@ #include <linux/list.h> #include <linux/mutex.h> #include <linux/sched/mm.h> -#include <linux/hmm.h> #include "kfd_priv.h" #include "kfd_svm.h"diff --git
a/drivers/gpu/drm/amd/amdkfd/kfd_svm.cb/drivers/gpu/drm/amd/amdkfd/kfd_svm.cindex91609dd5730f..f041643308ca100644drivers/gpu/drm/amd/amdkfd/kfd_svm.c +7
-7@@ -1698,7 +1698,7 @@ static int svm_range_validate_and_map(struct mm_struct *mm, start = map_start << PAGE_SHIFT; end = (map_last + 1) << PAGE_SHIFT; for (addr = start; !r && addr < end; ) { - struct hmm_range *hmm_range = NULL; + struct amdgpu_hmm_range *range = NULL; unsigned long map_start_vma; unsigned long map_last_vma; struct vm_area_struct *vma;@@ -1737,13 +1737,13 @@ static int svm_range_validate_and_map(struct mm_struct *mm, } WRITE_ONCE(p->svms.faulting_task, current); - hmm_range = amdgpu_hmm_range_alloc(); + range = amdgpu_hmm_range_alloc(NULL); r = amdgpu_hmm_range_get_pages(&prange->notifier, addr, npages, readonly, owner, - hmm_range); + range); WRITE_ONCE(p->svms.faulting_task, NULL); if (r) { - amdgpu_hmm_range_free(hmm_range); + amdgpu_hmm_range_free(range); pr_debug("failed %d to get svm range pages\n", r); } } else {@@ -1753,7 +1753,7 @@ static int svm_range_validate_and_map(struct mm_struct *mm, if (!r) { offset = (addr >> PAGE_SHIFT) - prange->start; r = svm_range_dma_map(prange, ctx->bitmap, offset, npages, - hmm_range->hmm_pfns); + range->hmm_range.hmm_pfns); if (r) pr_debug("failed %d to dma map range\n", r); }@@ -1764,12 +1764,12 @@ static int svm_range_validate_and_map(struct mm_struct *mm,* Overrride return value to TRY AGAIN only if prior returns * were successful */ - if (hmm_range && !amdgpu_hmm_range_valid(hmm_range) && !r) { + if (range && !amdgpu_hmm_range_valid(range) && !r) { pr_debug("hmm update the range, need validate again\n"); r = -EAGAIN; } /* Free the hmm range */ - amdgpu_hmm_range_free(hmm_range); + amdgpu_hmm_range_free(range); if (!r && !list_empty(&prange->child_list)) {drivers/gpu/drm/amd/amdkfd/kfd_svm.h +0
-1@@ -31,7 +31,6 @@ #include <linux/list.h> #include <linux/mutex.h> #include <linux/sched/mm.h> -#include <linux/hmm.h> #include "amdgpu.h" #include "kfd_priv.h"