Skip to contentSkip to conversation

drm/amdkfd: Validate CRIU queue restore sizes and wave state

PATCH New William Palacek 1 participant 5 messages Oct 13

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover drm/amdkfd: Validate CRIU queue restore sizes and wave state

The control stack and MQD sizes of a CRIU restored queue come from the checkpoint image, and get_wave_state() builds its returned sizes and the user header from raw MQD fields. This series bounds both.

1. Size the restored control stack from topology, as create-queue does, reject images that carry more, and keep the gfx9 MQD control stack size and offset within the allocation.
2. Fix the gfx9 checkpoint control stack bound, which stayed 0 because the MQD manager is created before the node is in topology.
3. Derive the wave state header and used sizes from clamped values on gfx8 through gfx12.1, so none of them can wrap.
4. Accept the one-MQD-per-XCC image that checkpoint writes on multi-XCC nodes, restore the MQD stride of the new allocation, and only restore MQD types that can be checkpointed.

Tested on gfx942 (8 XCC) and gfx1036 with synthetic restore images:
out-of-range sizes are rejected with -EINVAL, inconsistent MQD fields give a consistent header, and a driver-written 8-XCC checkpoint (control stack 98304 bytes, was 0) restores in a fresh process with each XCC's MQD in place.

William Palacek (4):
drm/amdkfd: Size restored queues from topology drm/amdkfd: Set the v9 checkpoint control stack bound after topology drm/amdkfd: Report only clamped wave state sizes in get_wave_state() drm/amdkfd: Expect one MQD per XCC when restoring a queue

 .../drm/amd/amdkfd/kfd_device_queue_manager.c | 15 ++++---
 drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager.h  | 16 +++++++
 .../gpu/drm/amd/amdkfd/kfd_mqd_manager_v10.c  | 19 +++-----
 .../gpu/drm/amd/amdkfd/kfd_mqd_manager_v11.c  | 18 +++-----
 .../gpu/drm/amd/amdkfd/kfd_mqd_manager_v12.c  | 14 ++----
 .../drm/amd/amdkfd/kfd_mqd_manager_v12_1.c    | 23 +++-------
 .../gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c   | 45 ++++++-------------
 .../gpu/drm/amd/amdkfd/kfd_mqd_manager_vi.c   |  9 ++--
 .../amd/amdkfd/kfd_process_queue_manager.c    | 33 ++++++++++----
 drivers/gpu/drm/amd/amdkfd/kfd_topology.c     |  4 ++
 10 files changed, 94 insertions(+), 102 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/4 drm/amdkfd: Size restored queues from topology

kfd_criu_restore_queue() takes the control stack size of a restored queue from the checkpoint image, without the check create-queue does in kfd_queue_acquire_buffers(). On gfx9 with CWSR, that size, per XCC, sizes the MQD buffer in allocate_mqd() and is the ceiling get_wave_state() uses to bound its copy of the control stack. A value of 0xFFFFFFFF reaches a kmalloc() above KMALLOC_MAX_SIZE and trips a page allocator warning. Without CWSR the MQD comes from the GTT sub-allocator, sized for the MQD alone, yet restore_mqd() still copies the image's control stack one page after it.

Take the per-XCC control stack size from topology instead, as create-queue requires, and 0 for queues without a control stack (non-compute, or CWSR disabled). Reject an image that carries more control stack than that per XCC.

The MQD in the image keeps its own cp_hqd_cntl_stack_size and cp_hqd_cntl_stack_offset, which CWSR uses to place the control stack after the MQD. Set the size to the allocated size in the gfx9 restore_mqd(), as init_mqd() does, and keep the offset within it, so CWSR cannot access past the control stack of the new allocation.

Fixes 3a9822d7bd62 ("drm/amdkfd: CRIU checkpoint and restore queue control stack")
---
 .../gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c   |  3 ++
 .../amd/amdkfd/kfd_process_queue_manager.c    | 33 ++++++++++++++-----
 2 files changed, 27 insertions(+), 9 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c +3 -0
