drm/amdkfd: Acquire buffers in CRIU queue restore (again)
Subsystems (3)
PATCH v4 1/2 drm/amdkfd: Acquire buffers in CRIU queue restore (again)
CRIU queue restore doesn't acquire the bo buffers in the same way normal queue creation does. In normal queue creation, the driver gets an additional reference on the queue's underlying buffers that lasts until the queue is destroyed; this prevents the queue's buffers from being removed from underneath it.
Acquire them, but don't get the svm bo. SVM isn't yet fully restored by this point in CRIU and so there's no svm range to get. This will be addressed in the next patch.
v2: Don't use checkpoint ctl_stack_size
--- drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 2 +- drivers/gpu/drm/amd/amdkfd/kfd_priv.h | 2 +- drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c | 8 ++++++++ drivers/gpu/drm/amd/amdkfd/kfd_queue.c | 8 +++++++- 4 files changed, 17 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +1 -1
@@ -433,7 +433,7 @@ static int kfd_ioctl_create_queue(struct file *filep, struct kfd_process *p, goto err_bind_process; } - err =kfd_queue_acquire_buffers (pdd, &q_properties); + err =kfd_queue_acquire_buffers (pdd, &q_properties, false);if (err) {pr_debug ("failed to acquire user queue buffers\n" ); goto err_acquire_queue_buf;
drivers/gpu/drm/amd/amdkfd/kfd_priv.h +1 -1
@@ -1381,7 +1381,7 @@ void print_queue(struct queue *q);int kfd_queue_buffer_get (struct amdgpu_vm *vm,void __user *addr,struct amdgpu_bo **pbo,u64 expected_size);void kfd_queue_buffer_put (struct amdgpu_bo **bo); -int kfd_queue_acquire_buffers (struct kfd_process_device *pdd,struct queue_properties *properties); +int kfd_queue_acquire_buffers (struct kfd_process_device *pdd,struct queue_properties *properties,bool is_criu);int kfd_queue_release_buffers (struct kfd_process_device *pdd,struct queue_properties *properties);void kfd_queue_unref_bo_va (struct amdgpu_vm *vm,struct amdgpu_bo **bo); int kfd_queue_unref_bo_vas(struct kfd_process_device *pdd,
drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c +8 -0
@@ -1095,9 +1095,17 @@ int kfd_criu_restore_queue(struct kfd_process *p,print_queue_properties (&qp); + ret =kfd_queue_acquire_buffers (pdd, &qp, true); +if (ret) { +pr_err ("Failed to acquire user queue buffers err:%d\n" , ret); + goto exit; + } + ret =pqm_create_queue (&p->pqm, pdd->dev, &qp, &queue_id, q_data, mqd, ctl_stack,NULL );if (ret) {pr_err ("Failed to create new queue err:%d\n" , ret); +kfd_queue_unref_bo_vas (pdd, &qp); +kfd_queue_release_buffers (pdd, &qp); goto exit; }
drivers/gpu/drm/amd/amdkfd/kfd_queue.c +7 -1
@@ -239,7 +239,7 @@ static void kfd_queue_put_bos(struct queue_properties *properties)kfd_queue_buffer_put (&properties->cwsr_bo); } -int kfd_queue_acquire_buffers (struct kfd_process_device *pdd,struct queue_properties *properties) +int kfd_queue_acquire_buffers (struct kfd_process_device *pdd,struct queue_properties *properties,bool is_criu) {struct kfd_topology_device *topo_dev;u64 expected_queue_size;
@@ -334,6 +334,12 @@ int kfd_queue_acquire_buffers(struct kfd_process_device *pdd, struct queue_propeamdgpu_bo_unreserve (vm->root.bo); +/* CRIU svm buffers are restored at a later stage than queues. They'll be in +* before the queue is actually used, so no risk here +*/ +if (is_criu) +return 0 ; + err =kfd_queue_buffer_svm_get (pdd, properties->ctx_save_restore_area_address, total_cwsr_size);if (err)
Signature · 1 line
2.34.1
PATCH v4 2/2 drm/amdkfd: Acquire cwsr buffer during CRIU resume
In normal kfd queue create, a reference to the cwsr svm range is acquired so that this buffer is not freed from underneath the queue. We weren't doing that.
After svm ranges are restored in RESUME phase of CRIU, go through all the queues and acquire a reference to the cwsr.
--- drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 2 ++ drivers/gpu/drm/amd/amdkfd/kfd_priv.h | 3 ++ .../amd/amdkfd/kfd_process_queue_manager.c | 28 +++++++++++++++++++ drivers/gpu/drm/amd/amdkfd/kfd_queue.c | 4 +-- 4 files changed, 35 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +2 -0
@@ -2861,6 +2861,8 @@ static int criu_resume(struct file *filep, goto exit; } +kfd_criu_resume_queues (target); + ret =amdgpu_amdkfd_criu_resume (target->kgd_process_info);if (ret)pr_err ("amdgpu_amdkfd_criu_resume failed for %i\n" , args->pid);
drivers/gpu/drm/amd/amdkfd/kfd_priv.h +3 -0
@@ -1371,6 +1371,8 @@ int kfd_criu_restore_event(struct file *devkfd,uint8_t __user *user_priv_data, uint64_t *priv_data_offset, uint64_t max_priv_data_size); + +int kfd_criu_resume_queues (struct kfd_process *p);/* CRIU - End */ /* Queue Context Management */
@@ -1387,6 +1389,7 @@ void kfd_queue_unref_bo_va(struct amdgpu_vm *vm, struct amdgpu_bo **bo);int kfd_queue_unref_bo_vas (struct kfd_process_device *pdd,struct queue_properties *properties);void kfd_queue_ctx_save_restore_size (struct kfd_topology_device *dev); +int kfd_queue_buffer_svm_get (struct kfd_process_device *pdd,u64 addr,u64 size);struct mqd_manager *mqd_manager_init_cik (enum KFD_MQD_TYPE type,struct kfd_node *dev);
drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c +28 -0
@@ -1124,6 +1124,34 @@ int kfd_criu_restore_queue(struct kfd_process *p,return ret; } +int kfd_criu_resume_queues (struct kfd_process *p) +{ +int ret, i; +u64 total_cwsr_size; + +for (i =0 ; i < p->n_pdds; i++) { +struct kfd_process_device *pdd = p->pdds[i]; +struct kfd_topology_device *topo_dev; +struct queue *q; + + topo_dev =kfd_topology_device_by_id (pdd->dev->id); + +list_for_each_entry (q, &pdd->qpd.queues_list, list) { + total_cwsr_size = q->properties.ctx_save_restore_area_size + + topo_dev->node_props.debug_memory_size; + total_cwsr_size =ALIGN (total_cwsr_size,PAGE_SIZE ); + + ret =kfd_queue_buffer_svm_get (pdd, q->properties.ctx_save_restore_area_address, + total_cwsr_size); +if (ret) +return ret; + } + + } + +return 0 ; +} + int pqm_get_queue_checkpoint_info(struct process_queue_manager *pqm, unsigned int qid, uint32_t *mqd_size,
drivers/gpu/drm/amd/amdkfd/kfd_queue.c +2 -2
@@ -88,7 +88,7 @@ void uninit_queue(struct queue *q)#if IS_ENABLED (CONFIG_HSA_AMD_SVM ) -static int kfd_queue_buffer_svm_get (struct kfd_process_device *pdd,u64 addr,u64 size) +int kfd_queue_buffer_svm_get (struct kfd_process_device *pdd,u64 addr,u64 size) {struct kfd_process *p = pdd->process;struct list_head update_list;
@@ -182,7 +182,7 @@ static void kfd_queue_buffer_svm_put(struct kfd_process_device *pdd, u64 addr, u }#else -static int kfd_queue_buffer_svm_get (struct kfd_process_device *pdd,u64 addr,u64 size) +int kfd_queue_buffer_svm_get (struct kfd_process_device *pdd,u64 addr,u64 size) {return -EINVAL ; }
Signature · 1 line
2.34.1
Re: [PATCH v4 2/2] drm/amdkfd: Acquire cwsr buffer during CRIU resume
Quoted 63 lines · David Francis
In normal kfd queue create, a reference to the cwsr svm range is acquired so that this buffer is not freed from underneath the queue. We weren't doing that.
After svm ranges are restored in RESUME phase of CRIU, go through all the queues and acquire a reference to the cwsr.
Signed-off-by David Francis <[email protected]>--- drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 2 ++ drivers/gpu/drm/amd/amdkfd/kfd_priv.h | 3 ++ .../amd/amdkfd/kfd_process_queue_manager.c | 28 +++++++++++++++++++ drivers/gpu/drm/amd/amdkfd/kfd_queue.c | 4 +-- 4 files changed, 35 insertions(+), 2 deletions(-)drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +2
-0@@ -2861,6 +2861,8 @@ static int criu_resume(struct file *filep, goto exit; } +kfd_criu_resume_queues (target); + ret =amdgpu_amdkfd_criu_resume (target->kgd_process_info);if (ret)pr_err ("amdgpu_amdkfd_criu_resume failed for %i\n" , args->pid);drivers/gpu/drm/amd/amdkfd/kfd_priv.h +3
-0@@ -1371,6 +1371,8 @@ int kfd_criu_restore_event(struct file *devkfd,uint8_t __user *user_priv_data, uint64_t *priv_data_offset, uint64_t max_priv_data_size); + +int kfd_criu_resume_queues (struct kfd_process *p);/* CRIU - End */ /* Queue Context Management */ @@ -1387,6 +1389,7 @@ void kfd_queue_unref_bo_va(struct amdgpu_vm *vm, struct amdgpu_bo **bo);int kfd_queue_unref_bo_vas (struct kfd_process_device *pdd,struct queue_properties *properties);void kfd_queue_ctx_save_restore_size (struct kfd_topology_device *dev); +int kfd_queue_buffer_svm_get (struct kfd_process_device *pdd,u64 addr,u64 size);struct mqd_manager *mqd_manager_init_cik (enum KFD_MQD_TYPE type,struct kfd_node *dev);drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c +6
-0@@ -1124,6 +1124,34 @@ int kfd_criu_restore_queue(struct kfd_process *p,return ret; } +int kfd_criu_resume_queues (struct kfd_process *p) +{ +int ret, i; +u64 total_cwsr_size; + +for (i =0 ; i < p->n_pdds; i++) {
drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c int kfd_criu_restore_queue(struct kfd_process *p, · line 1142
Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c +10
-0+struct kfd_process_device *pdd = p->pdds[i]; +struct kfd_topology_device *topo_dev; +struct queue *q; + + topo_dev =kfd_topology_device_by_id (pdd->dev->id); + +list_for_each_entry (q, &pdd->qpd.queues_list, list) { + total_cwsr_size = q->properties.ctx_save_restore_area_size + + topo_dev->node_props.debug_memory_size; + total_cwsr_size =ALIGN (total_cwsr_size,PAGE_SIZE );
Compared to kfd_queue_acquire_buffers this is missing a multiplication with the number of XCCs in the compute partition. With that fixed, this patch is
Quoted 37 lines
drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c +12
-0+ + ret =kfd_queue_buffer_svm_get (pdd, q->properties.ctx_save_restore_area_address, + total_cwsr_size); +if (ret) +return ret; + } + + } + +return 0 ; +} + int pqm_get_queue_checkpoint_info(struct process_queue_manager *pqm, unsigned int qid, uint32_t *mqd_size,drivers/gpu/drm/amd/amdkfd/kfd_queue.c +2
-2@@ -88,7 +88,7 @@ void uninit_queue(struct queue *q)#if IS_ENABLED (CONFIG_HSA_AMD_SVM ) -static int kfd_queue_buffer_svm_get (struct kfd_process_device *pdd,u64 addr,u64 size) +int kfd_queue_buffer_svm_get (struct kfd_process_device *pdd,u64 addr,u64 size) {struct kfd_process *p = pdd->process;struct list_head update_list;@@ -182,7 +182,7 @@ static void kfd_queue_buffer_svm_put(struct kfd_process_device *pdd, u64 addr, u }#else -static int kfd_queue_buffer_svm_get (struct kfd_process_device *pdd,u64 addr,u64 size) +int kfd_queue_buffer_svm_get (struct kfd_process_device *pdd,u64 addr,u64 size) {return -EINVAL ; }
Kuehling, Felix
maintainerQuoted 55 lines · David Francis
CRIU queue restore doesn't acquire the bo buffers in the same way normal queue creation does. In normal queue creation, the driver gets an additional reference on the queue's underlying buffers that lasts until the queue is destroyed; this prevents the queue's buffers from being removed from underneath it.
Acquire them, but don't get the svm bo. SVM isn't yet fully restored by this point in CRIU and so there's no svm range to get. This will be addressed in the next patch.
v2: Don't use checkpoint
ctl_stack_sizeSigned-off-by David Francis <[email protected]>--- drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 2 +- drivers/gpu/drm/amd/amdkfd/kfd_priv.h | 2 +- drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c | 8 ++++++++ drivers/gpu/drm/amd/amdkfd/kfd_queue.c | 8 +++++++- 4 files changed, 17 insertions(+), 3 deletions(-)drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +1
-1@@ -433,7 +433,7 @@ static int kfd_ioctl_create_queue(struct file *filep, struct kfd_process *p, goto err_bind_process; } - err =kfd_queue_acquire_buffers (pdd, &q_properties); + err =kfd_queue_acquire_buffers (pdd, &q_properties, false);if (err) {pr_debug ("failed to acquire user queue buffers\n" ); goto err_acquire_queue_buf;drivers/gpu/drm/amd/amdkfd/kfd_priv.h +1
-1@@ -1381,7 +1381,7 @@ void print_queue(struct queue *q);int kfd_queue_buffer_get (struct amdgpu_vm *vm,void __user *addr,struct amdgpu_bo **pbo,u64 expected_size);void kfd_queue_buffer_put (struct amdgpu_bo **bo); -int kfd_queue_acquire_buffers (struct kfd_process_device *pdd,struct queue_properties *properties); +int kfd_queue_acquire_buffers (struct kfd_process_device *pdd,struct queue_properties *properties,bool is_criu);int kfd_queue_release_buffers (struct kfd_process_device *pdd,struct queue_properties *properties);void kfd_queue_unref_bo_va (struct amdgpu_vm *vm,struct amdgpu_bo **bo); int kfd_queue_unref_bo_vas(struct kfd_process_device *pdd,drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c +1
-0@@ -1095,9 +1095,17 @@ int kfd_criu_restore_queue(struct kfd_process *p,print_queue_properties (&qp); + ret =kfd_queue_acquire_buffers (pdd, &qp, true);
drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c int kfd_criu_restore_queue(struct kfd_process *p, · line 1108
Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c +7
-0+if (ret) { +pr_err ("Failed to acquire user queue buffers err:%d\n" , ret); + goto exit; + } + ret =pqm_create_queue (&p->pqm, pdd->dev, &qp, &queue_id, q_data, mqd, ctl_stack,NULL );if (ret) {pr_err ("Failed to create new queue err:%d\n" , ret); +kfd_queue_unref_bo_vas (pdd, &qp); +kfd_queue_release_buffers (pdd, &qp);
I think kfd_queue_release_buffers also needs another parameter to tell it not to handle SVM. Otherwise it will call kfd_queue_buffer_svm_put and try to decrement queue refcounts in pranges that weren't incremented.
Regards,
Felix
Quoted 29 lines
drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c +0
-0goto exit; }drivers/gpu/drm/amd/amdkfd/kfd_queue.c +7
-1@@ -239,7 +239,7 @@ static void kfd_queue_put_bos(struct queue_properties *properties)kfd_queue_buffer_put (&properties->cwsr_bo); } -int kfd_queue_acquire_buffers (struct kfd_process_device *pdd,struct queue_properties *properties) +int kfd_queue_acquire_buffers (struct kfd_process_device *pdd,struct queue_properties *properties,bool is_criu) {struct kfd_topology_device *topo_dev;u64 expected_queue_size;@@ -334,6 +334,12 @@ int kfd_queue_acquire_buffers(struct kfd_process_device *pdd, struct queue_propeamdgpu_bo_unreserve (vm->root.bo); +/* CRIU svm buffers are restored at a later stage than queues. They'll be in +* before the queue is actually used, so no risk here +*/ +if (is_criu) +return 0 ; + err =kfd_queue_buffer_svm_get (pdd, properties->ctx_save_restore_area_address, total_cwsr_size);if (err)
Quoted 6 lines
I think
kfd_queue_release_buffersalso needs another parameter to tell it not to handle SVM. Otherwise it will callkfd_queue_buffer_svm_putand try to decrement queue refcounts in pranges that weren't incremented.Regards,
Felix
Good catch, will fix
David
________________________________________
From: Kuehling, Felix <[email protected]>
Sent: Wednesday, November 4, 2026 2:51 PM
To: Francis, David; [email protected]
Subject: Re: [PATCH v4 1/2] drm/amdkfd: Acquire buffers in CRIU queue restore (again)
Quoted 55 lines · David Francis
CRIU queue restore doesn't acquire the bo buffers in the same way normal queue creation does. In normal queue creation, the driver gets an additional reference on the queue's underlying buffers that lasts until the queue is destroyed; this prevents the queue's buffers from being removed from underneath it.
Acquire them, but don't get the svm bo. SVM isn't yet fully restored by this point in CRIU and so there's no svm range to get. This will be addressed in the next patch.
v2: Don't use checkpoint
ctl_stack_sizeSigned-off-by David Francis <[email protected]>--- drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 2 +- drivers/gpu/drm/amd/amdkfd/kfd_priv.h | 2 +- drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c | 8 ++++++++ drivers/gpu/drm/amd/amdkfd/kfd_queue.c | 8 +++++++- 4 files changed, 17 insertions(+), 3 deletions(-)drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +1
-1@@ -433,7 +433,7 @@ static int kfd_ioctl_create_queue(struct file *filep, struct kfd_process *p, goto err_bind_process; } - err =kfd_queue_acquire_buffers (pdd, &q_properties); + err =kfd_queue_acquire_buffers (pdd, &q_properties, false);if (err) {pr_debug ("failed to acquire user queue buffers\n" ); goto err_acquire_queue_buf;drivers/gpu/drm/amd/amdkfd/kfd_priv.h +1
-1@@ -1381,7 +1381,7 @@ void print_queue(struct queue *q);int kfd_queue_buffer_get (struct amdgpu_vm *vm,void __user *addr,struct amdgpu_bo **pbo,u64 expected_size);void kfd_queue_buffer_put (struct amdgpu_bo **bo); -int kfd_queue_acquire_buffers (struct kfd_process_device *pdd,struct queue_properties *properties); +int kfd_queue_acquire_buffers (struct kfd_process_device *pdd,struct queue_properties *properties,bool is_criu);int kfd_queue_release_buffers (struct kfd_process_device *pdd,struct queue_properties *properties);void kfd_queue_unref_bo_va (struct amdgpu_vm *vm,struct amdgpu_bo **bo); int kfd_queue_unref_bo_vas(struct kfd_process_device *pdd,drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c +1
-0@@ -1095,9 +1095,17 @@ int kfd_criu_restore_queue(struct kfd_process *p,print_queue_properties (&qp); + ret =kfd_queue_acquire_buffers (pdd, &qp, true);
drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c int kfd_criu_restore_queue(struct kfd_process *p, · line 1108
Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c +7
-0+if (ret) { +pr_err ("Failed to acquire user queue buffers err:%d\n" , ret); + goto exit; + } + ret =pqm_create_queue (&p->pqm, pdd->dev, &qp, &queue_id, q_data, mqd, ctl_stack,NULL );if (ret) {pr_err ("Failed to create new queue err:%d\n" , ret); +kfd_queue_unref_bo_vas (pdd, &qp); +kfd_queue_release_buffers (pdd, &qp);
I think kfd_queue_release_buffers also needs another parameter to tell it not to handle SVM. Otherwise it will call kfd_queue_buffer_svm_put and try to decrement queue refcounts in pranges that weren't incremented.
Regards,
Felix
Quoted 29 lines
drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c +0
-0goto exit; }drivers/gpu/drm/amd/amdkfd/kfd_queue.c +7
-1@@ -239,7 +239,7 @@ static void kfd_queue_put_bos(struct queue_properties *properties)kfd_queue_buffer_put (&properties->cwsr_bo); } -int kfd_queue_acquire_buffers (struct kfd_process_device *pdd,struct queue_properties *properties) +int kfd_queue_acquire_buffers (struct kfd_process_device *pdd,struct queue_properties *properties,bool is_criu) {struct kfd_topology_device *topo_dev;u64 expected_queue_size;@@ -334,6 +334,12 @@ int kfd_queue_acquire_buffers(struct kfd_process_device *pdd, struct queue_propeamdgpu_bo_unreserve (vm->root.bo); +/* CRIU svm buffers are restored at a later stage than queues. They'll be in +* before the queue is actually used, so no risk here +*/ +if (is_criu) +return 0 ; + err =kfd_queue_buffer_svm_get (pdd, properties->ctx_save_restore_area_address, total_cwsr_size);if (err)