HMM profiler interface
Subsystems (3)
cover HMM profiler interface
The ROCm profiler would expose the data from KFD profiling APIs to application developers to tune the applications based on how the address range attributes affect the behavior and performance.
Per process event log use the existing SMI (system management interface) event API. Each event log is one line of text, starting with event id, and then name=value pairs for the event specific information.
Philip Yang (5):
drm/amdkfd: svm range restore work deadlock when process exit drm/amdkfd: correct SMI event read size
drm/amdkfd: enable per process SMI event drm/amdkfd: add page fault SMI event
drm/amdkfd: add migration SMI event
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c | 67 +++++++++----- drivers/gpu/drm/amd/amdkfd/kfd_migrate.h | 5 +- drivers/gpu/drm/amd/amdkfd/kfd_process.c | 1 - drivers/gpu/drm/amd/amdkfd/kfd_smi_events.c | 98 +++++++++++++++++++-- drivers/gpu/drm/amd/amdkfd/kfd_smi_events.h | 8 ++ drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 42 +++++---- include/uapi/linux/kfd_ioctl.h | 27 ++++++ 7 files changed, 198 insertions(+), 50 deletions(-)
Signature · 1 line
2.17.1
PATCH 1/5 drm/amdkfd: svm range restore work deadlock when process exit
kfd_process_notifier_release flush svm_range_restore_work may cause deadlock, because svm_range_restore_work calls svm_range_list_lock_and_flush_work which takes mmap write lock. Move flush svm_range_restore_work to kfd_process_wq_release to avoid deadlock. Then svm_range_restore_work needs taking mm ref.
--- drivers/gpu/drm/amd/amdkfd/kfd_process.c | 1 - drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 14 ++++++++------ 2 files changed, 8 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_process.c +0 -1
@@ -1150,7 +1150,6 @@ static void kfd_process_notifier_release(struct mmu_notifier *mn,cancel_delayed_work_sync (&p->eviction_work);cancel_delayed_work_sync (&p->restore_work); -cancel_delayed_work_sync (&p->svms.restore_work);mutex_lock (&p->mutex);
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +8 -6
@@ -1643,13 +1643,13 @@ static void svm_range_restore_work(struct work_struct *work)pr_debug ("restore svm ranges\n" ); -/* kfd_process_notifier_release destroys this worker thread. So during -* the lifetime of this thread, kfd_process and mm will be valid. -*/ p = container_of(svms, struct kfd_process, svms); - mm = p->mm; -if (!mm) +/* Avoid mm is gone when svm_range_validate_and_map ranges */ + mm =get_task_mm (p->lead_thread); +if (!mm) { +pr_debug ("svms 0x%p process mm gone\n" , svms);return ; + }svm_range_list_lock_and_flush_work (svms, mm);mutex_lock (&svms->lock);
@@ -1703,6 +1703,7 @@ static void svm_range_restore_work(struct work_struct *work) out_reschedule:mutex_unlock (&svms->lock);mmap_write_unlock (mm); +mmput (mm);/* If validation failed, reschedule another attempt */ if (evicted_ranges) {
@@ -2830,6 +2831,8 @@ void svm_range_list_fini(struct kfd_process *p)pr_debug ("pasid 0x%x svms 0x%p\n" , p->pasid, &p->svms); +cancel_delayed_work_sync (&p->svms.restore_work); +/* Ensure list work is finished before process is destroyed */ flush_work (&p->svms.deferred_list_work);
@@ -2840,7 +2843,6 @@ void svm_range_list_fini(struct kfd_process *p)atomic_inc (&p->svms.drain_pagefaults);svm_range_drain_retry_fault (&p->svms); -list_for_each_entry_safe (prange, next, &p->svms.list, list) {svm_range_unlink (prange);svm_range_remove_notifier (prange);
Signature · 1 line
2.17.1
PATCH 2/5 drm/amdkfd: correct SMI event read size
In reply to Philip Yangsizeof(buf) is 8 bytes because it is defined as unsigned char *buf, each SMI event read only copy max 8 bytes to user buffer. Correct this by using the buf allocate size.
--- drivers/gpu/drm/amd/amdkfd/kfd_smi_events.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_smi_events.c +3 -2
@@ -81,7 +81,8 @@ static ssize_t kfd_smi_ev_read(struct file *filep, char __user *user,struct kfd_smi_client *client = filep->private_data;unsigned char *buf; - buf =kmalloc_array (MAX_KFIFO_SIZE ,sizeof (*buf),GFP_KERNEL ); + size =min (size, (size_t )MAX_KFIFO_SIZE ); + buf =kmalloc (size,GFP_KERNEL );if (!buf)return -ENOMEM ;
@@ -95,7 +96,7 @@ static ssize_t kfd_smi_ev_read(struct file *filep, char __user *user, ret = -EAGAIN ; goto ret_err; } - to_copy =min3 (size,sizeof (buf), to_copy); + to_copy =min (size, to_copy); ret =kfifo_out (&client->fifo, buf, to_copy);spin_unlock (&client->lock);if (ret <=0 ) {
Signature · 1 line
2.17.1
PATCH 3/5 drm/amdkfd: enable per process SMI event
In reply to Philip YangProcess receive event log from same process by default. Add a flag to be able to receive event log from all processes, this requires super user permission.
Event log with pid 0 send to all processes.
Define new event log id, migration trigger, user queue eviction trigger, those new event log will be added in following patches.
Update kfd_ioctl.h version.
--- drivers/gpu/drm/amd/amdkfd/kfd_smi_events.c | 28 +++++++++++++++------ include/uapi/linux/kfd_ioctl.h | 27 ++++++++++++++++++++ 2 files changed, 48 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_smi_events.c +21 -7
@@ -37,6 +37,8 @@ struct kfd_smi_client {uint64_t events;struct kfd_dev *dev;spinlock_t lock; +pid_t pid; +bool suser; };#define MAX_KFIFO_SIZE 1024
@@ -150,16 +152,26 @@ static int kfd_smi_ev_release(struct inode *inode, struct file *filep)return 0 ; } -static void add_event_to_kfifo (struct kfd_dev *dev,unsigned int smi_event, -char *event_msg,int len) +static bool kfd_smi_ev_enabled (pid_t pid,struct kfd_smi_client *client, +unsigned int smi_event) +{ +if (pid && + !(client->suser && client->events &KFD_SMI_EVENT_MASK_FROM_INDEX (KFD_SMI_EVENT_ALL_PROCESSES )) && + client->pid != pid) +return false; + +return client->events &KFD_SMI_EVENT_MASK_FROM_INDEX (smi_event); +} + +static void add_event_to_kfifo (pid_t pid,struct kfd_dev *dev, +unsigned int smi_event,char *event_msg,int len) {struct kfd_smi_client *client;rcu_read_lock ();list_for_each_entry_rcu (client, &dev->smi_clients, list) { -if (!(READ_ONCE (client->events) & -KFD_SMI_EVENT_MASK_FROM_INDEX (smi_event))) +if (!kfd_smi_ev_enabled (pid, client, smi_event))continue ;spin_lock (&client->lock);if (kfifo_avail (&client->fifo) >= len) {
@@ -202,7 +214,7 @@ void kfd_smi_event_update_gpu_reset(struct kfd_dev *dev, bool post_reset) len =snprintf (fifo_in,sizeof (fifo_in),"%x %x\n" , event, dev->reset_seq_num); -add_event_to_kfifo (dev, event, fifo_in, len); +add_event_to_kfifo (0 , dev, event, fifo_in, len); } void kfd_smi_event_update_thermal_throttling(struct kfd_dev *dev,
@@ -225,7 +237,7 @@ void kfd_smi_event_update_thermal_throttling(struct kfd_dev *dev,KFD_SMI_EVENT_THERMAL_THROTTLE , throttle_bitmask, amdgpu_dpm_get_thermal_throttling_counter(dev->adev)); -add_event_to_kfifo (dev,KFD_SMI_EVENT_THERMAL_THROTTLE , fifo_in, len); +add_event_to_kfifo (0 , dev,KFD_SMI_EVENT_THERMAL_THROTTLE , fifo_in, len); }void kfd_smi_event_update_vmfault (struct kfd_dev *dev,uint16_t pasid)
@@ -250,7 +262,7 @@ void kfd_smi_event_update_vmfault(struct kfd_dev *dev, uint16_t pasid) len =snprintf (fifo_in,sizeof (fifo_in),"%x %x:%s\n" ,KFD_SMI_EVENT_VMFAULT , task_info.pid, task_info.task_name); -add_event_to_kfifo (dev,KFD_SMI_EVENT_VMFAULT , fifo_in, len); +add_event_to_kfifo (task_info.pid, dev,KFD_SMI_EVENT_VMFAULT , fifo_in, len); }int kfd_smi_event_open (struct kfd_dev *dev,uint32_t *fd)
@@ -282,6 +294,8 @@ int kfd_smi_event_open(struct kfd_dev *dev, uint32_t *fd)spin_lock_init (&client->lock); client->events =0 ; client->dev = dev; + client->pid = current->pid; + client->suser =capable (CAP_SYS_ADMIN );spin_lock (&dev->smi_lock);list_add_rcu (&client->list, &dev->smi_clients);
include/uapi/linux/kfd_ioctl.h +27 -0
@@ -32,6 +32,7 @@* - 1.4 - Indicate new SRAM EDC bit in device properties * - 1.5 - Add SVM API * - 1.6 - Query clear flags in SVM get_attr API +* - 1.7 - Add SMI profiler event log */ #define KFD_IOCTL_MAJOR_VERSION 1#define KFD_IOCTL_MINOR_VERSION 6
@@ -459,10 +460,36 @@ enum kfd_smi_event {KFD_SMI_EVENT_THERMAL_THROTTLE =2 ,KFD_SMI_EVENT_GPU_PRE_RESET =3 ,KFD_SMI_EVENT_GPU_POST_RESET =4 , +KFD_SMI_EVENT_MIGRATION =5 , +KFD_SMI_EVENT_PAGE_FAULT =6 , +KFD_SMI_EVENT_QUEUE_EVICTION =7 , +KFD_SMI_EVENT_QUEUE_EVICTION_RESTORE =8 , + +/* +* max event number, as a flag bit to get events from all processes, +* this requires super user permission, otherwise will not be able to +* receive events from any process. Without this flag to receive events +* from same process. +*/ +KFD_SMI_EVENT_ALL_PROCESSES =64 };#define KFD_SMI_EVENT_MASK_FROM_INDEX (i) (1ULL << ((i) - 1)) +enum KFD_MIGRATION_TRIGGER { +MIGRATION_TRIGGER_PREFETCH =1 , +MIGRATION_TRIGGER_PAGEFAULT , +MIGRATION_TRIGGER_PAGEFAULT_CPU , +MIGRATION_TRIGGER_TTM_EVICTION +}; + +enum KFD_USER_QUEUE_EVICTION_TRIGGER { +SVM_RANGE_EVICTION =1 , +USERPTR_EVICTION , +TTM_EVICTION , +SUSPEND_EVICTION +}; +struct kfd_ioctl_smi_events_args {__u32 gpuid;/* to KFD */ __u32 anon_fd;/* from KFD */
Signature · 1 line
2.17.1
PATCH 4/5 drm/amdkfd: add page fault SMI event
In reply to Philip YangAfter GPU page fault is recovered, output timestamp when fault is received, duration to recover the fault, if migration or only GPU page table is updated, fault address, read or write fault.
--- drivers/gpu/drm/amd/amdkfd/kfd_smi_events.c | 41 +++++++++++++++++++++ drivers/gpu/drm/amd/amdkfd/kfd_smi_events.h | 3 ++ drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 12 ++++-- 3 files changed, 53 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_smi_events.c +41 -0
@@ -265,6 +265,47 @@ void kfd_smi_event_update_vmfault(struct kfd_dev *dev, uint16_t pasid)add_event_to_kfifo (task_info.pid, dev,KFD_SMI_EVENT_VMFAULT , fifo_in, len); } +static bool kfd_smi_event_pid_duration (struct kfd_dev *dev,uint16_t pasid, +pid_t *pid,uint64_t ts, +uint64_t *duration) +{ +struct amdgpu_task_info task_info = {0 }; + +if (list_empty (&dev->smi_clients)) +return false; + +amdgpu_vm_get_task_info (dev->adev, pasid, &task_info); +if (!task_info.pid) { +pr_debug ("task is gone\n" ); +return false; + } +if (pid) + *pid = task_info.pid; +if (duration) + *duration =ktime_get_ns () - ts; +return true; +} + +void kfd_smi_event_page_fault (struct kfd_dev *dev,uint16_t pasid, +unsigned long address,bool migration, +bool write_fault,uint64_t ts) +{ +char fifo_in[128 ]; +uint64_t duration; +pid_t pid; +int len; + +if (!kfd_smi_event_pid_duration (dev, pasid, &pid, ts, &duration)) +return ; + + len =snprintf (fifo_in,sizeof (fifo_in),"%d ts=%lld duration=%lld" +" pid=%d pfn=0x%lx write=%d migration=%d\n" , +KFD_SMI_EVENT_PAGE_FAULT , ts, duration, pid, address, + write_fault, migration); + +add_event_to_kfifo (pid, dev,KFD_SMI_EVENT_PAGE_FAULT , fifo_in, len); +} +int kfd_smi_event_open (struct kfd_dev *dev,uint32_t *fd) {struct kfd_smi_client *client;
drivers/gpu/drm/amd/amdkfd/kfd_smi_events.h +3 -0
@@ -28,5 +28,8 @@ void kfd_smi_event_update_vmfault(struct kfd_dev *dev, uint16_t pasid);void kfd_smi_event_update_thermal_throttling (struct kfd_dev *dev,uint64_t throttle_bitmask);void kfd_smi_event_update_gpu_reset (struct kfd_dev *dev,bool post_reset); +void kfd_smi_event_page_fault (struct kfd_dev *dev,uint16_t pasid, +unsigned long address,bool migration, +bool write_fault,uint64_t ts);#endif
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +9 -3
@@ -32,6 +32,7 @@#include "kfd_priv.h" #include "kfd_svm.h" #include "kfd_migrate.h" +#include "kfd_smi_events.h" #ifdef dev_fmt#undef dev_fmt
@@ -2657,11 +2658,12 @@ svm_range_restore_pages(struct amdgpu_device *adev, unsigned int pasid,struct svm_range_list *svms;struct svm_range *prange;struct kfd_process *p; -uint64_t timestamp; +uint64_t timestamp =ktime_get_ns ();int32_t best_loc;int32_t gpuidx =MAX_GPU_INSTANCE ;bool write_locked = false;struct vm_area_struct *vma; +bool migration = false;int r =0 ;if (!KFD_IS_SVM_API_SUPPORTED (adev->kfd.dev)) {
@@ -2737,9 +2739,9 @@ svm_range_restore_pages(struct amdgpu_device *adev, unsigned int pasid, goto out_unlock_range; } - timestamp =ktime_to_us (ktime_get ()) - prange->validate_timestamp;/* skip duplicate vm fault on different pages of same range */ -if (timestamp <AMDGPU_SVM_RANGE_RETRY_FAULT_PENDING ) { +if ((ktime_to_us (timestamp) - prange->validate_timestamp) < +AMDGPU_SVM_RANGE_RETRY_FAULT_PENDING ) {pr_debug ("svms 0x%p [0x%lx %lx] already restored\n" , svms, prange->start, prange->last); r =0 ;
@@ -2776,6 +2778,7 @@ svm_range_restore_pages(struct amdgpu_device *adev, unsigned int pasid, prange->actual_loc);if (prange->actual_loc != best_loc) { + migration = true;if (best_loc) { r =svm_migrate_to_vram (prange, best_loc, mm);if (r) {
@@ -2804,6 +2807,9 @@ svm_range_restore_pages(struct amdgpu_device *adev, unsigned int pasid,pr_debug ("failed %d to map svms 0x%p [0x%lx 0x%lx] to gpus\n" , r, svms, prange->start, prange->last); +kfd_smi_event_page_fault (adev->kfd.dev, p->pasid, addr, migration, + write_fault, timestamp); + out_unlock_range:mutex_unlock (&prange->migrate_mutex); out_unlock_svms:
Signature · 1 line
2.17.1
PATCH 5/5 drm/amdkfd: add migration SMI event
In reply to Philip YangAfter migration is finished, output timestamp when migration starts, duration of migration, svm range address and size, GPU id of migration source and destination, svm range attributes,
Migration trigger could be prefetch, CPU, GPU page fault and TTM eviction.
--- drivers/gpu/drm/amd/amdkfd/kfd_migrate.c | 67 ++++++++++++++------- drivers/gpu/drm/amd/amdkfd/kfd_migrate.h | 5 +- drivers/gpu/drm/amd/amdkfd/kfd_smi_events.c | 24 ++++++++ drivers/gpu/drm/amd/amdkfd/kfd_smi_events.h | 5 ++ drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 16 +++-- 5 files changed, 86 insertions(+), 31 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c +44 -23
@@ -32,6 +32,7 @@#include "kfd_priv.h" #include "kfd_svm.h" #include "kfd_migrate.h" +#include "kfd_smi_events.h" #ifdef dev_fmt#undef dev_fmt
@@ -405,10 +406,11 @@ svm_migrate_copy_to_vram(struct amdgpu_device *adev, struct svm_range *prange,static long svm_migrate_vma_to_vram (struct amdgpu_device *adev,struct svm_range *prange,struct vm_area_struct *vma,uint64_t start, -uint64_t end) +uint64_t end,uint32_t trigger) {uint64_t npages = (end - start) >>PAGE_SHIFT ; -struct kfd_process_device *pdd; +struct kfd_process_device *pdd =NULL ; +uint64_t timestamp =ktime_get_ns ();struct dma_fence *mfence =NULL ;struct migrate_vma migrate;unsigned long cpages =0 ;
@@ -434,6 +436,10 @@ svm_migrate_vma_to_vram(struct amdgpu_device *adev, struct svm_range *prange, migrate.dst = migrate.src + npages; scratch = (dma_addr_t *)(migrate.dst + npages); + pdd =svm_range_get_pdd_by_adev (prange, adev); +if (!pdd) + goto out_free; + r =migrate_vma_setup (&migrate);if (r) { dev_err(adev->dev, "vma setup fail %d range [0x%lx 0x%lx]\n", r,
@@ -462,6 +468,11 @@ svm_migrate_vma_to_vram(struct amdgpu_device *adev, struct svm_range *prange,svm_migrate_copy_done (adev, mfence);migrate_vma_finalize (&migrate); +kfd_smi_event_migration (adev->kfd.dev, pdd->process->pasid, + start >>PAGE_SHIFT , end >>PAGE_SHIFT , +0 , adev->kfd.dev->id, prange->prefetch_loc, + prange->preferred_loc, trigger, timestamp); +svm_range_dma_unmap (adev->dev, scratch,0 , npages);svm_range_free_dma_mappings (prange);
@@ -469,10 +480,7 @@ svm_migrate_vma_to_vram(struct amdgpu_device *adev, struct svm_range *prange,kvfree (buf); out:if (!r && cpages) { - pdd =svm_range_get_pdd_by_adev (prange, adev); -if (pdd) -WRITE_ONCE (pdd->page_in, pdd->page_in + cpages); - +WRITE_ONCE (pdd->page_in, pdd->page_in + cpages);return cpages; }return r;
@@ -483,6 +491,7 @@ svm_migrate_vma_to_vram(struct amdgpu_device *adev, struct svm_range *prange,* @prange: range structure * @best_loc: the device to migrate to * @mm: the process mm structure +* @trigger: reason of migration * * Context: Process context, caller hold mmap read lock, svms lock, prange lock *
@@ -491,7 +500,7 @@ svm_migrate_vma_to_vram(struct amdgpu_device *adev, struct svm_range *prange,*/ static int svm_migrate_ram_to_vram (struct svm_range *prange,uint32_t best_loc, -struct mm_struct *mm) +struct mm_struct *mm,uint32_t trigger) {unsigned long addr, start, end;struct vm_area_struct *vma;
@@ -528,7 +537,7 @@ svm_migrate_ram_to_vram(struct svm_range *prange, uint32_t best_loc,break ; next =min (vma->vm_end, end); - r =svm_migrate_vma_to_vram (adev, prange, vma, addr, next); + r =svm_migrate_vma_to_vram (adev, prange, vma, addr, next, trigger);if (r <0 ) {pr_debug ("failed %ld to migrate\n" , r);break ;
@@ -644,12 +653,14 @@ svm_migrate_copy_to_ram(struct amdgpu_device *adev, struct svm_range *prange,static long svm_migrate_vma_to_ram (struct amdgpu_device *adev,struct svm_range *prange, -struct vm_area_struct *vma,uint64_t start,uint64_t end) +struct vm_area_struct *vma,uint64_t start,uint64_t end, +uint32_t trigger) {uint64_t npages = (end - start) >>PAGE_SHIFT ; +uint64_t timestamp =ktime_get_ns ();unsigned long upages = npages;unsigned long cpages =0 ; -struct kfd_process_device *pdd; +struct kfd_process_device *pdd =NULL ;struct dma_fence *mfence =NULL ;struct migrate_vma migrate;dma_addr_t *scratch;
@@ -674,6 +685,10 @@ svm_migrate_vma_to_ram(struct amdgpu_device *adev, struct svm_range *prange, migrate.dst = migrate.src + npages; scratch = (dma_addr_t *)(migrate.dst + npages); + pdd =svm_range_get_pdd_by_adev (prange, adev); +if (!pdd) + goto out_free; + r =migrate_vma_setup (&migrate);if (r) { dev_err(adev->dev, "vma setup fail %d range [0x%lx 0x%lx]\n", r,
@@ -704,16 +719,19 @@ svm_migrate_vma_to_ram(struct amdgpu_device *adev, struct svm_range *prange,svm_migrate_copy_done (adev, mfence);migrate_vma_finalize (&migrate); + +kfd_smi_event_migration (adev->kfd.dev, pdd->process->pasid, + start >>PAGE_SHIFT , end >>PAGE_SHIFT , + adev->kfd.dev->id,0 , prange->prefetch_loc, + prange->preferred_loc, trigger, timestamp); +svm_range_dma_unmap (adev->dev, scratch,0 , npages); out_free:kvfree (buf); out:if (!r && cpages) { - pdd =svm_range_get_pdd_by_adev (prange, adev); -if (pdd) -WRITE_ONCE (pdd->page_out, pdd->page_out + cpages); - +WRITE_ONCE (pdd->page_out, pdd->page_out + cpages);return upages; }return r ? r : upages;
@@ -723,13 +741,15 @@ svm_migrate_vma_to_ram(struct amdgpu_device *adev, struct svm_range *prange,* svm_migrate_vram_to_ram - migrate svm range from device to system * @prange: range structure * @mm: process mm, use current->mm if NULL +* @trigger: reason of migration * * Context: Process context, caller hold mmap read lock, svms lock, prange lock * * Return: * 0 - OK, otherwise error code */ -int svm_migrate_vram_to_ram (struct svm_range *prange,struct mm_struct *mm) +int svm_migrate_vram_to_ram (struct svm_range *prange,struct mm_struct *mm, +uint32_t trigger) {struct amdgpu_device *adev;struct vm_area_struct *vma;
@@ -767,7 +787,7 @@ int svm_migrate_vram_to_ram(struct svm_range *prange, struct mm_struct *mm)break ; next =min (vma->vm_end, end); - r =svm_migrate_vma_to_ram (adev, prange, vma, addr, next); + r =svm_migrate_vma_to_ram (adev, prange, vma, addr, next, trigger);if (r <0 ) {pr_debug ("failed %ld to migrate\n" , r);break ;
@@ -790,6 +810,7 @@ int svm_migrate_vram_to_ram(struct svm_range *prange, struct mm_struct *mm)* @prange: range structure * @best_loc: the device to migrate to * @mm: process mm, use current->mm if NULL +* @trigger: reason of migration * * Context: Process context, caller hold mmap read lock, svms lock, prange lock *
@@ -798,7 +819,7 @@ int svm_migrate_vram_to_ram(struct svm_range *prange, struct mm_struct *mm)*/ static int svm_migrate_vram_to_vram (struct svm_range *prange,uint32_t best_loc, -struct mm_struct *mm) +struct mm_struct *mm,uint32_t trigger) {int r, retries =3 ;
@@ -810,7 +831,7 @@ svm_migrate_vram_to_vram(struct svm_range *prange, uint32_t best_loc,pr_debug ("from gpu 0x%x to gpu 0x%x\n" , prange->actual_loc, best_loc);do { - r =svm_migrate_vram_to_ram (prange, mm); + r =svm_migrate_vram_to_ram (prange, mm, trigger);if (r)return r; }while (prange->actual_loc && --retries);
@@ -818,17 +839,17 @@ svm_migrate_vram_to_vram(struct svm_range *prange, uint32_t best_loc,if (prange->actual_loc)return -EDEADLK ; -return svm_migrate_ram_to_vram (prange, best_loc, mm); +return svm_migrate_ram_to_vram (prange, best_loc, mm, trigger); }int svm_migrate_to_vram (struct svm_range *prange,uint32_t best_loc, -struct mm_struct *mm) +struct mm_struct *mm,uint32_t trigger) {if (!prange->actual_loc) -return svm_migrate_ram_to_vram (prange, best_loc, mm); +return svm_migrate_ram_to_vram (prange, best_loc, mm, trigger);else -return svm_migrate_vram_to_vram (prange, best_loc, mm); +return svm_migrate_vram_to_vram (prange, best_loc, mm, trigger); }
@@ -897,7 +918,7 @@ static vm_fault_t svm_migrate_to_ram(struct vm_fault *vmf) goto out_unlock_prange; } - r =svm_migrate_vram_to_ram (prange, mm); + r =svm_migrate_vram_to_ram (prange, mm,MIGRATION_TRIGGER_PAGEFAULT_CPU );if (r)pr_debug ("failed %d migrate 0x%p [0x%lx 0x%lx] to ram\n" , r, prange, prange->start, prange->last);
drivers/gpu/drm/amd/amdkfd/kfd_migrate.h +3 -2
@@ -41,8 +41,9 @@ enum MIGRATION_COPY_DIR { };int svm_migrate_to_vram (struct svm_range *prange,uint32_t best_loc, -struct mm_struct *mm); -int svm_migrate_vram_to_ram (struct svm_range *prange,struct mm_struct *mm); +struct mm_struct *mm,uint32_t trigger); +int svm_migrate_vram_to_ram (struct svm_range *prange,struct mm_struct *mm, +uint32_t trigger);unsigned long svm_migrate_addr_to_pfn (struct amdgpu_device *adev,unsigned long addr);
drivers/gpu/drm/amd/amdkfd/kfd_smi_events.c +24 -0
@@ -286,6 +286,30 @@ static bool kfd_smi_event_pid_duration(struct kfd_dev *dev, uint16_t pasid,return true; } +void kfd_smi_event_migration (struct kfd_dev *dev,uint16_t pasid, +unsigned long start,unsigned long end, +uint32_t from,uint32_t to, +uint32_t prefetch_loc,uint32_t preferred_loc, +uint32_t trigger,uint64_t ts) +{ +char fifo_in[256 ]; +uint64_t duration; +pid_t pid; +int len; + +if (!kfd_smi_event_pid_duration (dev, pasid, &pid, ts, &duration)) +return ; + + len =snprintf (fifo_in,sizeof (fifo_in),"%d ts=%lld duration=%lld" +" pid=%d pfn=0x%lx npages=0x%lx from=0x%x to=0x%x" +" prefetch_loc=0x%x preferred_loc=0x%x trigger=%d\n" , +KFD_SMI_EVENT_MIGRATION , ts, duration, pid, start, + end - start, from, to, prefetch_loc, preferred_loc, + trigger); + +add_event_to_kfifo (pid, dev,KFD_SMI_EVENT_MIGRATION , fifo_in, len); +} +void kfd_smi_event_page_fault (struct kfd_dev *dev,uint16_t pasid,unsigned long address,bool migration,bool write_fault,uint64_t ts)
drivers/gpu/drm/amd/amdkfd/kfd_smi_events.h +5 -0
@@ -28,6 +28,11 @@ void kfd_smi_event_update_vmfault(struct kfd_dev *dev, uint16_t pasid);void kfd_smi_event_update_thermal_throttling (struct kfd_dev *dev,uint64_t throttle_bitmask);void kfd_smi_event_update_gpu_reset (struct kfd_dev *dev,bool post_reset); +void kfd_smi_event_migration (struct kfd_dev *dev,uint16_t pasid, +unsigned long start,unsigned long end, +uint32_t from,uint32_t to, +uint32_t prefetch_loc,uint32_t preferred_loc, +uint32_t trigger,uint64_t ts);void kfd_smi_event_page_fault (struct kfd_dev *dev,uint16_t pasid,unsigned long address,bool migration,bool write_fault,uint64_t ts);
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +10 -6
@@ -2780,7 +2780,8 @@ svm_range_restore_pages(struct amdgpu_device *adev, unsigned int pasid,if (prange->actual_loc != best_loc) { migration = true;if (best_loc) { - r =svm_migrate_to_vram (prange, best_loc, mm); + r =svm_migrate_to_vram (prange, best_loc, mm, +MIGRATION_TRIGGER_PAGEFAULT );if (r) {pr_debug ("svm_migrate_to_vram failed (%d) at %llx, falling back to system memory\n" , r, addr);
@@ -2788,12 +2789,14 @@ svm_range_restore_pages(struct amdgpu_device *adev, unsigned int pasid,* VRAM failed */ if (prange->actual_loc) - r =svm_migrate_vram_to_ram (prange, mm); + r =svm_migrate_vram_to_ram (prange, mm, +MIGRATION_TRIGGER_PAGEFAULT );else r =0 ; } }else { - r =svm_migrate_vram_to_ram (prange, mm); + r =svm_migrate_vram_to_ram (prange, mm, +MIGRATION_TRIGGER_PAGEFAULT ); }if (r) { pr_debug("failed %d to migrate svms %p [0x%lx 0x%lx]\n",
@@ -3115,12 +3118,12 @@ svm_range_trigger_migration(struct mm_struct *mm, struct svm_range *prange,return 0 ;if (!best_loc) { - r =svm_migrate_vram_to_ram (prange, mm); + r =svm_migrate_vram_to_ram (prange, mm,MIGRATION_TRIGGER_PREFETCH ); *migrated = !r;return r; } - r =svm_migrate_to_vram (prange, best_loc, mm); + r =svm_migrate_to_vram (prange, best_loc, mm,MIGRATION_TRIGGER_PREFETCH ); *migrated = !r;return r;
@@ -3177,7 +3180,8 @@ static void svm_range_evict_svm_bo_worker(struct work_struct *work)mutex_lock (&prange->migrate_mutex);do {svm_migrate_vram_to_ram (prange, - svm_bo->eviction_fence->mm); + svm_bo->eviction_fence->mm, +MIGRATION_TRIGGER_TTM_EVICTION ); }while (prange->actual_loc && --retries);WARN (prange->actual_loc,"Migration failed during eviction" );
Signature · 1 line
2.17.1
Please discard this, I will resend.
On 2022-01-14 3:32 p.m., Philip Yang wrote:
The ROCm profiler would expose the data from KFD profiling APIs to application developers to tune the applications based on how the address range attributes affect the behavior and performance. Per process event log use the existing SMI (system management interface) event API. Each event log is one line of text, starting with event id, and then name=value pairs for the event specific information. Philip Yang (5): drm/amdkfd: svm range restore work deadlock when process exit drm/amdkfd: correct SMI event read size drm/amdkfd: enable per process SMI event drm/amdkfd: add page fault SMI event drm/amdkfd: add migration SMI event drivers/gpu/drm/amd/amdkfd/kfd_migrate.c | 67 +++++++++----- drivers/gpu/drm/amd/amdkfd/kfd_migrate.h | 5 +- drivers/gpu/drm/amd/amdkfd/kfd_process.c | 1 - drivers/gpu/drm/amd/amdkfd/kfd_smi_events.c | 98 +++++++++++++++++++-- drivers/gpu/drm/amd/amdkfd/kfd_smi_events.h | 8 ++ drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 42 +++++---- include/uapi/linux/kfd_ioctl.h | 27 ++++++ 7 files changed, 198 insertions(+), 50 deletions(-)