index 6e6bc1ec0b64..c02dd9abc3ad 100644
@@ -462,6 +462,9 @@ static void restore_mqd(struct mqd_manager *mm, void **mqd,
 	addr = mqd_mem_obj->gpu_addr;
 
 	memcpy(m, mqd_src, sizeof(*m));
+	/* CWSR must not access past the control stack of this allocation */
+	m->cp_hqd_cntl_stack_size = qp->ctl_stack_size;
+	m->cp_hqd_cntl_stack_offset = min(m->cp_hqd_cntl_stack_offset, qp->ctl_stack_size);
 
 	*mqd = m;
 	if (gart_addr)
drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c +24 -9
index a15a826519ad..b41494775767 100644
@@ -26,6 +26,7 @@
 #include <linux/list.h>
 #include "kfd_device_queue_manager.h"
 #include "kfd_priv.h"
+#include "kfd_topology.h"
 #include "kfd_kernel_queue.h"
 #include "amdgpu_amdkfd.h"
 #include "amdgpu_reset.h"
@@ -983,7 +984,7 @@ int kfd_criu_checkpoint_queues(struct kfd_process *p,
 }
 
 static void set_queue_properties_from_criu(struct queue_properties *qp,
-					  struct kfd_criu_queue_priv_data *q_data, uint32_t num_xcc)
+					  struct kfd_criu_queue_priv_data *q_data)
 {
 	qp->is_interop = false;
 	qp->queue_percent = q_data->q_percent;
@@ -996,11 +997,6 @@ static void set_queue_properties_from_criu(struct queue_properties *qp,
 	qp->eop_ring_buffer_size = q_data->eop_ring_buffer_size;
 	qp->ctx_save_restore_area_address = q_data->ctx_save_restore_area_address;
 	qp->ctx_save_restore_area_size = q_data->ctx_save_restore_area_size;
-	if (q_data->type == KFD_QUEUE_TYPE_COMPUTE)
-		qp->ctl_stack_size = q_data->ctl_stack_size / num_xcc;
-	else
-		qp->ctl_stack_size = q_data->ctl_stack_size;
-
 	qp->type = q_data->type;
 	qp->format = q_data->format;
 }
@@ -1012,6 +1008,7 @@ int kfd_criu_restore_queue(struct kfd_process *p,
 {
 	uint8_t *mqd, *ctl_stack, *q_extra_data = NULL;
 	struct kfd_criu_queue_priv_data *q_data;
+	struct kfd_topology_device *topo_dev;
 	struct kfd_process_device *pdd;
 	uint64_t q_extra_data_size;
 	struct queue_properties qp;
@@ -1048,6 +1045,27 @@ int kfd_criu_restore_queue(struct kfd_process *p,
 		goto exit;
 	}
 
+	memset(&qp, 0, sizeof(qp));
+	set_queue_properties_from_criu(&qp, q_data);
+
+	/*
+	 * Size the control stack as create-queue does, not from the image.
+	 * Only compute queues with CWSR have one, one per XCC.
+	 */
+	if (qp.type == KFD_QUEUE_TYPE_COMPUTE && pdd->dev->kfd->cwsr_enabled) {
+		topo_dev = kfd_topology_device_by_id(pdd->dev->id);
+		if (!topo_dev) {
+			ret = -EINVAL;
+			goto exit;
+		}
+		qp.ctl_stack_size = topo_dev->node_props.ctl_stack_size;
+	}
+
+	if (q_data->ctl_stack_size > (u64)qp.ctl_stack_size * NUM_XCC(pdd->dev->xcc_mask)) {
+		ret = -EINVAL;
+		goto exit;
+	}
+
 	*priv_data_offset += sizeof(*q_data);
 	q_extra_data_size = (uint64_t)q_data->ctl_stack_size + q_data->mqd_size;
 
@@ -1077,9 +1095,6 @@ int kfd_criu_restore_queue(struct kfd_process *p,
 	mqd = q_extra_data;
 	ctl_stack = mqd + q_data->mqd_size;
 
-	memset(&qp, 0, sizeof(qp));
-	set_queue_properties_from_criu(&qp, q_data, NUM_XCC(pdd->dev->xcc_mask));
-
 	print_queue_properties(&qp);
 
 	ret = pqm_create_queue(&p->pqm, pdd->dev, &qp, &queue_id, q_data, mqd, ctl_stack, NULL);
Signature · 1 line
2.43.0
#2 lore raw

PATCH 2/4 drm/amdkfd: Set the v9 checkpoint control stack bound after topology

In reply to William Palacek

mqd_manager_init_v9() looks up the node in topology to set mm->ctl_stack_size, the bound for the control stack copy at CRIU checkpoint. The device queue manager, and with it the MQD managers, is created before kfd_topology_add_device() assigns the node its id, so the lookup never finds the node and the bound stays 0. Every gfx9 checkpoint then reports and copies an empty control stack.

Set the bound in kfd_topology_add_device(), right after topology computes the control stack size.

Fixes 575ebd79a819 ("drm/amdkfd: clamp v9 CRIU control stack checkpoint copy to BO size")
---
 drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c | 10 ----------
 drivers/gpu/drm/amd/amdkfd/kfd_topology.c       |  4 ++++
 2 files changed, 4 insertions(+), 10 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c +0 -10
index c02dd9abc3ad..35c36b6dc212 100644
@@ -27,7 +27,6 @@
 #include <linux/uaccess.h>
 #include "kfd_priv.h"
 #include "kfd_mqd_manager.h"
-#include "kfd_topology.h"
 #include "v9_structs.h"
 #include "gc/gc_9_0_offset.h"
 #include "gc/gc_9_0_sh_mask.h"
@@ -1039,15 +1038,6 @@ struct mqd_manager *mqd_manager_init_v9(enum KFD_MQD_TYPE type,
 		mqd->is_occupied = kfd_is_occupied_cp;
 		mqd->get_checkpoint_info = get_checkpoint_info;
 		mqd->mqd_size = sizeof(struct v9_mqd);
-		if (dev->kfd->cwsr_enabled) {
-			struct kfd_topology_device *topo_dev;
-
-			topo_dev = kfd_topology_device_by_id(dev->id);
-			if (topo_dev)
-				mqd->ctl_stack_size =
-					ALIGN(topo_dev->node_props.ctl_stack_size,
-					      AMDGPU_GPU_PAGE_SIZE);
-		}
 		mqd->mqd_stride = mqd_stride_v9;
 #if defined(CONFIG_DEBUG_FS)
 		mqd->debugfs_show_mqd = debugfs_show_mqd;
drivers/gpu/drm/amd/amdkfd/kfd_topology.c +4 -0
index 35b3abe57b80..1fdec20a9609 100644
@@ -2203,6 +2203,10 @@ int kfd_topology_add_device(struct kfd_node *gpu)
 		dev->node_props.capability |= HSA_CAP_FLAGS_COHERENTHOSTACCESS;
 
 	kfd_queue_ctx_save_restore_size(dev);
+	/* dqm is created before the node has an id to look up in topology */
+	if (gpu->kfd->cwsr_enabled)
+		gpu->dqm->mqd_mgrs[KFD_MQD_TYPE_CP]->ctl_stack_size =
+			dev->node_props.ctl_stack_size;
 
 	kfd_debug_print_topology();
 
Signature · 1 line
2.43.0
#3 lore raw

PATCH 3/4 drm/amdkfd: Report only clamped wave state sizes in get_wave_state()

In reply to William Palacek

get_wave_state() on gfx9 clamps the control stack size and offset before the copy_to_user() of the control stack, but still builds the returned ctl_stack_used_size, save_area_used_size and the context save area header from the raw MQD values. The gfx8, gfx10, gfx11, gfx12 and gfx12.1 versions do the same without any clamp. With an inconsistent MQD (for example restored from a CRIU image) the header and the returned sizes do not match what was copied, and either subtraction (cp_hqd_cntl_stack_size - cp_hqd_cntl_stack_offset, cp_hqd_wg_state_offset - cp_hqd_cntl_stack_size) can wrap to a value near 4 GiB. get_wave_state_v9_4_3() and get_wave_state_v12_1() inherit this since they call it per XCC.

Add kfd_mqd_clamp_wave_state(), which derives the header and both used sizes from clamped values: offset <= size <= ceiling and wg_state_offset >= size. gfx9 passes q->ctl_stack_size, the allocated control stack size, as the ceiling. gfx8, gfx10 and newer pass U32_MAX:
they copy nothing from the MQD buffer, their control stack is part of the user's save area. Values from a consistent MQD are unchanged, and the header keeps its layout of four u32 fields. wg_state_offset has no trusted upper bound: the wave state lives in the user's save area, which the kernel does not access.

Fixes 5df099e8bc83 ("drm/amdkfd: Add wavefront context save state retrieval ioctl")
Fixes 14328aa58ce5 ("drm/amdkfd: Add navi10 support to amdkfd. (v3)")
Fixes cc009e613de6 ("drm/amdkfd: Add KFD support for soc21 v3")
Fixes 48f0bdf4e38e ("drm/amdkfd: Added MQD manager files for GFX12.")
Fixes 01bbc4a4b947 ("drm/amdkfd: Add MQD manager for GFX 12.1.0")
Fixes 7ef144458f48 ("drm/amdkfd: Fix OOB memory exposure in get_wave_state()")
---
 drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager.h  | 16 ++++++++++
 .../gpu/drm/amd/amdkfd/kfd_mqd_manager_v10.c  | 19 ++++--------
 .../gpu/drm/amd/amdkfd/kfd_mqd_manager_v11.c  | 18 ++++-------
 .../gpu/drm/amd/amdkfd/kfd_mqd_manager_v12.c  | 14 +++------
 .../drm/amd/amdkfd/kfd_mqd_manager_v12_1.c    | 14 +++------
 .../gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c   | 31 ++++++-------------
 .../gpu/drm/amd/amdkfd/kfd_mqd_manager_vi.c   |  9 +++---
 7 files changed, 51 insertions(+), 70 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager.h +16 -0
index 38b46b696243..dc4681328a47 100644
@@ -163,6 +163,22 @@ struct mqd_user_context_save_area_header {
 	uint32_t wave_state_size;
 };
 
+/*
+ * Fill the user header from raw MQD fields. They come from CP firmware or a
+ * CRIU image, so make them self-consistent: offset <= size <= ceiling and
+ * wg_state_offset >= size, which keeps both used sizes from wrapping.
+ */
+static inline void kfd_mqd_clamp_wave_state(u32 cntl_stack_size, u32 cntl_stack_offset,
+					    u32 wg_state_offset, u32 ceiling,
+					    struct mqd_user_context_save_area_header *hdr)
+{
+	cntl_stack_size = min(cntl_stack_size, ceiling);
+	hdr->control_stack_offset = min(cntl_stack_offset, cntl_stack_size);
+	hdr->wave_state_offset = max(wg_state_offset, cntl_stack_size);
+	hdr->control_stack_size = cntl_stack_size - hdr->control_stack_offset;
+	hdr->wave_state_size = hdr->wave_state_offset - cntl_stack_size;
+}
+
 struct kfd_mem_obj *allocate_hiq_mqd(struct mqd_manager *mm,
 				struct queue_properties *q);
 
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v10.c +6 -13
index 4f8a8a1a6186..64a97723c083 100644
@@ -252,7 +252,7 @@ static int get_wave_state(struct mqd_manager *mm, void *mqd,
 			  u32 *save_area_used_size)
 {
 	struct v10_compute_mqd *m;
-	struct kfd_context_save_area_header header;
+	struct mqd_user_context_save_area_header header;
 
 	m = get_mqd(mqd);
 
@@ -261,23 +261,16 @@ static int get_wave_state(struct mqd_manager *mm, void *mqd,
 	 * Current position is at m->cp_hqd_cntl_stack_offset and
 	 * m->cp_hqd_wg_state_offset, respectively.
 	 */
-	*ctl_stack_used_size = m->cp_hqd_cntl_stack_size -
-		m->cp_hqd_cntl_stack_offset;
-	*save_area_used_size = m->cp_hqd_wg_state_offset -
-		m->cp_hqd_cntl_stack_size;
+	kfd_mqd_clamp_wave_state(m->cp_hqd_cntl_stack_size, m->cp_hqd_cntl_stack_offset,
+				 m->cp_hqd_wg_state_offset, U32_MAX, &header);
+	*ctl_stack_used_size = header.control_stack_size;
+	*save_area_used_size = header.wave_state_size;
 
 	/* Control stack is not copied to user mode for GFXv10 because
 	 * it's part of the context save area that is already
 	 * accessible to user mode
 	 */
-
-	header.wave_state.control_stack_size = *ctl_stack_used_size;
-	header.wave_state.wave_state_size = *save_area_used_size;
-
-	header.wave_state.wave_state_offset = m->cp_hqd_wg_state_offset;
-	header.wave_state.control_stack_offset = m->cp_hqd_cntl_stack_offset;
-
-	if (copy_to_user(ctl_stack, &header, sizeof(header.wave_state)))
+	if (copy_to_user(ctl_stack, &header, sizeof(header)))
 		return -EFAULT;
 
 	return 0;
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v11.c +6 -12
index bf015dc5b868..e3d2f50f86c8 100644
@@ -289,7 +289,7 @@ static int get_wave_state(struct mqd_manager *mm, void *mqd,
 			  u32 *save_area_used_size)
 {
 	struct v11_compute_mqd *m;
-	struct kfd_context_save_area_header header;
+	struct mqd_user_context_save_area_header header;
 
 	m = get_mqd(mqd);
 
@@ -298,22 +298,16 @@ static int get_wave_state(struct mqd_manager *mm, void *mqd,
 	 * Current position is at m->cp_hqd_cntl_stack_offset and
 	 * m->cp_hqd_wg_state_offset, respectively.
 	 */
-	*ctl_stack_used_size = m->cp_hqd_cntl_stack_size -
-		m->cp_hqd_cntl_stack_offset;
-	*save_area_used_size = m->cp_hqd_wg_state_offset -
-		m->cp_hqd_cntl_stack_size;
+	kfd_mqd_clamp_wave_state(m->cp_hqd_cntl_stack_size, m->cp_hqd_cntl_stack_offset,
+				 m->cp_hqd_wg_state_offset, U32_MAX, &header);
+	*ctl_stack_used_size = header.control_stack_size;
+	*save_area_used_size = header.wave_state_size;
 
 	/* Control stack is not copied to user mode for GFXv11 because
 	 * it's part of the context save area that is already
 	 * accessible to user mode
 	 */
-	header.wave_state.control_stack_size = *ctl_stack_used_size;
-	header.wave_state.wave_state_size = *save_area_used_size;
-
-	header.wave_state.wave_state_offset = m->cp_hqd_wg_state_offset;
-	header.wave_state.control_stack_offset = m->cp_hqd_cntl_stack_offset;
-
-	if (copy_to_user(ctl_stack, &header, sizeof(header.wave_state)))
+	if (copy_to_user(ctl_stack, &header, sizeof(header)))
 		return -EFAULT;
 
 	return 0;
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v12.c +4 -10
index 6ea09b031caf..1068a7629331 100644
@@ -267,21 +267,15 @@ static int get_wave_state(struct mqd_manager *mm, void *mqd,
 	 * Current position is at m->cp_hqd_cntl_stack_offset and
 	 * m->cp_hqd_wg_state_offset, respectively.
 	 */
-	*ctl_stack_used_size = m->cp_hqd_cntl_stack_size -
-		m->cp_hqd_cntl_stack_offset;
-	*save_area_used_size = m->cp_hqd_wg_state_offset -
-		m->cp_hqd_cntl_stack_size;
+	kfd_mqd_clamp_wave_state(m->cp_hqd_cntl_stack_size, m->cp_hqd_cntl_stack_offset,
+				 m->cp_hqd_wg_state_offset, U32_MAX, &header);
+	*ctl_stack_used_size = header.control_stack_size;
+	*save_area_used_size = header.wave_state_size;
 
 	/* Control stack is not copied to user mode for GFXv12 because
 	 * it's part of the context save area that is already
 	 * accessible to user mode
 	 */
-	header.control_stack_size = *ctl_stack_used_size;
-	header.wave_state_size = *save_area_used_size;
-
-	header.wave_state_offset = m->cp_hqd_wg_state_offset;
-	header.control_stack_offset = m->cp_hqd_cntl_stack_offset;
-
 	if (copy_to_user(ctl_stack, &header, sizeof(header)))
 		return -EFAULT;
 
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v12_1.c +4 -10
index d508ac5a18f8..79e0d6250720 100644
@@ -369,21 +369,15 @@ static int get_wave_state(struct mqd_manager *mm, void *mqd,
 	 * Current position is at m->cp_hqd_cntl_stack_offset and
 	 * m->cp_hqd_wg_state_offset, respectively.
 	 */
-	*ctl_stack_used_size = m->cp_hqd_cntl_stack_size -
-		m->cp_hqd_cntl_stack_offset;
-	*save_area_used_size = m->cp_hqd_wg_state_offset -
-		m->cp_hqd_cntl_stack_size;
+	kfd_mqd_clamp_wave_state(m->cp_hqd_cntl_stack_size, m->cp_hqd_cntl_stack_offset,
+				 m->cp_hqd_wg_state_offset, U32_MAX, &header);
+	*ctl_stack_used_size = header.control_stack_size;
+	*save_area_used_size = header.wave_state_size;
 
 	/* Control stack is not copied to user mode for GFXv12 because
 	 * it's part of the context save area that is already
 	 * accessible to user mode
 	 */
-	header.control_stack_size = *ctl_stack_used_size;
-	header.wave_state_size = *save_area_used_size;
-
-	header.wave_state_offset = m->cp_hqd_wg_state_offset;
-	header.control_stack_offset = m->cp_hqd_cntl_stack_offset;
-
 	if (copy_to_user(ctl_stack, &header, sizeof(header)))
 		return -EFAULT;
 
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c +10 -21
index 35c36b6dc212..948415908551 100644
@@ -364,35 +364,24 @@ static int get_wave_state(struct mqd_manager *mm, void *mqd,
 			  u32 *save_area_used_size)
 {
 	struct v9_mqd *m;
-	struct kfd_context_save_area_header header;
-	u32 cntl_stack_size;
-	u32 cntl_stack_offset;
+	struct mqd_user_context_save_area_header header;
 
 	/* Control stack is located one page after MQD. */
 	void *mqd_ctl_stack = (void *)((uintptr_t)mqd + AMDGPU_GPU_PAGE_SIZE);
 
 	m = get_mqd(mqd);
-	cntl_stack_size = min_t(u32, m->cp_hqd_cntl_stack_size,   q->ctl_stack_size);
-	cntl_stack_offset = min_t(u32, m->cp_hqd_cntl_stack_offset, cntl_stack_size);
+	/* Only q->ctl_stack_size, the allocation size, is trusted. */
+	kfd_mqd_clamp_wave_state(m->cp_hqd_cntl_stack_size, m->cp_hqd_cntl_stack_offset,
+				 m->cp_hqd_wg_state_offset, q->ctl_stack_size, &header);
+	*ctl_stack_used_size = header.control_stack_size;
+	*save_area_used_size = header.wave_state_size;
 
-	*ctl_stack_used_size = m->cp_hqd_cntl_stack_size -
-		m->cp_hqd_cntl_stack_offset;
-	*save_area_used_size = m->cp_hqd_wg_state_offset -
-		m->cp_hqd_cntl_stack_size;
-
-	header.wave_state.control_stack_size = *ctl_stack_used_size;
-	header.wave_state.wave_state_size = *save_area_used_size;
-
-	header.wave_state.wave_state_offset = m->cp_hqd_wg_state_offset;
-	header.wave_state.control_stack_offset = m->cp_hqd_cntl_stack_offset;
-
-	if (copy_to_user(ctl_stack, &header, sizeof(header.wave_state)))
+	if (copy_to_user(ctl_stack, &header, sizeof(header)))
 		return -EFAULT;
 
-	*ctl_stack_used_size = cntl_stack_size - cntl_stack_offset;
-
-	if (copy_to_user(ctl_stack + cntl_stack_offset, mqd_ctl_stack + cntl_stack_offset,
-					*ctl_stack_used_size))
+	if (copy_to_user(ctl_stack + header.control_stack_offset,
+			 mqd_ctl_stack + header.control_stack_offset,
+			 *ctl_stack_used_size))
 		return -EFAULT;
 
 	return 0;
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_vi.c +5 -4
index 029572548c14..a11d01689f8b 100644
@@ -272,13 +272,14 @@ static int get_wave_state(struct mqd_manager *mm, void *mqd,
 			  u32 *save_area_used_size)
 {
 	struct vi_mqd *m;
+	struct mqd_user_context_save_area_header header;
 
 	m = get_mqd(mqd);
 
-	*ctl_stack_used_size = m->cp_hqd_cntl_stack_size -
-		m->cp_hqd_cntl_stack_offset;
-	*save_area_used_size = m->cp_hqd_wg_state_offset -
-		m->cp_hqd_cntl_stack_size;
+	kfd_mqd_clamp_wave_state(m->cp_hqd_cntl_stack_size, m->cp_hqd_cntl_stack_offset,
+				 m->cp_hqd_wg_state_offset, U32_MAX, &header);
+	*ctl_stack_used_size = header.control_stack_size;
+	*save_area_used_size = header.wave_state_size;
 
 	/* Control stack is not copied to user mode for GFXv8 because
 	 * it's part of the context save area that is already
Signature · 1 line
2.43.0
#4 lore raw

PATCH 4/4 drm/amdkfd: Expect one MQD per XCC when restoring a queue

In reply to William Palacek

get_queue_checkpoint_info() reports mqd_size as the per-XCC MQD size times NUM_XCC, and checkpoint_mqd_v9_4_3() stores one MQD per XCC. The mqd_size check added to the CRIU restore path compares against the single per-XCC size, so every checkpoint image of a queue on a multi-XCC node is rejected with -EINVAL.

Worse, a single-MQD size is the only value that check accepts there, and restore_mqd_v9_4_3() then reads one MQD per XCC from a buffer holding only one, copying up to (NUM_XCC - 1) * sizeof(struct v9_mqd) bytes of adjacent kernel heap into the queue's MQD.

Make mqd_size_from_queue_type() return the size checkpoint writes, so a restore image must carry exactly one MQD per XCC. With that, restore_mqd_v9_4_3() must also not keep the image's cp_mqd_stride_size:
checkpoint and get_wave_state() step through the per-XCC MQDs with it, so set it to the stride of the new allocation as init_mqd_v9_4_3() does.

Restore only MQD types that have checkpoint_mqd(), since no other image layout exists. GFX12 and GFX12.1 have restore_mqd() but no checkpoint yet, and restore_mqd_v12_1() handles XCC0 only, so a multi-XCC GFX12.1 image would now pass the size check and leave XCC1..N with zeroed MQDs.

Fixes c76bcb1cfce6 ("drm/amdkfd: Check bounds on CRIU restore queue type and mqd size")
---
 .../gpu/drm/amd/amdkfd/kfd_device_queue_manager.c | 15 ++++++++++-----
 .../gpu/drm/amd/amdkfd/kfd_mqd_manager_v12_1.c    |  9 +--------
 drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c   |  1 +
 3 files changed, 12 insertions(+), 13 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +10 -5
index 5e57576fba21..88bbef08c39c 100644
@@ -816,8 +816,9 @@ static int create_queue_nocpsch(struct device_queue_manager *dqm,
 
 	mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type(
 			q->properties.type)];
-	if (qd && !mqd_mgr->restore_mqd) {
-		pr_debug("restore_mqd not implemented for queue type %d\n",
+	/* Restore takes the image layout that checkpoint_mqd() writes */
+	if (qd && (!mqd_mgr->restore_mqd || !mqd_mgr->checkpoint_mqd)) {
+		pr_debug("CRIU not implemented for queue type %d\n",
 			 q->properties.type);
 		retval = -EOPNOTSUPP;
 		goto deallocate_vmid;
@@ -2399,8 +2400,9 @@ static int create_queue_cpsch(struct device_queue_manager *dqm, struct queue *q,
 
 	mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type(
 			q->properties.type)];
-	if (qd && !mqd_mgr->restore_mqd) {
-		pr_debug("restore_mqd not implemented for queue type %d\n",
+	/* Restore takes the image layout that checkpoint_mqd() writes */
+	if (qd && (!mqd_mgr->restore_mqd || !mqd_mgr->checkpoint_mqd)) {
+		pr_debug("CRIU not implemented for queue type %d\n",
 			 q->properties.type);
 		retval = -EOPNOTSUPP;
 		goto out_deallocate_doorbell;
@@ -4123,7 +4125,10 @@ bool kfd_dqm_is_queue_in_process(struct device_queue_manager *dqm,
 
 size_t mqd_size_from_queue_type(struct device_queue_manager *dqm, enum kfd_queue_type type)
 {
-	return dqm->mqd_mgrs[get_mqd_type_from_queue_type(type)]->mqd_size;
+	struct mqd_manager *mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type(type)];
+
+	/* Checkpoint stores one MQD per XCC, see get_queue_checkpoint_info() */
+	return mqd_mgr->mqd_size * NUM_XCC(mqd_mgr->dev->xcc_mask);
 }
 
 #if defined(CONFIG_DEBUG_FS)
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v12_1.c +1 -8
index 79e0d6250720..4eac85a654cb 100644
@@ -670,14 +670,7 @@ static void restore_mqd_v12_1(struct mqd_manager *mm, void **mqd,
 	u64 addr;
 	struct v12_1_compute_mqd *m;
 
-	/*
-	 * GFX12.1 is multi-XCC capable but this restore handles XCC0 only.
-	 * Multi-XCC CRIU restore is currently unreachable because
-	 * kfd_criu_restore_queue() validates against unscaled mqd_size.
-	 */
-	if (NUM_XCC(mm->dev->xcc_mask) > 1)
-		pr_warn_once("GFX12.1 multi-XCC CRIU restore not fully supported\n");
-
+	/* Handles XCC0 only, unreachable until there is a checkpoint_mqd() */
 	m = (struct v12_1_compute_mqd *)mqd_mem_obj->cpu_ptr;
 	addr = mqd_mem_obj->gpu_addr;
 
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c +1 -0
index 948415908551..d95a28a80917 100644
@@ -864,6 +864,7 @@ static void restore_mqd_v9_4_3(struct mqd_manager *mm, void **mqd,
 					(uint8_t *)mqd_src + xcc * sizeof(*m),
 					(uint8_t *)ctl_stack_src + xcc *  mqd_ctl_stack_size,
 					mqd_ctl_stack_size);
+		m->cp_mqd_stride_size = offset;
 	}
 
 	if (mqd_on_vram(mm->dev->adev))
Signature · 1 line
2.43.0