drm/amdkfd: don't drop an SVM CWSR reference that was never taken
Subsystems (3)
PATCH 1/4 drm/amdkfd: don't drop an SVM CWSR reference that was never taken
When kfd_queue_acquire_buffers() fails, its error path calls kfd_queue_release_buffers(), which also drops a queue_refcount on the SVM range at ctx_save_restore_area_address. The SVM range is the last buffer acquired, and kfd_queue_buffer_svm_get() takes no reference when it fails, so on every error path this reference was never taken. If another queue of the process uses that CWSR range, its reference is dropped instead, and unmapping the range no longer evicts that queue while its CWSR area is unmapped from the GPU.
On navi31, with queue A using an SVM CWSR range: munmap() of the range logs
Freeing queue vital buffer 0x..., queue evicted
but after a CREATE_QUEUE that reuses A's CWSR address with an unmapped write pointer (-EINVAL), the same munmap() leaves A running without the message.
Only put the BOs on the acquire error path. The warning and the eviction are back after the failed CREATE_QUEUE.
--- drivers/gpu/drm/amd/amdkfd/kfd_queue.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_queue.c +17 -6
@@ -230,6 +230,15 @@ void kfd_queue_buffer_put(struct amdgpu_bo **bo)amdgpu_bo_unref (bo); } +static void kfd_queue_put_bos (struct queue_properties *properties) +{ +kfd_queue_buffer_put (&properties->wptr_bo); +kfd_queue_buffer_put (&properties->rptr_bo); +kfd_queue_buffer_put (&properties->ring_bo); +kfd_queue_buffer_put (&properties->eop_buf_bo); +kfd_queue_buffer_put (&properties->cwsr_bo); +} +int kfd_queue_acquire_buffers (struct kfd_process_device *pdd,struct queue_properties *properties) {struct kfd_topology_device *topo_dev;
@@ -343,7 +352,13 @@ int kfd_queue_acquire_buffers(struct kfd_process_device *pdd, struct queue_prope* dropping the VM reservation. */ kfd_queue_unref_bo_vas (pdd, properties); -kfd_queue_release_buffers (pdd, properties); +/* +* The SVM CWSR range is the last thing acquired and a failed +* kfd_queue_buffer_svm_get() takes nothing, so only the BOs are held +* here. Dropping the SVM reference would drop the one of another queue +* using the same CWSR address. +*/ +kfd_queue_put_bos (properties);return err; }
@@ -352,11 +367,7 @@ int kfd_queue_release_buffers(struct kfd_process_device *pdd, struct queue_propestruct kfd_topology_device *topo_dev;u64 total_cwsr_size; -kfd_queue_buffer_put (&properties->wptr_bo); -kfd_queue_buffer_put (&properties->rptr_bo); -kfd_queue_buffer_put (&properties->ring_bo); -kfd_queue_buffer_put (&properties->eop_buf_bo); -kfd_queue_buffer_put (&properties->cwsr_bo); +kfd_queue_put_bos (properties); topo_dev =kfd_topology_device_by_id (pdd->dev->id);if (!topo_dev)
Signature · 1 line
2.50.1
PATCH 2/4 drm/amdkfd: evict the queues only once for CRIU PROCESS_INFO
criu_process_info() evicts the queues of the process on every call, and each kfd_process_evict_queues() increments qpd->evicted, but CRIU UNPAUSE restores them only once. After PROCESS_INFO twice and UNPAUSE the queues stay evicted for good. On navi31, a barrier-AND packet on an HSA queue completes after PROCESS_INFO + UNPAUSE, but times out after PROCESS_INFO, PROCESS_INFO, UNPAUSE.
The error path has a related problem: when kfd_process_evict_queues() itself fails, it has already rolled back its partial eviction, and the kfd_process_restore_queues() in the error path restores once more.
Evict only when the queues are not paused yet, and on errors only undo an eviction done by this call. The barrier now completes after any number of PROCESS_INFO calls followed by UNPAUSE.
--- drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +10 -5
@@ -2869,6 +2869,7 @@ static int criu_process_info(struct file *filep,struct kfd_process *p, struct kfd_ioctl_criu_args *args) { + bool paused_here = false;int ret =0 ;mutex_lock (&p->mutex);
@@ -2879,11 +2880,15 @@ static int criu_process_info(struct file *filep, goto err_unlock; } - ret =kfd_process_evict_queues (p,KFD_QUEUE_EVICTION_CRIU_CHECKPOINT ); -if (ret) - goto err_unlock; +/* UNPAUSE restores the queues once, so evict them only once */ +if (!p->queues_paused) { + ret =kfd_process_evict_queues (p,KFD_QUEUE_EVICTION_CRIU_CHECKPOINT ); +if (ret) + goto err_unlock; - p->queues_paused = true; + p->queues_paused = true; + paused_here = true; + } args->pid =task_pid_nr_ns (p->lead_thread,task_active_pid_ns (p->lead_thread));
@@ -2898,7 +2903,7 @@ static int criu_process_info(struct file *filep, args->priv_data_size); err_unlock: -if (ret) { +if (ret && paused_here) {kfd_process_restore_queues (p); p->queues_paused = false; }
Signature · 1 line
2.50.1
Re: [PATCH 2/4] drm/amdkfd: evict the queues only once for CRIU PROCESS_INFO
Quoted 6 lines · Jesse Zhang
criu_process_info()evicts the queues of the process on every call, and eachkfd_process_evict_queues()incrementsqpd->evicted, but CRIU UNPAUSE restores them only once. AfterPROCESS_INFOtwice and UNPAUSE the queues stay evicted for good. On navi31, a barrier-AND packet on an HSA queue completes afterPROCESS_INFO+ UNPAUSE, but times out afterPROCESS_INFO,PROCESS_INFO, UNPAUSE.
I'm not disagreeing with the patch. It makes the API more robust. But is this a valid use case? I.e., is CRIU actually calling PROCESS_INFO multiple times? Why?
Quoted 4 lines
The error path has a related problem: when
kfd_process_evict_queues()itself fails, it has already rolled back its partial eviction, and thekfd_process_restore_queues()in the error path restores once more.
I agree. Your patch addresses this as well.
Quoted 7 lines
Evict only when the queues are not paused yet, and on errors only undo an eviction done by this call. The barrier now completes after any number of
PROCESS_INFOcalls followed by UNPAUSE.Fixes cd9f79103003 ("drm/amdkfd: CRIU Implement KFD unpause operation")Signed-off-by Jesse Zhang <[email protected]>
Quoted 45 lines
--- drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-)drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +10
-5@@ -2869,6 +2869,7 @@ static int criu_process_info(struct file *filep,struct kfd_process *p, struct kfd_ioctl_criu_args *args) { + bool paused_here = false;int ret =0 ;mutex_lock (&p->mutex);@@ -2879,11 +2880,15 @@ static int criu_process_info(struct file *filep, goto err_unlock; } - ret =kfd_process_evict_queues (p,KFD_QUEUE_EVICTION_CRIU_CHECKPOINT ); -if (ret) - goto err_unlock; +/* UNPAUSE restores the queues once, so evict them only once */ +if (!p->queues_paused) { + ret =kfd_process_evict_queues (p,KFD_QUEUE_EVICTION_CRIU_CHECKPOINT ); +if (ret) + goto err_unlock; - p->queues_paused = true; + p->queues_paused = true; + paused_here = true; + } args->pid =task_pid_nr_ns (p->lead_thread,task_active_pid_ns (p->lead_thread));@@ -2898,7 +2903,7 @@ static int criu_process_info(struct file *filep, args->priv_data_size); err_unlock: -if (ret) { +if (ret && paused_here) {kfd_process_restore_queues (p); p->queues_paused = false; }
PATCH 3/4 drm/amdkfd: send SMI process and VM fault events only to allowed clients
In reply to Jesse ZhangCommit a403d3f65c79 ("drm/amdkfd: fix SMI event cross-process information leak") made the VMFAULT and PROCESS_START/END SMI events carry the pid of the process they are about, so that kfd_smi_ev_enabled() only passes them to clients of that process and to privileged clients. Commit fd406e58b9e6 ("drm/amdkfd: Fix SMI event PID reporting for containers") then switched kfd_smi_event_add() to take a task and passed NULL for these events, i.e. pid 0, which every client receives again.
On navi31, an unprivileged SMI client (uid 1000) subscribed to PROCESS_START/END sees root running rocminfo:
event: c 223a9 rocminfo
event: d 223a9 rocminfo
The same commit also made the filter compare PIDs of different
namespaces: the client stores its PID in its own namespace and events use the PID of the task in the task's namespace, so a client that is PID 1 in one container receives the events of PID 1 in another. And an exited task has no PID in its namespace anymore, which gives pid 0 for PROCESS_END.
Match clients by global TGID, which is unique and stays valid after the task exited, and keep showing the namespace PID in the event text. Pass the TGID of the process for the process and VM fault events again. With this the unprivileged client gets nothing, a root client still gets both events, and an unprivileged client that is PID 1 in its own PID namespace no longer gets the events of rocminfo running as PID 1 in another one.
--- drivers/gpu/drm/amd/amdkfd/kfd_smi_events.c | 50 ++++++++++++--------- 1 file changed, 30 insertions(+), 20 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_smi_events.c +30 -20
@@ -210,27 +210,33 @@ static inline pid_t kfd_smi_task_to_pid(struct task_struct *task)return task ?task_tgid_nr_ns (task,task_active_pid_ns (task)) :0 ; } +/* +* Clients are matched by global TGID, which is unique across PID namespaces +* and stays valid after the task exited. Events show kfd_smi_task_to_pid(). +*/ +static inline pid_t kfd_smi_task_tgid (struct task_struct *task) +{ +return task ?task_tgid_nr (task) :0 ; +} + __printf(4, 5) -static void kfd_smi_event_add (struct task_struct *task,struct kfd_node *dev, +static void kfd_smi_event_add (pid_t tgid,struct kfd_node *dev,unsigned int event,char *fmt, ...) {char fifo_in[KFD_SMI_EVENT_MSG_SIZE ];int len;va_list args; -pid_t pid;if (list_empty (&dev->smi_clients))return ; - pid =kfd_smi_task_to_pid (task); - len =scnprintf (fifo_in,sizeof (fifo_in),"%x " , event);va_start (args, fmt); len +=vscnprintf (fifo_in + len,sizeof (fifo_in) - len, fmt, args);va_end (args); -add_event_to_kfifo (pid, dev, event, fifo_in, len); +add_event_to_kfifo (tgid, dev, event, fifo_in, len); } void kfd_smi_event_update_gpu_reset(struct kfd_node *dev, bool post_reset,
@@ -252,14 +258,14 @@ void kfd_smi_event_update_gpu_reset(struct kfd_node *dev, bool post_reset,amdgpu_reset_get_desc (reset_context, reset_cause,sizeof (reset_cause)); -kfd_smi_event_add (NULL , dev, event,KFD_EVENT_FMT_UPDATE_GPU_RESET ( +kfd_smi_event_add (0 , dev, event,KFD_EVENT_FMT_UPDATE_GPU_RESET ( dev->reset_seq_num, reset_cause)); }void kfd_smi_event_update_thermal_throttling (struct kfd_node *dev,uint64_t throttle_bitmask) { -kfd_smi_event_add (NULL , dev,KFD_SMI_EVENT_THERMAL_THROTTLE , +kfd_smi_event_add (0 , dev,KFD_SMI_EVENT_THERMAL_THROTTLE ,KFD_EVENT_FMT_THERMAL_THROTTLING ( throttle_bitmask,amdgpu_dpm_get_thermal_throttling_counter (dev->adev)));
@@ -271,10 +277,14 @@ void kfd_smi_event_update_vmfault(struct kfd_node *dev, uint16_t pasid) task_info =amdgpu_vm_get_task_info_pasid (dev->adev, pasid);if (task_info) { -/* Report VM faults from user applications, not retry from kernel */ +/* +* Report VM faults from user applications, not retry from +* kernel, and only to clients allowed to see that process. +*/ if (task_info->task.pid) -kfd_smi_event_add (NULL , dev,KFD_SMI_EVENT_VMFAULT ,KFD_EVENT_FMT_VMFAULT ( - task_info->task.pid, task_info->task.comm)); +kfd_smi_event_add (task_info->tgid, dev,KFD_SMI_EVENT_VMFAULT , +KFD_EVENT_FMT_VMFAULT (task_info->task.pid, + task_info->task.comm));amdgpu_vm_put_task_info (task_info); } }
@@ -283,7 +293,7 @@ void kfd_smi_event_page_fault_start(struct kfd_node *node, struct task_struct *t unsigned long address, bool write_fault, ktime_t ts) { -kfd_smi_event_add (task ,node ,KFD_SMI_EVENT_PAGE_FAULT_START , +kfd_smi_event_add (kfd_smi_task_tgid (task ),node ,KFD_SMI_EVENT_PAGE_FAULT_START ,KFD_EVENT_FMT_PAGEFAULT_START (ktime_to_ns (ts ), kfd_smi_task_to_pid(task), address, node->id, write_fault ? 'W' : 'R'));
@@ -292,7 +302,7 @@ void kfd_smi_event_page_fault_start(struct kfd_node *node, struct task_struct *tvoid kfd_smi_event_page_fault_end (struct kfd_node *node,struct task_struct *task,unsigned long address,bool migration) { -kfd_smi_event_add (task, node,KFD_SMI_EVENT_PAGE_FAULT_END , +kfd_smi_event_add (kfd_smi_task_tgid (task), node,KFD_SMI_EVENT_PAGE_FAULT_END ,KFD_EVENT_FMT_PAGEFAULT_END (ktime_get_boottime_ns (),kfd_smi_task_to_pid (task), address, node->id, migration ?'M' :'U' ));
@@ -304,7 +314,7 @@ void kfd_smi_event_migration_start(struct kfd_node *node, struct task_struct *ta uint32_t prefetch_loc, uint32_t preferred_loc, uint32_t trigger) { -kfd_smi_event_add (task ,node ,KFD_SMI_EVENT_MIGRATE_START , +kfd_smi_event_add (kfd_smi_task_tgid (task ),node ,KFD_SMI_EVENT_MIGRATE_START ,KFD_EVENT_FMT_MIGRATE_START (ktime_get_boottime_ns (), kfd_smi_task_to_pid(task), start, end - start, from,to ,prefetch_loc ,preferred_loc ,trigger ));
@@ -315,7 +325,7 @@ void kfd_smi_event_migration_end(struct kfd_node *node, struct task_struct *task uint32_t from, uint32_t to, uint32_t trigger, int error_code) { -kfd_smi_event_add (task ,node ,KFD_SMI_EVENT_MIGRATE_END , +kfd_smi_event_add (kfd_smi_task_tgid (task ),node ,KFD_SMI_EVENT_MIGRATE_END ,KFD_EVENT_FMT_MIGRATE_END (ktime_get_boottime_ns (), kfd_smi_task_to_pid(task), start, end - start, from,to ,trigger ,error_code ));
@@ -324,14 +334,14 @@ void kfd_smi_event_migration_end(struct kfd_node *node, struct task_struct *taskvoid kfd_smi_event_queue_eviction (struct kfd_node *node,struct task_struct *task,uint32_t trigger) { -kfd_smi_event_add (task, node,KFD_SMI_EVENT_QUEUE_EVICTION , +kfd_smi_event_add (kfd_smi_task_tgid (task), node,KFD_SMI_EVENT_QUEUE_EVICTION ,KFD_EVENT_FMT_QUEUE_EVICTION (ktime_get_boottime_ns (),kfd_smi_task_to_pid (task), node->id, trigger)); }void kfd_smi_event_queue_restore (struct kfd_node *node,struct task_struct *task) { -kfd_smi_event_add (task, node,KFD_SMI_EVENT_QUEUE_RESTORE , +kfd_smi_event_add (kfd_smi_task_tgid (task), node,KFD_SMI_EVENT_QUEUE_RESTORE ,KFD_EVENT_FMT_QUEUE_RESTORE (ktime_get_boottime_ns (),kfd_smi_task_to_pid (task), node->id,'0' )); }
@@ -348,7 +358,7 @@ void kfd_smi_event_queue_restore_rescheduled(struct mm_struct *mm)for (i =0 ; i < p->n_pdds; i++) {struct kfd_process_device *pdd = p->pdds[i]; - kfd_smi_event_add(p->lead_thread, pdd->dev, + kfd_smi_event_add(kfd_smi_task_tgid(p->lead_thread), pdd->dev, KFD_SMI_EVENT_QUEUE_RESTORE, KFD_EVENT_FMT_QUEUE_RESTORE(ktime_get_boottime_ns(), kfd_smi_task_to_pid(p->lead_thread),
@@ -361,7 +371,7 @@ void kfd_smi_event_unmap_from_gpu(struct kfd_node *node, struct task_struct *tas unsigned long address, unsigned long last, uint32_t trigger) { -kfd_smi_event_add (task ,node ,KFD_SMI_EVENT_UNMAP_FROM_GPU , +kfd_smi_event_add (kfd_smi_task_tgid (task ),node ,KFD_SMI_EVENT_UNMAP_FROM_GPU ,KFD_EVENT_FMT_UNMAP_FROM_GPU (ktime_get_boottime_ns (),kfd_smi_task_to_pid (task ),address , last - address + 1, node->id, trigger));
@@ -379,7 +389,7 @@ void kfd_smi_event_process(struct kfd_process_device *pdd, bool start) task_info =amdgpu_vm_get_task_info_vm (avm);if (task_info) { - kfd_smi_event_add(NULL, pdd->dev, + kfd_smi_event_add(kfd_smi_task_tgid(pdd->process->lead_thread), pdd->dev, start ? KFD_SMI_EVENT_PROCESS_START : KFD_SMI_EVENT_PROCESS_END, KFD_EVENT_FMT_PROCESS(task_info->task.pid,
@@ -408,7 +418,7 @@ int kfd_smi_event_open(struct kfd_node *dev, uint32_t *fd)spin_lock_init (&client->lock); client->events =0 ; client->dev = dev; - client->pid =kfd_smi_task_to_pid (current); + client->pid =kfd_smi_task_tgid (current); client->suser =capable (CAP_SYS_ADMIN );spin_lock (&dev->smi_lock);
Signature · 1 line
2.50.1
Felix Kuehling
maintainerRe: [PATCH 3/4] drm/amdkfd: send SMI process and VM fault events only to allowed clients
Quoted 30 lines · Jesse Zhang
Commit
a403d3f65c79("drm/amdkfd: fix SMI event cross-process information leak") made the VMFAULT and PROCESS_START/END SMI events carry the pid of the process they are about, so thatkfd_smi_ev_enabled()only passes them to clients of that process and to privileged clients. Commitfd406e58b9e6("drm/amdkfd: Fix SMI event PID reporting for containers") then switchedkfd_smi_event_add()to take a task and passed NULL for these events, i.e. pid 0, which every client receives again.On navi31, an unprivileged SMI client (uid 1000) subscribed to PROCESS_START/END sees root running rocminfo:
event: c 223a9 rocminfo
event: d 223a9 rocminfoThe same commit also made the filter compare PIDs of different
namespaces: the client stores its PID in its own namespace and events use the PID of the task in the task's namespace, so a client that is PID 1 in one container receives the events of PID 1 in another. And an exited task has no PID in its namespace anymore, which gives pid 0 forPROCESS_END.Match clients by global TGID, which is unique and stays valid after the task exited, and keep showing the namespace PID in the event text. Pass the TGID of the process for the process and VM fault events again. With this the unprivileged client gets nothing, a root client still gets both events, and an unprivileged client that is PID 1 in its own PID namespace no longer gets the events of rocminfo running as PID 1 in another one.
Quoted 1 line
Fixes fd406e58b9e6 ("drm/amdkfd: Fix SMI event PID reporting for containers")
This was a commit by Andrew, who is no longer here. Harish reviewed it at the time. @Harish, can you take a look at this as well?
Quoted 15 lines
Signed-off-by Jesse Zhang <[email protected]>--- drivers/gpu/drm/amd/amdkfd/kfd_smi_events.c | 50 ++++++++++++--------- 1 file changed, 30 insertions(+), 20 deletions(-)drivers/gpu/drm/amd/amdkfd/kfd_smi_events.c +2
-0@@ -210,27 +210,33 @@ static inline pid_t kfd_smi_task_to_pid(struct task_struct *task)return task ?task_tgid_nr_ns (task,task_active_pid_ns (task)) :0 ; } +/* +* Clients are matched by global TGID, which is unique across PID namespaces
drivers/gpu/drm/amd/amdkfd/kfd_smi_events.c static inline pid_t kfd_smi_task_to_pid(struct task_struct *task) · line 223
Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_smi_events.c +8
-1+* and stays valid after the task exited. Events show kfd_smi_task_to_pid(). +*/ +static inline pid_t kfd_smi_task_tgid (struct task_struct *task) +{ +return task ?task_tgid_nr (task) :0 ; +} + __printf(4, 5) -static void kfd_smi_event_add(struct task_struct *task, struct kfd_node *dev, +static void kfd_smi_event_add(pid_t tgid, struct kfd_node *dev,
Please update add_event_to_kfifo and kfd_smi_ev_enabled as well to rename the pid parameter to tgid.
Quoted 139 lines
drivers/gpu/drm/amd/amdkfd/kfd_smi_events.c +19
-18unsigned int event, char *fmt, ...) { char fifo_in[KFD_SMI_EVENT_MSG_SIZE];int len;va_list args; -pid_t pid;if (list_empty (&dev->smi_clients))return ; - pid =kfd_smi_task_to_pid (task); - len =scnprintf (fifo_in,sizeof (fifo_in),"%x " , event);va_start (args, fmt); len +=vscnprintf (fifo_in + len,sizeof (fifo_in) - len, fmt, args);va_end (args); -add_event_to_kfifo (pid, dev, event, fifo_in, len); +add_event_to_kfifo (tgid, dev, event, fifo_in, len); } void kfd_smi_event_update_gpu_reset(struct kfd_node *dev, bool post_reset,@@ -252,14 +258,14 @@ void kfd_smi_event_update_gpu_reset(struct kfd_node *dev, bool post_reset,amdgpu_reset_get_desc (reset_context, reset_cause,sizeof (reset_cause)); -kfd_smi_event_add (NULL , dev, event,KFD_EVENT_FMT_UPDATE_GPU_RESET ( +kfd_smi_event_add (0 , dev, event,KFD_EVENT_FMT_UPDATE_GPU_RESET ( dev->reset_seq_num, reset_cause)); }void kfd_smi_event_update_thermal_throttling (struct kfd_node *dev,uint64_t throttle_bitmask) { -kfd_smi_event_add (NULL , dev,KFD_SMI_EVENT_THERMAL_THROTTLE , +kfd_smi_event_add (0 , dev,KFD_SMI_EVENT_THERMAL_THROTTLE ,KFD_EVENT_FMT_THERMAL_THROTTLING ( throttle_bitmask,amdgpu_dpm_get_thermal_throttling_counter (dev->adev)));@@ -271,10 +277,14 @@ void kfd_smi_event_update_vmfault(struct kfd_node *dev, uint16_t pasid) task_info =amdgpu_vm_get_task_info_pasid (dev->adev, pasid);if (task_info) { -/* Report VM faults from user applications, not retry from kernel */ +/* +* Report VM faults from user applications, not retry from +* kernel, and only to clients allowed to see that process. +*/ if (task_info->task.pid) -kfd_smi_event_add (NULL , dev,KFD_SMI_EVENT_VMFAULT ,KFD_EVENT_FMT_VMFAULT ( - task_info->task.pid, task_info->task.comm)); +kfd_smi_event_add (task_info->tgid, dev,KFD_SMI_EVENT_VMFAULT , +KFD_EVENT_FMT_VMFAULT (task_info->task.pid, + task_info->task.comm));amdgpu_vm_put_task_info (task_info); } }@@ -283,7 +293,7 @@ void kfd_smi_event_page_fault_start(struct kfd_node *node, struct task_struct *t unsigned long address, bool write_fault, ktime_t ts) { -kfd_smi_event_add (task ,node ,KFD_SMI_EVENT_PAGE_FAULT_START , +kfd_smi_event_add (kfd_smi_task_tgid (task ),node ,KFD_SMI_EVENT_PAGE_FAULT_START ,KFD_EVENT_FMT_PAGEFAULT_START (ktime_to_ns (ts ), kfd_smi_task_to_pid(task), address, node->id, write_fault ? 'W' : 'R'));@@ -292,7 +302,7 @@ void kfd_smi_event_page_fault_start(struct kfd_node *node, struct task_struct *tvoid kfd_smi_event_page_fault_end (struct kfd_node *node,struct task_struct *task,unsigned long address,bool migration) { -kfd_smi_event_add (task, node,KFD_SMI_EVENT_PAGE_FAULT_END , +kfd_smi_event_add (kfd_smi_task_tgid (task), node,KFD_SMI_EVENT_PAGE_FAULT_END ,KFD_EVENT_FMT_PAGEFAULT_END (ktime_get_boottime_ns (),kfd_smi_task_to_pid (task), address, node->id, migration ?'M' :'U' ));@@ -304,7 +314,7 @@ void kfd_smi_event_migration_start(struct kfd_node *node, struct task_struct *ta uint32_t prefetch_loc, uint32_t preferred_loc, uint32_t trigger) { -kfd_smi_event_add (task ,node ,KFD_SMI_EVENT_MIGRATE_START , +kfd_smi_event_add (kfd_smi_task_tgid (task ),node ,KFD_SMI_EVENT_MIGRATE_START ,KFD_EVENT_FMT_MIGRATE_START (ktime_get_boottime_ns (), kfd_smi_task_to_pid(task), start, end - start, from,to ,prefetch_loc ,preferred_loc ,trigger ));@@ -315,7 +325,7 @@ void kfd_smi_event_migration_end(struct kfd_node *node, struct task_struct *task uint32_t from, uint32_t to, uint32_t trigger, int error_code) { -kfd_smi_event_add (task ,node ,KFD_SMI_EVENT_MIGRATE_END , +kfd_smi_event_add (kfd_smi_task_tgid (task ),node ,KFD_SMI_EVENT_MIGRATE_END ,KFD_EVENT_FMT_MIGRATE_END (ktime_get_boottime_ns (), kfd_smi_task_to_pid(task), start, end - start, from,to ,trigger ,error_code ));@@ -324,14 +334,14 @@ void kfd_smi_event_migration_end(struct kfd_node *node, struct task_struct *taskvoid kfd_smi_event_queue_eviction (struct kfd_node *node,struct task_struct *task,uint32_t trigger) { -kfd_smi_event_add (task, node,KFD_SMI_EVENT_QUEUE_EVICTION , +kfd_smi_event_add (kfd_smi_task_tgid (task), node,KFD_SMI_EVENT_QUEUE_EVICTION ,KFD_EVENT_FMT_QUEUE_EVICTION (ktime_get_boottime_ns (),kfd_smi_task_to_pid (task), node->id, trigger)); }void kfd_smi_event_queue_restore (struct kfd_node *node,struct task_struct *task) { -kfd_smi_event_add (task, node,KFD_SMI_EVENT_QUEUE_RESTORE , +kfd_smi_event_add (kfd_smi_task_tgid (task), node,KFD_SMI_EVENT_QUEUE_RESTORE ,KFD_EVENT_FMT_QUEUE_RESTORE (ktime_get_boottime_ns (),kfd_smi_task_to_pid (task), node->id,'0' )); }@@ -348,7 +358,7 @@ void kfd_smi_event_queue_restore_rescheduled(struct mm_struct *mm)for (i =0 ; i < p->n_pdds; i++) {struct kfd_process_device *pdd = p->pdds[i]; - kfd_smi_event_add(p->lead_thread, pdd->dev, + kfd_smi_event_add(kfd_smi_task_tgid(p->lead_thread), pdd->dev, KFD_SMI_EVENT_QUEUE_RESTORE, KFD_EVENT_FMT_QUEUE_RESTORE(ktime_get_boottime_ns(), kfd_smi_task_to_pid(p->lead_thread),@@ -361,7 +371,7 @@ void kfd_smi_event_unmap_from_gpu(struct kfd_node *node, struct task_struct *tas unsigned long address, unsigned long last, uint32_t trigger) { -kfd_smi_event_add (task ,node ,KFD_SMI_EVENT_UNMAP_FROM_GPU , +kfd_smi_event_add (kfd_smi_task_tgid (task ),node ,KFD_SMI_EVENT_UNMAP_FROM_GPU ,KFD_EVENT_FMT_UNMAP_FROM_GPU (ktime_get_boottime_ns (),kfd_smi_task_to_pid (task ),address , last - address + 1, node->id, trigger));@@ -379,7 +389,7 @@ void kfd_smi_event_process(struct kfd_process_device *pdd, bool start) task_info =amdgpu_vm_get_task_info_vm (avm);if (task_info) { - kfd_smi_event_add(NULL, pdd->dev, + kfd_smi_event_add(kfd_smi_task_tgid(pdd->process->lead_thread), pdd->dev, start ? KFD_SMI_EVENT_PROCESS_START : KFD_SMI_EVENT_PROCESS_END, KFD_EVENT_FMT_PROCESS(task_info->task.pid,
drivers/gpu/drm/amd/amdkfd/kfd_smi_events.c int kfd_smi_event_open(struct kfd_node *dev, uint32_t *fd) · line 421
Quoted 5 lines
drivers/gpu/drm/amd/amdkfd/kfd_smi_events.c +1
-1@@ -408,7 +418,7 @@ int kfd_smi_event_open(struct kfd_node *dev, uint32_t *fd)spin_lock_init (&client->lock); client->events =0 ; client->dev = dev; - client->pid =kfd_smi_task_to_pid (current); + client->pid =kfd_smi_task_tgid (current);
Also rename client->pid to client->tgid to be consistent and make it obvious what kind of ID is being compared everywhere in the code.
Regards,
Felix
Quoted 3 lines
drivers/gpu/drm/amd/amdkfd/kfd_smi_events.c +0
-0client->suser =capable (CAP_SYS_ADMIN );spin_lock (&dev->smi_lock);
PATCH 4/4 drm/amdkfd: split vram_pages of SVM ranges prefetched without a GPU mapping
In reply to Jesse ZhangWhen an SVM range is split, svm_range_split_pages() gives the new range the number of VRAM pages counted in its part of the dma_addr arrays and subtracts it from the old range. Pages migrated to VRAM by a prefetch have no dma_addr entries until the range is mapped on a GPU, e.g. with no ACCESS attribute or before the first fault, so the new range gets 0 and the old one keeps the count for both.
Migrating the parts back to system memory then gets the accounting
wrong: the old part keeps a VRAM page count after all its pages are gone, so its svm_bo is never freed, and the new part underflows. On navi31, as an unprivileged process: prefetch 4 MiB to the GPU, prefetch the upper 2 MiB and then the lower 2 MiB back to system memory:
Recorded vram pages(0x0) should not be less than migration pages(0x200).
WARNING: ../amdkfd/kfd_migrate.c:922 at svm_migrate_vram_to_ram+0xbdc/0xcd0 [amdgpu]
and /sys/class/kfd/kfd/proc/<pid>/vram_<gpu_id> stays at 4194304 until the process exits.
Without dma_addr arrays, the split is still exact when the old range is completely in VRAM, which is what a prefetch of the whole range leaves. Give the new range all its pages in that case. The warning is gone and the VRAM usage drops back to 0.
--- drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +11 -1
@@ -1020,9 +1020,12 @@ svm_range_split_pages(struct svm_range *new, struct svm_range *old, uint64_t start, uint64_t last) { uint64_t npages = last - start + 1; +bool counted = false;int i, r; for (i = 0; i < MAX_GPU_INSTANCE; i++) { + if (old->dma_addr[i]) + counted = true; r = svm_range_split_array(&new->dma_addr[i], &old->dma_addr[i], sizeof(*old->dma_addr[i]), old->start, npages, new->start, new->npages,
@@ -1030,8 +1033,15 @@ svm_range_split_pages(struct svm_range *new, struct svm_range *old,if (r)return r; } -if (old->actual_loc) +if (old->actual_loc) { +/* +* Pages migrated by prefetch have no dma_addr until they are +* mapped on a GPU. A fully resident range still splits exactly. +*/ +if (!counted && old->vram_pages == npages + new->npages) + new->vram_pages = new->npages; old->vram_pages -= new->vram_pages; + }return 0 ; }
Signature · 1 line
2.50.1
Re: [PATCH 4/4] drm/amdkfd: split vram_pages of SVM ranges prefetched without a GPU mapping
Quoted 49 lines · Jesse Zhang
When an SVM range is split,
svm_range_split_pages()gives the new range the number of VRAM pages counted in its part of thedma_addrarrays and subtracts it from the old range. Pages migrated to VRAM by a prefetch have nodma_addrentries until the range is mapped on a GPU, e.g. with no ACCESS attribute or before the first fault, so the new range gets 0 and the old one keeps the count for both.Migrating the parts back to system memory then gets the accounting
wrong: the old part keeps a VRAM page count after all its pages are gone, so itssvm_bois never freed, and the new part underflows. On navi31, as an unprivileged process: prefetch 4 MiB to the GPU, prefetch the upper 2 MiB and then the lower 2 MiB back to system memory:Recorded vram pages(0x0) should not be less than migration pages(0x200).
WARNING: ../amdkfd/kfd_migrate.c:922 at svm_migrate_vram_to_ram+0xbdc/0xcd0 [amdgpu]and/sys/class/kfd/kfd/proc/<pid>/vram_<gpu_id> stays at 4194304 until the process exits.
Without
dma_addrarrays, the split is still exact when the old range is completely in VRAM, which is what a prefetch of the whole range leaves. Give the new range all its pages in that case. The warning is gone and the VRAM usage drops back to 0.Fixes 006ad514a50c ("drm/amdkfd: Use partial hmm page walk during buffer validation in SVM")Signed-off-by Jesse Zhang<[email protected]>--- drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)drivers/gpu/drm/amd/amdkfd/kfd_svm.c +3
-0@@ -1020,9 +1020,12 @@ svm_range_split_pages(struct svm_range *new, struct svm_range *old, uint64_t start, uint64_t last) { uint64_t npages = last - start + 1; +bool counted = false;int i, r; for (i = 0; i < MAX_GPU_INSTANCE; i++) { + if (old->dma_addr[i]) + counted = true; r = svm_range_split_array(&new->dma_addr[i], &old->dma_addr[i], sizeof(*old->dma_addr[i]), old->start, npages, new->start, new->npages,@@ -1030,8 +1033,15 @@ svm_range_split_pages(struct svm_range *new, struct svm_range *old,if (r)
drivers/gpu/drm/amd/amdkfd/kfd_svm.c svm_range_split_pages(struct svm_range *new, struct svm_range *old, · line 1042
Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +7
-1return r; } -if (old->actual_loc) +if (old->actual_loc) { +/* +* Pages migrated by prefetch have no dma_addr until they are +* mapped on a GPU. A fully resident range still splits exactly. +*/ +if (!counted && old->vram_pages == npages + new->npages) + new->vram_pages = new->npages;
With XNACK off, svm_range_set_attr() leaves the GPU mapping update to the delayed restore worker rather than doing it inline, so the dma_addr arrays are not populated yet. The bug is a timing window: the prefetch back to system memory happens before the scheduled worker gets to run.
The fix, though, assumes prange->vram_pages == npages. That does not hold for a partial migration — svm_migrate_ram_to_vram() only adds the pages it actually migrated, so the range can be partly resident in VRAM with no dma_addr array, and the accounting is still wrong in that case.
Fixing it completely means redesigning the per-range VRAM page accounting so residency is tracked per page independently of dma_addr. That is a much bigger change, and this simple solution is good enough to fix the bug in front of us, so let's go with it. Two suggestions:
Rename bool counted to has_dma_addr
if (!has_dma_addr && old->vram_pages == old->npages) new->vram_pages = new->npages;
Please also mention the partial-migration limitation in the commit message.
Regards,
Philip
Quoted 6 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +1
-1old->vram_pages -= new->vram_pages; + }return 0 ; } -- 2.50.1
Quoted 18 lines · Jesse Zhang
When
kfd_queue_acquire_buffers()fails, its error path callskfd_queue_release_buffers(), which also drops aqueue_refcounton the SVM range atctx_save_restore_area_address. The SVM range is the last buffer acquired, andkfd_queue_buffer_svm_get()takes no reference when it fails, so on every error path this reference was never taken. If another queue of the process uses that CWSR range, its reference is dropped instead, and unmapping the range no longer evicts that queue while its CWSR area is unmapped from the GPU.On navi31, with queue A using an SVM CWSR range:
munmap()of the range logsFreeing queue vital buffer 0x..., queue evicted
but after a
CREATE_QUEUEthat reuses A's CWSR address with an unmapped write pointer (-EINVAL), the samemunmap()leaves A running without the message.
Quoted 5 lines
Only put the BOs on the acquire error path. The warning and the eviction are back after the failed
CREATE_QUEUE.Fixes b049504e211e ("drm/amdkfd: Validate user queue svm memory residency")Signed-off-by Jesse Zhang <[email protected]>
Quoted 52 lines
--- drivers/gpu/drm/amd/amdkfd/kfd_queue.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-)drivers/gpu/drm/amd/amdkfd/kfd_queue.c +17
-6@@ -230,6 +230,15 @@ void kfd_queue_buffer_put(struct amdgpu_bo **bo)amdgpu_bo_unref (bo); } +static void kfd_queue_put_bos (struct queue_properties *properties) +{ +kfd_queue_buffer_put (&properties->wptr_bo); +kfd_queue_buffer_put (&properties->rptr_bo); +kfd_queue_buffer_put (&properties->ring_bo); +kfd_queue_buffer_put (&properties->eop_buf_bo); +kfd_queue_buffer_put (&properties->cwsr_bo); +} +int kfd_queue_acquire_buffers (struct kfd_process_device *pdd,struct queue_properties *properties) {struct kfd_topology_device *topo_dev;@@ -343,7 +352,13 @@ int kfd_queue_acquire_buffers(struct kfd_process_device *pdd, struct queue_prope* dropping the VM reservation. */ kfd_queue_unref_bo_vas (pdd, properties); -kfd_queue_release_buffers (pdd, properties); +/* +* The SVM CWSR range is the last thing acquired and a failed +* kfd_queue_buffer_svm_get() takes nothing, so only the BOs are held +* here. Dropping the SVM reference would drop the one of another queue +* using the same CWSR address. +*/ +kfd_queue_put_bos (properties);return err; }@@ -352,11 +367,7 @@ int kfd_queue_release_buffers(struct kfd_process_device *pdd, struct queue_propestruct kfd_topology_device *topo_dev;u64 total_cwsr_size; -kfd_queue_buffer_put (&properties->wptr_bo); -kfd_queue_buffer_put (&properties->rptr_bo); -kfd_queue_buffer_put (&properties->ring_bo); -kfd_queue_buffer_put (&properties->eop_buf_bo); -kfd_queue_buffer_put (&properties->cwsr_bo); +kfd_queue_put_bos (properties); topo_dev =kfd_topology_device_by_id (pdd->dev->id);if (!topo_dev)