drm/amdkfd: fix CREATE_QUEUE on a released process
Subsystems (3)
PATCH 1/2 drm/amdkfd: fix CREATE_QUEUE on a released process
After execve() by the group leader, an open /dev/kfd fd stays valid, but the MMU notifier release has already destroyed the process queue manager and set p->mm to NULL. kfd_ioctl() only checks the group leader, so AMDKFD_IOC_CREATE_QUEUE reaches find_available_queue_slot() with a NULL queue_slot_bitmap and oopses with p->mutex held. The mutex is never released, and the release worker can block on it.
Refuse ioctls and mmap on a process whose mm has been released. Take p->mutex in the release path around the queue teardown and the p->mm = NULL store. CREATE_QUEUE re-checks p->mm under the mutex, so a queue cannot be added after the teardown. pqm_create_queue() also returns -ENODEV when the slot bitmap is gone, which covers both the normal slot allocation and the CRIU restore path.
--- drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 26 +++++++++++++++++++ drivers/gpu/drm/amd/amdkfd/kfd_process.c | 13 +++++++++- .../amd/amdkfd/kfd_process_queue_manager.c | 7 +++++ 3 files changed, 45 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +26 -0
@@ -401,6 +401,17 @@ static int kfd_ioctl_create_queue(struct file *filep, struct kfd_process *p,mutex_lock (&p->mutex); +/* +* The MMU notifier release destroys the queue manager and clears p->mm +* under p->mutex. The p->mm check in kfd_ioctl() is done without the +* lock, so the release may have run since then. Check again with the +* lock held and refuse to create a queue on a released process. +*/ +if (!p->mm) { + err = -ENODEV ; + goto err_pdd; + } + pdd =kfd_process_device_data_by_id (p, args->gpu_id);if (!pdd) {pr_debug ("Could not find gpu id 0x%x\n" , args->gpu_id);
@@ -3740,6 +3751,17 @@ static long kfd_ioctl(struct file *filep, unsigned int cmd, unsigned long arg) goto err_i1; } +/* +* The address space of this process has been torn down (MMU notifier +* release) and its queue manager has been destroyed. The fd can stay +* open across execve() of the group leader, so refuse the ioctl. +*/ +if (!READ_ONCE (process->mm)) { +dev_dbg (kfd_device,"KFD process address space already released\n" ); + retcode = -ENODEV ; + goto err_i1; + } +/* Do not trust userspace, use our own definition */ func = ioctl->func;
@@ -3860,6 +3882,10 @@ static int kfd_mmap(struct file *filep, struct vm_area_struct *vma)if (process->lead_thread != current->group_leader)return -EBADF ; +/* Same as kfd_ioctl(): the context is dead once its mm is released */ +if (!READ_ONCE (process->mm)) +return -ENODEV ; + mmap_offset = vma->vm_pgoff <<PAGE_SHIFT ; gpu_id =KFD_MMAP_GET_GPU_ID (mmap_offset);
drivers/gpu/drm/amd/amdkfd/kfd_process.c +12 -1
@@ -1500,6 +1500,15 @@ void kfd_process_notifier_release_internal(struct kfd_process *p)srcu_read_unlock (&kfd_processes_srcu, idx); } +/* +* Hold p->mutex so that an ioctl on a file that survived execve() of +* the group leader (e.g. CREATE_QUEUE) either completes before the +* queues are torn down or sees p->mm == NULL afterwards. Otherwise it +* could use the queue manager that pqm_uninit() destroys, or add a +* queue that nothing will ever remove. +*/ +mutex_lock (&p->mutex); +/* * Dequeue and destroy user queues, it is not safe for GPU to access * system memory after mmu release notifier callback returns because
@@ -1517,7 +1526,9 @@ void kfd_process_notifier_release_internal(struct kfd_process *p) }/* Indicate to other users that MM is no longer valid */ - p->mm =NULL ; +WRITE_ONCE (p->mm,NULL ); + +mutex_unlock (&p->mutex);if (p->context_id ==KFD_CONTEXT_ID_PRIMARY )mmu_notifier_put (&p->mmu_notifier);
drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c +7 -0
@@ -378,6 +378,13 @@ int pqm_create_queue(struct process_queue_manager *pqm,if (pdd->qpd.queue_count >= max_queues)return -ENOSPC ; +/* +* pqm_uninit() frees the queue slot bitmap when the process address +* space is released. Do not touch the bitmap after that. +*/ +if (!pqm->queue_slot_bitmap) +return -ENODEV ; +if (q_data) { retval =assign_queue_slot_by_qid (pqm, q_data->q_id); *qid = q_data->q_id;
Signature · 1 line
2.43.0