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: 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 drm/amdkfd: add user queue eviction restore SMI event
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h | 7 +- .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 11 +- drivers/gpu/drm/amd/amdkfd/kfd_device.c | 4 +- drivers/gpu/drm/amd/amdkfd/kfd_migrate.c | 67 ++++++--- drivers/gpu/drm/amd/amdkfd/kfd_migrate.h | 5 +- drivers/gpu/drm/amd/amdkfd/kfd_priv.h | 2 +- drivers/gpu/drm/amd/amdkfd/kfd_process.c | 36 ++++- drivers/gpu/drm/amd/amdkfd/kfd_smi_events.c | 133 ++++++++++++++++-- drivers/gpu/drm/amd/amdkfd/kfd_smi_events.h | 12 ++ drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 34 +++-- include/uapi/linux/kfd_ioctl.h | 27 ++++ 11 files changed, 282 insertions(+), 56 deletions(-)
Signature · 1 line
2.17.1
PATCH 1/5 drm/amdkfd: correct SMI event read size
sizeof(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 2/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
Felix Kuehling
maintainerRe: [PATCH 2/5] drm/amdkfd: enable per process SMI event
Am 2022-01-14 um 3:38 p.m. schrieb Philip Yang:
Quoted 32 lines
Process 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.hversion.Signed-off-by Philip Yang <[email protected]>--- 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 +2
-0@@ -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 ;
drivers/gpu/drm/amd/amdkfd/kfd_smi_events.c static int kfd_smi_ev_release(struct inode *inode, struct file *filep) · line 160
Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_smi_events.c +6
-2} -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)
We cannot change the default behaviour for existing SMI events. Otherwise be break existing user mode.
I'd make the new flag KFD_SMI_EVENT_SAME_PROCESS. Its default is 0, which preserves the existing behaviour.
The SAME_PROCESS flag can apply to all events. But for the new profiler events, we need to check both the flag and the ADMIN capability to allow events from all processes, because they contain more sensitive information than the existing SMI events.
drivers/gpu/drm/amd/amdkfd/kfd_smi_events.c static int kfd_smi_ev_release(struct inode *inode, struct file *filep) · line 163
Quoted 3 lines
drivers/gpu/drm/amd/amdkfd/kfd_smi_events.c +3
-0+return false; + +return client->events &KFD_SMI_EVENT_MASK_FROM_INDEX (smi_event);
You still need to access client->events with READ_ONCE here. The reason is, that this function is not holding the lock. So we need to read the variable atomically.
Regards,
Felix
Quoted 101 lines
drivers/gpu/drm/amd/amdkfd/kfd_smi_events.c +10
-5+} + +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 */
PATCH 3/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
Felix Kuehling
maintainerRe: [PATCH 3/5] drm/amdkfd: add page fault SMI event
Am 2022-01-14 um 3:38 p.m. schrieb Philip Yang:
Quoted 19 lines
After 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.
Signed-off-by Philip Yang <[email protected]>--- 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 +0
-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); }
drivers/gpu/drm/amd/amdkfd/kfd_smi_events.c void kfd_smi_event_update_vmfault(struct kfd_dev *dev, uint16_t pasid) · line 277
Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_smi_events.c +10
-0+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);
The caller (svm_range_restore_pages) already knows the kfd_process. It should be able to provide the pid directly from p->lead_thread.pid. No need to look that up from the pasid and vm task info.
Quoted 15 lines
drivers/gpu/drm/amd/amdkfd/kfd_smi_events.c +15
-0+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) +{
drivers/gpu/drm/amd/amdkfd/kfd_smi_events.c void kfd_smi_event_update_vmfault(struct kfd_dev *dev, uint16_t pasid) · line 302
Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_smi_events.c +10
-0+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",
Please don't break the string over several lines. I believe that would also trigger a checkpatch warning.
drivers/gpu/drm/amd/amdkfd/kfd_smi_events.c void kfd_smi_event_update_vmfault(struct kfd_dev *dev, uint16_t pasid) · line 304
Quoted 2 lines
drivers/gpu/drm/amd/amdkfd/kfd_smi_events.c +2
-0+KFD_SMI_EVENT_PAGE_FAULT , ts, duration, pid, address, + write_fault, migration);
The existing events use %x for all numbers, including event number and pid. Maybe better to stick with that convention for consistency. At least for the event number.
The existing events seems to favor a very compact layout. I could think of ways to make this event more compact as well (still using decimal for some things for readability):
"%x %d(%d)-%d @%x %c%c", KFD_SMI_EVENT_PAGE_FAULT, ts, duration, pid, address, write ? 'W' : 'w', migration ? 'M' : 'm'
Quoted 33 lines
drivers/gpu/drm/amd/amdkfd/kfd_smi_events.c +4
-0+ +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 +1
-0@@ -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
drivers/gpu/drm/amd/amdkfd/kfd_svm.c svm_range_restore_pages(struct amdgpu_device *adev, unsigned int pasid, · line 2661
Quoted 5 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +1
-1@@ -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 ();
kfd_ioctl_get_clock_counters uses ktime_get_boottime_ns for the system_clock_counter. We should use the same here (and in the duration calculation) to make it possible to correlate timestamps from different KFD APIs.
Quoted 9 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +1
-0int32_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)) {
drivers/gpu/drm/amd/amdkfd/kfd_svm.c svm_range_restore_pages(struct amdgpu_device *adev, unsigned int pasid, · line 2744
Quoted 8 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +2
-2@@ -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 ) {
ktime_to_us takes a ktime_t. But timestamp is just a time in nanoseconds. I think the correct conversion is just div_u64(timestamp, 1000).
Regards,
Felix
Quoted 21 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +4
-0pr_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:
Re: [PATCH 3/5] drm/amdkfd: add page fault SMI event
On 2022-01-14 5:37 p.m., Felix Kuehling wrote:
Am 2022-01-14 um 3:38 p.m. schrieb Philip Yang: After 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. Signed-off-by: Philip Yang <[email protected]> --- 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(-) diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_smi_events.c b/drivers/gpu/drm/amd/amdkfd/kfd_smi_events.c index 5818ea8ad4ce..6ed3d85348d6 100644 --- a/drivers/gpu/drm/amd/amdkfd/kfd_smi_events.c +++ b/drivers/gpu/drm/amd/amdkfd/kfd_smi_events.c @@ -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); The caller (svm_range_restore_pages) already knows the kfd_process. It should be able to provide the pid directly from p->lead_thread.pid. No need to look that up from the pasid and vm task info. yes, I can use p->lead_thread.pid for other events as well.
+ 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", Please don't break the string over several lines. I believe that would also trigger a checkpatch warning. It does trigger checkpatch warning, although I saw other places using multiple lines format, but this is not good to grep string by line, I will short it and use one line format.
+ KFD_SMI_EVENT_PAGE_FAULT, ts, duration, pid, address, + write_fault, migration); The existing events use %x for all numbers, including event number and pid. Maybe better to stick with that convention for consistency. At least for the event number. Will output event number as hex.
The existing events seems to favor a very compact layout. I could think of ways to make this event more compact as well (still using decimal for some things for readability): "%x %d(%d)-%d @%x %c%c", KFD_SMI_EVENT_PAGE_FAULT, ts, duration, pid, address, write ? 'W' : 'w', migration ? 'M' : 'm' good idea to short the format and message.
+ + 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; diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_smi_events.h b/drivers/gpu/drm/amd/amdkfd/kfd_smi_events.h index bffd0c32b060..fa3a8fdad69f 100644 --- a/drivers/gpu/drm/amd/amdkfd/kfd_smi_events.h +++ b/drivers/gpu/drm/amd/amdkfd/kfd_smi_events.h @@ -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 diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c index 37b3191615b6..b81667162dc1 100644 --- a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c +++ b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c @@ -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(); kfd_ioctl_get_clock_counters uses ktime_get_boottime_ns for the system_clock_counter. We should use the same here (and in the duration calculation) to make it possible to correlate timestamps from different KFD APIs. ok 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) { ktime_to_us takes a ktime_t. But timestamp is just a time in nanoseconds. I think the correct conversion is just div_u64(timestamp, 1000). prange->validate_timestamp will change to ktime_get_boottime_ns as well.
Thanks,
Philip
Regards, Felix 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:
PATCH 4/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
PATCH 5/5 drm/amdkfd: add user queue eviction restore SMI event
In reply to Philip YangOutput user queue eviction and restore event. User queue eviction may be triggered by migration, MMU notifier, TTM eviction or device suspend.
User queue restore may be rescheduled if eviction happens again while restore.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h | 7 +++- .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 11 +++--- drivers/gpu/drm/amd/amdkfd/kfd_device.c | 4 +-- drivers/gpu/drm/amd/amdkfd/kfd_priv.h | 2 +- drivers/gpu/drm/amd/amdkfd/kfd_process.c | 36 +++++++++++++++++-- drivers/gpu/drm/amd/amdkfd/kfd_smi_events.c | 35 ++++++++++++++++++ drivers/gpu/drm/amd/amdkfd/kfd_smi_events.h | 4 +++ drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 6 ++-- 8 files changed, 92 insertions(+), 13 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +6 -1
@@ -309,6 +309,7 @@ void amdgpu_amdkfd_gpuvm_destroy_cb(struct amdgpu_device *adev,*/ void amdgpu_amdkfd_release_notify (struct amdgpu_bo *bo);void amdgpu_amdkfd_reserve_system_mem (uint64_t size); +void kfd_process_smi_event_restore_rescheduled (struct mm_struct *mm);#else static inline void amdgpu_amdkfd_gpuvm_init_mem_limits (void )
@@ -325,9 +326,13 @@ static inlinevoid amdgpu_amdkfd_release_notify (struct amdgpu_bo *bo) { } + +static inline void kfd_process_smi_event_restore_rescheduled (struct mm_struct *mm) +{ +}#endif /* KGD2KFD callbacks */ -int kgd2kfd_quiesce_mm (struct mm_struct *mm); +int kgd2kfd_quiesce_mm (struct mm_struct *mm,uint32_t trigger);int kgd2kfd_resume_mm (struct mm_struct *mm);int kgd2kfd_schedule_evict_and_restore_process (struct mm_struct *mm,struct dma_fence *fence);
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +7 -4
@@ -2072,7 +2072,7 @@ int amdgpu_amdkfd_evict_userptr(struct kgd_mem *mem, evicted_bos =atomic_inc_return (&process_info->evicted_bos);if (evicted_bos ==1 ) {/* First eviction, stop the queues */ - r =kgd2kfd_quiesce_mm (mm); + r =kgd2kfd_quiesce_mm (mm,USERPTR_EVICTION );if (r)pr_err ("Failed to quiesce KFD\n" ); schedule_delayed_work(&process_info->restore_userptr_work,
@@ -2346,13 +2346,16 @@ static void amdgpu_amdkfd_restore_userptr_worker(struct work_struct *work) unlock_out:mutex_unlock (&process_info->lock); -mmput (mm); -put_task_struct (usertask);/* If validation failed, reschedule another attempt */ -if (evicted_bos) +if (evicted_bos) {schedule_delayed_work (&process_info->restore_userptr_work,msecs_to_jiffies (AMDGPU_USERPTR_RESTORE_DELAY_MS )); + +kfd_process_smi_event_restore_rescheduled (mm); + } +mmput (mm); +put_task_struct (usertask); }/** amdgpu_amdkfd_gpuvm_restore_process_bos - Restore all BOs for the given
drivers/gpu/drm/amd/amdkfd/kfd_device.c +2 -2
@@ -783,7 +783,7 @@ void kgd2kfd_interrupt(struct kfd_dev *kfd, const void *ih_ring_entry)spin_unlock_irqrestore (&kfd->interrupt_lock, flags); } -int kgd2kfd_quiesce_mm (struct mm_struct *mm) +int kgd2kfd_quiesce_mm (struct mm_struct *mm,uint32_t trigger) {struct kfd_process *p;int r;
@@ -797,7 +797,7 @@ int kgd2kfd_quiesce_mm(struct mm_struct *mm)return -ESRCH ;WARN (debug_evictions,"Evicting pid %d" , p->lead_thread->pid); - r =kfd_process_evict_queues (p); + r =kfd_process_evict_queues (p, trigger);kfd_unref_process (p);return r;
drivers/gpu/drm/amd/amdkfd/kfd_priv.h +1 -1
@@ -906,7 +906,7 @@ static inline struct kfd_process_device *kfd_process_device_from_gpuidx( }void kfd_unref_process (struct kfd_process *p); -int kfd_process_evict_queues (struct kfd_process *p); +int kfd_process_evict_queues (struct kfd_process *p,uint32_t trigger);int kfd_process_restore_queues (struct kfd_process *p);void kfd_suspend_all_processes (void );int kfd_resume_all_processes (void );
drivers/gpu/drm/amd/amdkfd/kfd_process.c +33 -3
@@ -43,6 +43,7 @@ struct mm_struct;#include "kfd_dbgmgr.h" #include "kfd_iommu.h" #include "kfd_svm.h" +#include "kfd_smi_events.h" /* * List of struct kfd_process (field kfd_process).
@@ -1701,7 +1702,7 @@ struct kfd_process *kfd_lookup_process_by_mm(const struct mm_struct *mm)* Eviction is reference-counted per process-device. This means multiple * evictions from different sources can be nested safely. */ -int kfd_process_evict_queues (struct kfd_process *p) +int kfd_process_evict_queues (struct kfd_process *p,uint32_t trigger) {int r =0 ;int i;
@@ -1710,6 +1711,9 @@ int kfd_process_evict_queues(struct kfd_process *p)for (i =0 ; i < p->n_pdds; i++) {struct kfd_process_device *pdd = p->pdds[i]; +kfd_smi_event_queue_eviction (pdd->dev, p->pasid,ktime_get_ns (), + trigger); + r = pdd->dev->dqm->ops.evict_process_queues (pdd->dev->dqm, &pdd->qpd);/* evict return -EIO if HWS is hang or asic is resetting, in this case
@@ -1734,6 +1738,10 @@ int kfd_process_evict_queues(struct kfd_process *p)if (n_evicted ==0 )break ; + +kfd_smi_event_queue_eviction_restore (pdd->dev, p->pasid, +ktime_get_ns (), false); +if (pdd->dev->dqm->ops.restore_process_queues (pdd->dev->dqm, &pdd->qpd))pr_err ("Failed to restore queues\n" );
@@ -1753,6 +1761,9 @@ int kfd_process_restore_queues(struct kfd_process *p)for (i =0 ; i < p->n_pdds; i++) {struct kfd_process_device *pdd = p->pdds[i]; +kfd_smi_event_queue_eviction_restore (pdd->dev, p->pasid, +ktime_get_ns (), false); + r = pdd->dev->dqm->ops.restore_process_queues (pdd->dev->dqm, &pdd->qpd);if (r) {
@@ -1765,6 +1776,25 @@ int kfd_process_restore_queues(struct kfd_process *p)return ret; } +void kfd_process_smi_event_restore_rescheduled (struct mm_struct *mm) +{ +struct kfd_process *p; +int i; + + p =kfd_lookup_process_by_mm (mm); +if (!p) +return ; + +for (i =0 ; i < p->n_pdds; i++) { +struct kfd_process_device *pdd = p->pdds[i]; + +kfd_smi_event_queue_eviction_restore (pdd->dev, + pdd->process->pasid, +ktime_get_ns (), true); + } +kfd_unref_process (p); +} +int kfd_process_gpuidx_from_gpuid (struct kfd_process *p,uint32_t gpu_id) {int i;
@@ -1814,7 +1844,7 @@ static void evict_process_worker(struct work_struct *work)flush_delayed_work (&p->restore_work);pr_debug ("Started evicting pasid 0x%x\n" , p->pasid); - ret =kfd_process_evict_queues (p); + ret =kfd_process_evict_queues (p,TTM_EVICTION );if (!ret) {dma_fence_signal (p->ef);dma_fence_put (p->ef);
@@ -1881,7 +1911,7 @@ void kfd_suspend_all_processes(void)cancel_delayed_work_sync (&p->eviction_work);cancel_delayed_work_sync (&p->restore_work); -if (kfd_process_evict_queues (p)) +if (kfd_process_evict_queues (p,SUSPEND_EVICTION ))pr_err ("Failed to suspend process 0x%x\n" , p->pasid);dma_fence_signal (p->ef);dma_fence_put (p->ef);
drivers/gpu/drm/amd/amdkfd/kfd_smi_events.c +35 -0
@@ -330,6 +330,41 @@ void kfd_smi_event_page_fault(struct kfd_dev *dev, uint16_t pasid,add_event_to_kfifo (pid, dev,KFD_SMI_EVENT_PAGE_FAULT , fifo_in, len); } +void kfd_smi_event_queue_eviction (struct kfd_dev *dev,uint16_t pasid, +uint64_t ts,uint32_t trigger) +{ +char fifo_in[64 ]; +pid_t pid; +int len; + +if (!kfd_smi_event_pid_duration (dev, pasid, &pid,0 ,NULL )) +return ; + + len =snprintf (fifo_in,sizeof (fifo_in),"%d ts=%lld pid=%d gpuid=0x%x" +" trigger=%d\n" ,KFD_SMI_EVENT_QUEUE_EVICTION , ts, pid, + dev->id, trigger); + +add_event_to_kfifo (pid, dev,KFD_SMI_EVENT_QUEUE_EVICTION , fifo_in, len); +} + +void kfd_smi_event_queue_eviction_restore (struct kfd_dev *dev,uint16_t pasid, +uint64_t ts,bool rescheduled) +{ +char fifo_in[64 ]; +pid_t pid; +int len; + +if (!kfd_smi_event_pid_duration (dev, pasid, &pid,0 ,NULL )) +return ; + + len =snprintf (fifo_in,sizeof (fifo_in),"%d ts=%lld pid=%d gpuid=0x%x" +" %s\n" ,KFD_SMI_EVENT_QUEUE_EVICTION_RESTORE , ts, pid, + dev->id, rescheduled ?"rescheduled" :"success" ); + +add_event_to_kfifo (pid, dev,KFD_SMI_EVENT_QUEUE_EVICTION_RESTORE , + 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 +4 -0
@@ -36,5 +36,9 @@ void kfd_smi_event_migration(struct kfd_dev *dev, uint16_t pasid,void kfd_smi_event_page_fault (struct kfd_dev *dev,uint16_t pasid,unsigned long address,bool migration,bool write_fault,uint64_t ts); +void kfd_smi_event_queue_eviction (struct kfd_dev *dev,uint16_t pasid, +uint64_t ts,uint32_t trigger); +void kfd_smi_event_queue_eviction_restore (struct kfd_dev *dev,uint16_t pasid, +uint64_t ts,bool rescheduled);#endif
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +4 -2
@@ -1704,14 +1704,16 @@ 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) {pr_debug ("reschedule to restore svm range\n" );schedule_delayed_work (&svms->restore_work,msecs_to_jiffies (AMDGPU_SVM_RANGE_RESTORE_DELAY_MS )); + +kfd_process_smi_event_restore_rescheduled (mm); } +mmput (mm); }/**
@@ -1767,7 +1769,7 @@ svm_range_evict(struct svm_range *prange, struct mm_struct *mm, prange->svms, prange->start, prange->last);/* First eviction, stop the queues */ - r =kgd2kfd_quiesce_mm (mm); + r =kgd2kfd_quiesce_mm (mm,SVM_RANGE_EVICTION );if (r)pr_debug ("failed to quiesce KFD\n" );
Signature · 1 line
2.17.1