Skip to contentSkip to conversation

drm/amdgpu/userq: Add INFO IOCTL query for doorbell ranges

PATCH In review Alex Deucher 4 participants 18 messages Nov 18 – Nov 21

Subsystems (3)
lore
Share & tools
lore

Alex Deucher

author maintainer
#0 lore raw

PATCH 1/11 drm/amdgpu/userq: Add INFO IOCTL query for doorbell ranges

Each IP gets a subset of each doorbell page. Add a query so userspace can query the ranges.

v2: fix unit conversion

Cc Yogesh Mohan Marimuthu <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c | 20 ++++++++++++++++++++
 include/uapi/drm/amdgpu_drm.h           |  6 ++++++
 2 files changed, 26 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +20 -0
index d7872acb4d790..0d157c18b7aaa 100644
@@ -1453,6 +1453,26 @@ int amdgpu_info_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
 			return -EINVAL;
 		}
 	}
+	case AMDGPU_INFO_UQ_DOORBELL_RANGE: {
+		struct drm_amdgpu_info_doorbell_range db_range = {};
+
+		switch (info->query_hw_ip.type) {
+		case AMDGPU_HW_IP_GFX:
+		case AMDGPU_HW_IP_COMPUTE:
+			db_range.offset_bytes = adev->doorbell_index.kiq << 3;
+			db_range.size_bytes = (adev->doorbell_index.sdma_engine[0] -
+					       adev->doorbell_index.kiq) << 3;
+			return copy_to_user(out, &db_range,
+					    min((size_t)size, sizeof(db_range))) ? -EFAULT : 0;
+		case AMDGPU_HW_IP_DMA:
+			db_range.offset_bytes = adev->doorbell_index.sdma_engine[0] << 3;
+			db_range.size_bytes = adev->doorbell_index.sdma_doorbell_range * adev->sdma.num_instances * 4;
+			return copy_to_user(out, &db_range,
+					    min((size_t)size, sizeof(db_range))) ? -EFAULT : 0;
+		default:
+			return -EINVAL;
+		}
+	}
 	default:
 		DRM_DEBUG_KMS("Invalid request %d\n", info->query);
 		return -EINVAL;
include/uapi/drm/amdgpu_drm.h +6 -0
index 29009f6aabde9..86d42a5d683c0 100644
@@ -1289,6 +1289,7 @@ struct drm_amdgpu_cs_chunk_cp_gfx_shadow {
 #define AMDGPU_INFO_GPUVM_FAULT			0x23
 /* query FW object size and alignment */
 #define AMDGPU_INFO_UQ_FW_AREAS			0x24
+#define AMDGPU_INFO_UQ_DOORBELL_RANGE		0x25
 
 #define AMDGPU_INFO_MMR_SE_INDEX_SHIFT	0
 #define AMDGPU_INFO_MMR_SE_INDEX_MASK	0xff
@@ -1668,6 +1669,11 @@ struct drm_amdgpu_info_uq_metadata {
 	};
 };
 
+struct drm_amdgpu_info_doorbell_range {
+	__u32 offset_bytes;
+	__u32 size_bytes;
+};
+
 /*
  * Supported GPU families
  */
Signature · 1 line
2.56.1
author maintainer
#1 lore raw

PATCH 2/11 drm/amdgpu/mes11: add API for gang submit

Add API for setting up a gang. Gangs schedule together.

Cc Yogesh Mohan Marimuthu <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/mes_v11_0.c        |  2 +-
 drivers/gpu/drm/amd/include/mes_v11_api_def.h | 18 ++++++++++++++++++
 2 files changed, 19 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/mes_v11_0.c +1 -1
index 30d3d25d59842..0242fe8135374 100644
@@ -147,7 +147,7 @@ static const char *mes_v11_0_opcodes[] = {
 	"UPDATE_ROOT_PAGE_TABLE",
 	"AMD_LOG",
 	"unused",
-	"unused",
+	"SET_GANG_SUBMIT",
 	"SET_HW_RSRC_1",
 };
 
drivers/gpu/drm/amd/include/mes_v11_api_def.h +18 -0
index 6d67d015a74fb..bf2161b2328ad 100644
@@ -64,6 +64,7 @@ enum MES_SCH_API_OPCODE {
 	MES_SCH_API_MISC			= 14,
 	MES_SCH_API_UPDATE_ROOT_PAGE_TABLE      = 15,
 	MES_SCH_API_AMD_LOG                     = 16,
+	MES_SCH_API_SET_GANG_SUBMIT             = 18,
 	MES_SCH_API_SET_HW_RSRC_1               = 19,
 	MES_SCH_API_MAX				= 0xFF
 };
@@ -688,6 +689,13 @@ struct SET_SHADER_DEBUGGER {
 	uint32_t process_context_array_index;
 };
 
+struct SET_GANG_SUBMIT {
+	uint64_t gang_context_addr;
+	uint64_t slave_gang_context_addr;
+	uint32_t gang_context_array_index;
+	uint32_t slave_gang_context_array_index;
+};
+
 enum MESAPI_MISC__CHANGE_CONFIG_OPTION {
 	MESAPI_MISC__CHANGE_CONFIG_OPTION_LIMIT_SINGLE_PROCESS = 0,
 	MESAPI_MISC__CHANGE_CONFIG_OPTION_ENABLE_HWS_LOGGING_BUFFER = 1,
@@ -775,5 +783,15 @@ union MESAPI_AMD_LOG {
 	uint32_t max_dwords_in_api[API_FRAME_SIZE_IN_DWORDS];
 };
 
+union MESAPI__SET_GANG_SUBMIT {
+	struct {
+		union MES_API_HEADER    header;
+		struct MES_API_STATUS   api_status;
+		struct SET_GANG_SUBMIT  set_gang_submit;
+	};
+
+	uint32_t max_dwords_in_api[API_FRAME_SIZE_IN_DWORDS];
+};
+
 #pragma pack(pop)
 #endif
Signature · 1 line
2.56.1
author maintainer
#2 lore raw

PATCH 3/11 drm/amdgpu/mes: add callback for gang submit

In reply to Alex Deucher

Add function pointer for gang submit setup.

v2: handle array indices

Cc Yogesh Mohan Marimuthu <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h | 10 ++++++++++
 1 file changed, 10 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h +10 -0
index e5dbcf8ef890a..43733037a762c 100644
@@ -401,6 +401,13 @@ struct mes_inv_tlbs_pasid_input {
 	uint8_t         flush_type;
 };
 
+struct mes_set_gang_submit_input {
+	uint64_t	primary_gang_context_addr;
+	uint64_t	secondary_gang_context_addr;
+	uint32_t	primary_gang_context_array_idx;
+	uint32_t	secondary_gang_context_array_idx;
+};
+
 enum mes_misc_opcode {
 	MES_MISC_OP_WRITE_REG,
 	MES_MISC_OP_READ_REG,
@@ -520,6 +527,9 @@ struct amdgpu_mes_funcs {
 
 	int (*invalidate_tlbs_pasid)(struct amdgpu_mes *mes,
 			      struct mes_inv_tlbs_pasid_input *input);
+
+	int (*set_gang_submit)(struct amdgpu_mes *mes,
+			       struct mes_set_gang_submit_input *input);
 };
 
 enum amdgpu_mqd_update_flag {
Signature · 1 line
2.56.1
author maintainer
#3 lore raw

PATCH 4/11 drm/amdgpu/mes11: add support for setting gang submit

In reply to Alex Deucher

Enable a primary and secondary queue that schedule together.

v2: fix offset of api_status (Prike)
v3: handle array indices

Cc Yogesh Mohan Marimuthu <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/mes_v11_0.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
drivers/gpu/drm/amd/amdgpu/mes_v11_0.c +26 -0
index 0242fe8135374..2be54f7dc6f25 100644
@@ -1283,6 +1283,31 @@ static int mes_v11_0_enable_dbgext_irq(struct amdgpu_mes *mes, bool enable)
 	return 0;
 }
 
+static int mes_v11_0_set_gang_submit(struct amdgpu_mes *mes,
+				     struct mes_set_gang_submit_input *input)
+{
+	union MESAPI__SET_GANG_SUBMIT mes_gang_submit_pkt;
+
+	memset(&mes_gang_submit_pkt, 0, sizeof(mes_gang_submit_pkt));
+
+	mes_gang_submit_pkt.header.type = MES_API_TYPE_SCHEDULER;
+	mes_gang_submit_pkt.header.opcode = MES_SCH_API_SET_GANG_SUBMIT;
+	mes_gang_submit_pkt.header.dwsize = API_FRAME_SIZE_IN_DWORDS;
+
+	mes_gang_submit_pkt.set_gang_submit.gang_context_addr =
+		input->primary_gang_context_addr;
+	mes_gang_submit_pkt.set_gang_submit.slave_gang_context_addr =
+		input->secondary_gang_context_addr;
+	mes_gang_submit_pkt.set_gang_submit.gang_context_array_index =
+		input->primary_gang_context_array_idx;
+	mes_gang_submit_pkt.set_gang_submit.slave_gang_context_array_index =
+		input->secondary_gang_context_array_idx;
+
+	return mes_v11_0_submit_pkt_and_poll_completion(mes,
+			&mes_gang_submit_pkt, sizeof(mes_gang_submit_pkt),
+			offsetof(union MESAPI__SET_GANG_SUBMIT, api_status));
+}
+
 static const struct amdgpu_mes_funcs mes_v11_0_funcs = {
 	.add_hw_queue = mes_v11_0_add_hw_queue,
 	.remove_hw_queue = mes_v11_0_remove_hw_queue,
@@ -1294,6 +1319,7 @@ static const struct amdgpu_mes_funcs mes_v11_0_funcs = {
 	.enable_dbgext_irq = mes_v11_0_enable_dbgext_irq,
 	.reset_hw_queue = mes_v11_0_reset_hw_queue,
 	.detect_and_reset_hung_queues = mes_v11_0_detect_and_reset_hung_queues,
+	.set_gang_submit = mes_v11_0_set_gang_submit,
 };
 
 static int mes_v11_0_allocate_ucode_buffer(struct amdgpu_device *adev,
Signature · 1 line
2.56.1
author maintainer
#4 lore raw

PATCH 5/11 drm/amdgpu/mes12: add support for setting gang submit

In reply to Alex Deucher

Enable a primary and secondary queue that schedule together.

v2: fix offset of api_status (Prike)
v3: use AMDGPU_MES_SCHED_PIPE (Michael)
v4: handle array indices

Cc Yogesh Mohan Marimuthu <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/mes_v12_0.c | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
drivers/gpu/drm/amd/amdgpu/mes_v12_0.c +25 -0
index 988e05823d2d7..f6c359e7d4861 100644
@@ -1313,7 +1313,31 @@ static int mes_v12_0_inv_tlbs_pasid(struct amdgpu_mes *mes,
 	return mes_v12_0_submit_pkt_and_poll_completion(mes, AMDGPU_MES_KIQ_PIPE,
 			&mes_inv_tlbs, sizeof(mes_inv_tlbs),
 			offsetof(union MESAPI__INV_TLBS, api_status));
+}
+
+static int mes_v12_0_set_gang_submit(struct amdgpu_mes *mes,
+				     struct mes_set_gang_submit_input *input)
+{
+	union MESAPI__SET_GANG_SUBMIT mes_gang_submit_pkt;
+
+	memset(&mes_gang_submit_pkt, 0, sizeof(mes_gang_submit_pkt));
 
+	mes_gang_submit_pkt.header.type = MES_API_TYPE_SCHEDULER;
+	mes_gang_submit_pkt.header.opcode = MES_SCH_API_SET_GANG_SUBMIT;
+	mes_gang_submit_pkt.header.dwsize = API_FRAME_SIZE_IN_DWORDS;
+
+	mes_gang_submit_pkt.set_gang_submit.gang_context_addr =
+		input->primary_gang_context_addr;
+	mes_gang_submit_pkt.set_gang_submit.slave_gang_context_addr =
+		input->secondary_gang_context_addr;
+	mes_gang_submit_pkt.set_gang_submit.gang_context_array_index =
+		input->primary_gang_context_array_idx;
+	mes_gang_submit_pkt.set_gang_submit.slave_gang_context_array_index =
+		input->secondary_gang_context_array_idx;
+
+	return mes_v12_0_submit_pkt_and_poll_completion(mes, AMDGPU_MES_SCHED_PIPE,
+			&mes_gang_submit_pkt, sizeof(mes_gang_submit_pkt),
+			offsetof(union MESAPI__SET_GANG_SUBMIT, api_status));
 }
 
 /*
@@ -1367,6 +1391,7 @@ static const struct amdgpu_mes_funcs mes_v12_0_funcs = {
 	.invalidate_tlbs_pasid = mes_v12_0_inv_tlbs_pasid,
 	.detect_and_reset_hung_queues = mes_v12_0_detect_and_reset_hung_queues,
 	.enable_dbgext_irq = mes_v12_0_enable_dbgext_irq,
+	.set_gang_submit = mes_v12_0_set_gang_submit,
 };
 
 static int mes_v12_0_allocate_ucode_buffer(struct amdgpu_device *adev,
Signature · 1 line
2.56.1
author maintainer
#5 lore raw

PATCH 6/11 drm/amdgpu/userq: add callback for gang setup

In reply to Alex Deucher

Two queues can be ganged and will schedule together.

Cc Yogesh Mohan Marimuthu <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h | 2 ++
 1 file changed, 2 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +2 -0
index 488dc21d7c81b..efaa658a26a9a 100644
@@ -115,6 +115,8 @@ struct amdgpu_userq_funcs {
 	int (*preempt)(struct amdgpu_usermode_queue *queue);
 	int (*restore)(struct amdgpu_usermode_queue *queue);
 	int (*reset)(struct amdgpu_usermode_queue *queue);
+	int (*set_gang)(struct amdgpu_usermode_queue *primary_queue,
+			struct amdgpu_usermode_queue *secondary_queue);
 };
 
 /* Usermode queues for gfx */
Signature · 1 line
2.56.1
author maintainer
#6 lore raw

PATCH 7/11 drm/amdgpu/userq/mes: implement set gang submit

In reply to Alex Deucher

Enable the userq callback for setting up gang submission.

v2: fix context addr after rebase, array indices

Cc Yogesh Mohan Marimuthu <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 31 ++++++++++++++++++++++
 1 file changed, 31 insertions(+)
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +31 -0
index 7abb9d95ef59a..68c10a8adcd20 100644
@@ -649,6 +649,36 @@ static int mes_userq_restore(struct amdgpu_usermode_queue *queue)
 	return r;
 }
 
+static int mes_set_gang(struct amdgpu_usermode_queue *primary_queue,
+			struct amdgpu_usermode_queue *secondary_queue)
+{
+	struct amdgpu_userq_obj *primary_ctx = &primary_queue->fw_obj;
+	struct amdgpu_userq_obj *secondary_ctx = &secondary_queue->fw_obj;
+	struct amdgpu_userq_mgr *uq_mgr = primary_queue->userq_mgr;
+	struct amdgpu_device *adev = uq_mgr->adev;
+	struct mes_set_gang_submit_input input;
+	struct amdgpu_mes *mes = &adev->mes;
+	int r;
+
+	memset(&input, 0x0, sizeof(struct mes_set_gang_submit_input));
+
+	input.primary_gang_context_addr = primary_ctx->gpu_addr;
+	input.secondary_gang_context_addr = secondary_ctx->gpu_addr;
+	if (mes->use_rs64mem) {
+		input.primary_gang_context_array_idx =
+			primary_queue->gang_ctx_array_index;
+		input.secondary_gang_context_array_idx =
+			secondary_queue->gang_ctx_array_index;
+	}
+
+	amdgpu_mes_lock(&adev->mes);
+	r = adev->mes.funcs->set_gang_submit(&adev->mes, &input);
+	amdgpu_mes_unlock(&adev->mes);
+	if (r)
+		dev_err(adev->dev, "Failed to set gang submit, err (%d)\n", r);
+	return r;
+}
+
 const struct amdgpu_userq_funcs userq_mes_funcs = {
 	.mqd_create = mes_userq_mqd_create,
 	.mqd_destroy = mes_userq_mqd_destroy,
@@ -657,4 +687,5 @@ const struct amdgpu_userq_funcs userq_mes_funcs = {
 	.preempt = mes_userq_preempt,
 	.restore = mes_userq_restore,
 	.reset = mes_userq_reset,
+	.set_gang = mes_set_gang,
 };
Signature · 1 line
2.56.1
#7 lore raw

Re: [PATCH 07/11] drm/amdgpu/userq/mes: implement set gang submit

Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce: - [High] drm/amdgpu: gang context array index…

Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce:
- [High] drm/amdgpu: gang context array index leak in mes_userq_map()
[1]
--

commit d0ceeae2277f6c5db3f1b3901e53f85f8174b593
Author: Alex Deucher <[email protected]>

drm/amdgpu/userq/mes: implement set gang submit

This commit implements the .set_gang callback in userq_mes_funcs to support gang submission for MES usermode queues.

Quoted 19 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +11 -0
index 7abb9d95ef59a..68c10a8adcd20 100644
@@ -649,6 +649,36 @@ static int mes_userq_restore(struct amdgpu_usermode_queue *queue)
 	return r;
 }
 
+static int mes_set_gang(struct amdgpu_usermode_queue *primary_queue,
+			struct amdgpu_usermode_queue *secondary_queue)
+{
+	struct amdgpu_userq_obj *primary_ctx = &primary_queue->fw_obj;
+	struct amdgpu_userq_obj *secondary_ctx = &secondary_queue->fw_obj;
+	struct amdgpu_userq_mgr *uq_mgr = primary_queue->userq_mgr;
+	struct amdgpu_device *adev = uq_mgr->adev;
+	struct mes_set_gang_submit_input input;
+	struct amdgpu_mes *mes = &adev->mes;
+	int r;
+

drivers/gpu/drm/amd/amdgpu/mes_userqueue.c static int mes_userq_restore(struct amdgpu_usermode_queue *queue) · line 672

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +10 -0
+	memset(&input, 0x0, sizeof(struct mes_set_gang_submit_input));
+
+	input.primary_gang_context_addr = primary_ctx->gpu_addr;
+	input.secondary_gang_context_addr = secondary_ctx->gpu_addr;
+	if (mes->use_rs64mem) {
+		input.primary_gang_context_array_idx =
+			primary_queue->gang_ctx_array_index;
+		input.secondary_gang_context_array_idx =
+			secondary_queue->gang_ctx_array_index;
+	}

[Severity: High]
This problem wasn't introduced by this patch, but does mes_userq_map() leak queue->gang_ctx_array_index [1] when add_hw_queue() fails?

drivers/gpu/drm/amd/amdgpu/mes_userqueue.c:mes_userq_map() {
	...
	if (mes->use_rs64mem) {
		...
		r = amdgpu_mes_alloc_gang_ctx_index(mes, &queue->gang_ctx_array_index);
		if (r) {
			DRM_ERROR("Failed to allocate userq gang index err:%d\n", r);
			return r;
		}
		queue_input.process_context_array_index = uq_mgr->proc_ctx_array_index;
		queue_input.gang_context_array_index = queue->gang_ctx_array_index;
	}

	amdgpu_mes_lock(&adev->mes);
	r = adev->mes.funcs->add_hw_queue(&adev->mes, &queue_input);
	amdgpu_mes_unlock(&adev->mes);
	if (r) {
		DRM_ERROR("Failed to map queue in HW, err (%d)\n", r);
		return r;
	}
	...
}

When add_hw_queue() fails during amdgpu_userq_create(), mes_userq_map() returns without calling amdgpu_mes_free_gang_ctx_index(), and the caller's error cleanup does not call mes_userq_unmap(). Repeated mapping failures can exhaust the gang context bitmap and prevent creating new MES user queues.

[1] https://sashiko.dev/#/bug/linux-70b9dde6-e881-480d-9c56-534a231e5249

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
author maintainer
#8 lore raw

PATCH 8/11 drm/amdgpu/userq: add handling for set_gang

In reply to Alex Deucher

Make sure the ganged queues are handled properly in map, unmap, restore, and preempt. Need to call set_gang to make sure the queues are schedule together.

v2: rework all of the logic

Cc Yogesh Mohan Marimuthu <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 40 +++++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h |  2 ++
 2 files changed, 42 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +40 -0
index ed6f3b67e8720..3afd17af28548 100644
@@ -303,6 +303,27 @@ static bool amdgpu_userq_buffer_vas_mapped(struct amdgpu_usermode_queue *queue)
 	return true;
 }
 
+static int amdgpu_userq_set_gang_helper(struct amdgpu_usermode_queue *queue)
+{
+	struct amdgpu_usermode_queue *secondary_queue = queue->gang_secondary;
+	struct amdgpu_userq_mgr *uq_mgr = queue->userq_mgr;
+	struct amdgpu_device *adev = uq_mgr->adev;
+	const struct amdgpu_userq_funcs *userq_funcs =
+		adev->userq_funcs[queue->queue_type];
+	int r;
+
+	if (secondary_queue &&
+	    (queue->state == AMDGPU_USERQ_STATE_MAPPED ||
+	     queue->state == AMDGPU_USERQ_STATE_PREEMPTED) &&
+	    (secondary_queue->state == AMDGPU_USERQ_STATE_MAPPED ||
+	     secondary_queue->state == AMDGPU_USERQ_STATE_PREEMPTED)) {
+		r = userq_funcs->set_gang(queue, secondary_queue);
+		if (r)
+			return r;
+	}
+	return 0;
+}
+
 static int amdgpu_userq_preempt_helper(struct amdgpu_usermode_queue *queue)
 {
 	struct amdgpu_userq_mgr *uq_mgr = queue->userq_mgr;
@@ -968,6 +989,11 @@ amdgpu_userq_restore_all(struct amdgpu_userq_mgr *uq_mgr)
 		if (r)
 			ret = r;
 	}
+	xa_for_each(&uq_mgr->userq_xa, queue_id, queue) {
+		r = amdgpu_userq_set_gang_helper(queue);
+		if (r)
+			ret = r;
+	}
 	mutex_unlock(&uq_mgr->userq_mutex);
 
 	if (ret)
@@ -1446,6 +1472,13 @@ int amdgpu_userq_resume(struct amdgpu_device *adev)
 		if (r)
 			return r;
 	}
+	xa_for_each(&adev->userq_doorbell_xa, queue_id, queue) {
+		uqm = queue->userq_mgr;
+		guard(mutex)(&uqm->userq_mutex);
+		r = amdgpu_userq_set_gang_helper(queue);
+		if (r)
+			return r;
+	}
 
 	return 0;
 }
@@ -1591,6 +1624,13 @@ int amdgpu_userq_post_reset(struct amdgpu_device *adev, bool vram_lost)
 			queue->state = AMDGPU_USERQ_STATE_MAPPED;
 		}
 	}
+	xa_for_each(&adev->userq_doorbell_xa, queue_id, queue) {
+		r = amdgpu_userq_set_gang_helper(queue);
+		if (r) {
+			dev_err(adev->dev, "Failed to gang queue %ld\n", queue_id);
+			ret = r;
+		}
+	}
 
 	return ret;
 }
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +2 -0
index efaa658a26a9a..145d53363ed2e 100644
@@ -102,6 +102,8 @@ struct amdgpu_usermode_queue {
 	} userq_vas;
 
 	uint32_t	gang_ctx_array_index;
+	struct amdgpu_usermode_queue *gang_primary;
+	struct amdgpu_usermode_queue *gang_secondary;
 };
 
 struct amdgpu_userq_funcs {
Signature · 1 line
2.56.1
#9 lore raw

Re: [PATCH 08/11] drm/amdgpu/userq: add handling for set_gang

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] drm/amdgpu/userq: missing reset_domain lock in amdgpu_userq_set_gang_helper() - [Medium]…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] drm/amdgpu/userq: missing reset_domain lock in amdgpu_userq_set_gang_helper()
- [Medium] drm/amdgpu/userq: invalid set_gang call in amdgpu_userq_post_reset()
--

commit c119a0781eef4bb00ce5e6c3043c15d765972cac
Author: Alex Deucher <[email protected]>

drm/amdgpu/userq: add handling for set_gang

This commit adds gang primary/secondary pointers to struct amdgpu_usermode_queue and introduces amdgpu_userq_set_gang_helper() to re-establish queue gangs during user queue restore, resume, and post-reset recovery.

Quoted 48 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +26 -0
index ed6f3b67e8720..3afd17af28548 100644
@@ -303,6 +303,27 @@ static bool amdgpu_userq_buffer_vas_mapped(struct amdgpu_usermode_queue *queue)
 	return true;
 }
 
+static int amdgpu_userq_set_gang_helper(struct amdgpu_usermode_queue *queue)
+{
+	struct amdgpu_usermode_queue *secondary_queue = queue->gang_secondary;
+	struct amdgpu_userq_mgr *uq_mgr = queue->userq_mgr;
+	struct amdgpu_device *adev = uq_mgr->adev;
+	const struct amdgpu_userq_funcs *userq_funcs =
+		adev->userq_funcs[queue->queue_type];
+	int r;
+
+	if (secondary_queue &&
+	    (queue->state == AMDGPU_USERQ_STATE_MAPPED ||
+	     queue->state == AMDGPU_USERQ_STATE_PREEMPTED) &&
+	    (secondary_queue->state == AMDGPU_USERQ_STATE_MAPPED ||
+	     secondary_queue->state == AMDGPU_USERQ_STATE_PREEMPTED)) {
+		r = userq_funcs->set_gang(queue, secondary_queue);
+		if (r)
+			return r;
+	}
+	return 0;
+}
+
 static int amdgpu_userq_preempt_helper(struct amdgpu_usermode_queue *queue)
 {
 	struct amdgpu_userq_mgr *uq_mgr = queue->userq_mgr;
@@ -968,6 +989,11 @@ amdgpu_userq_restore_all(struct amdgpu_userq_mgr *uq_mgr)
 		if (r)
 			ret = r;
 	}
+	xa_for_each(&uq_mgr->userq_xa, queue_id, queue) {
+		r = amdgpu_userq_set_gang_helper(queue);
+		if (r)
+			ret = r;
+	}
 	mutex_unlock(&uq_mgr->userq_mutex);
 
 	if (ret)
@@ -1446,6 +1472,13 @@ int amdgpu_userq_resume(struct amdgpu_device *adev)
 		if (r)
 			return r;
 	}

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_resume(struct amdgpu_device *adev) · line 1484

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +7 -0
+	xa_for_each(&adev->userq_doorbell_xa, queue_id, queue) {
+		uqm = queue->userq_mgr;
+		guard(mutex)(&uqm->userq_mutex);
+		r = amdgpu_userq_set_gang_helper(queue);
+		if (r)
+			return r;
+	}
 
 	return 0;
 }

[Severity: Medium]
Should callers on the non-reset paths hold a read lock on adev->reset_domain->sem across amdgpu_userq_set_gang_helper()?

In amdgpu_userq_restore_all() and amdgpu_userq_resume(), the first loop calls amdgpu_userq_map_helper(), which takes and releases adev->reset_domain->sem per queue:

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c:amdgpu_userq_map_helper() {
    ...
	/* The reset changes the queue state and the MES is unusable meanwhile */
	guard(rwsem_read)(&adev->reset_domain->sem);
    ...
}

When the second loop calls amdgpu_userq_set_gang_helper(), adev->reset_domain->sem is no longer held. If a GPU reset starts between the two loops, amdgpu_userq_pre_reset() and amdgpu_userq_post_reset() mutate queue->state, secondary_queue->state, and gang_ctx_array_index under the write side of adev->reset_domain->sem without taking uq_mgr->userq_mutex:

kworker (uq_mgr->resume_work) GPU reset thread
amdgpu_userq_restore_all()
mutex_lock(&uq_mgr->userq_mutex)
amdgpu_userq_map_helper(queue)

    down_read(&reset_domain->sem)
    ...
    up_read(&reset_domain->sem)
                                  down_write(&reset_domain->sem)
                                  amdgpu_userq_pre_reset()
                                    userq_funcs->unmap(queue)
                                    queue->state = AMDGPU_USERQ_STATE_HUNG
  amdgpu_userq_set_gang_helper()
    userq_funcs->set_gang()       <-- submits to MES during reset

Calling userq_funcs->set_gang() without adev->reset_domain->sem held also triggers lockdep_assert_held(&adev->reset_domain->sem) in amdgpu_device_skip_hw_access() when ringing the MES doorbell. Note that amdgpu_userq_post_reset() also calls amdgpu_userq_set_gang_helper() while already holding adev->reset_domain->sem for write, so taking the read lock inside amdgpu_userq_set_gang_helper() itself would deadlock the reset path.

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -0
@@ -1591,6 +1624,13 @@ int amdgpu_userq_post_reset(struct amdgpu_device *adev, bool vram_lost)
 			queue->state = AMDGPU_USERQ_STATE_MAPPED;
 		}
 	}

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_post_reset(struct amdgpu_device *adev, bool vram_lost) · line 1636

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +7 -0
+	xa_for_each(&adev->userq_doorbell_xa, queue_id, queue) {
+		r = amdgpu_userq_set_gang_helper(queue);
+		if (r) {
+			dev_err(adev->dev, "Failed to gang queue %ld\n", queue_id);
+			ret = r;
+		}
+	}
 
 	return ret;
 }

[Severity: Medium]
Can amdgpu_userq_post_reset() call userq_funcs->set_gang() on queues that were not re-added to MES after the reset?

During a GPU reset, amdgpu_userq_pre_reset() only transitions AMDGPU_USERQ_STATE_MAPPED queues to AMDGPU_USERQ_STATE_HUNG, leaving any preempted queues in AMDGPU_USERQ_STATE_PREEMPTED while MES hardware state is wiped. In amdgpu_userq_post_reset(), the first loop only remaps queues whose state is AMDGPU_USERQ_STATE_HUNG when !vram_lost.

Because the second loop runs unconditionally (even when vram_lost is true) and amdgpu_userq_set_gang_helper() accepts queues in AMDGPU_USERQ_STATE_PREEMPTED, it will issue MES SET_GANG_SUBMIT commands for preempted queues that have not been mapped in MES after the reset, causing MES command failures and failing amdgpu_userq_post_reset().

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
author maintainer
#10 lore raw

PATCH 9/11 drm/amdgpu: add UAPI to create user queue gangs

In reply to Alex Deucher

Queues in a gang will schedule together.

Cc Yogesh Mohan Marimuthu <[email protected]>
---
 include/uapi/drm/amdgpu_drm.h | 11 +++++++++++
 1 file changed, 11 insertions(+)
include/uapi/drm/amdgpu_drm.h +11 -0
index 86d42a5d683c0..bfb982f1d1757 100644
@@ -340,6 +340,7 @@ union drm_amdgpu_ctx {
 /* user queue IOCTL operations */
 #define AMDGPU_USERQ_OP_CREATE	1
 #define AMDGPU_USERQ_OP_FREE	2
+#define AMDGPU_USERQ_OP_CREATE_GANG	3
 
 /* queue priority levels */
 /* low < normal low < normal high < high */
@@ -422,6 +423,15 @@ struct drm_amdgpu_userq_in {
 	__u64 mqd_size;
 };
 
+struct drm_amdgpu_userq_in_create_gang {
+	/** AMDGPU_USERQ_OP_* */
+	__u32	op;
+	__u32	pad;
+	/** Queue ids passed for operation USERQ_OP_CREATE_GANG */
+	__u32	primary_queue_id;
+	__u32	secondary_queue_id;
+};
+
 /* The structure to carry output of userqueue ops */
 struct drm_amdgpu_userq_out {
 	/**
@@ -435,6 +445,7 @@ struct drm_amdgpu_userq_out {
 
 union drm_amdgpu_userq {
 	struct drm_amdgpu_userq_in in;
+	struct drm_amdgpu_userq_in_create_gang in_cg;
 	struct drm_amdgpu_userq_out out;
 };
 
Signature · 1 line
2.56.1
#11 lore raw

Re: [PATCH 09/11] drm/amdgpu: add UAPI to create user queue gangs

AMD General

MES firmware names the ganged queues as slave and master. Mesa radv calls it as follower and leader.

Can the primary/secondary naming please be changed into and existing name slave/master.

Thank you,
Yogesh

________________________________
From: Alex Deucher <[email protected]>
Sent: Thursday, November 19, 2026 4:07 AM
To: [email protected] <[email protected]>
Cc: Deucher, Alexander <[email protected]>; Mohan Marimuthu, Yogesh <[email protected]>
Subject: [PATCH 09/11] drm/amdgpu: add UAPI to create user queue gangs

Queues in a gang will schedule together.

Cc Yogesh Mohan Marimuthu <[email protected]>
---
 include/uapi/drm/amdgpu_drm.h | 11 +++++++++++
 1 file changed, 11 insertions(+)
include/uapi/drm/amdgpu_drm.h +11 -0
index 86d42a5d683c0..bfb982f1d1757 100644
@@ -340,6 +340,7 @@ union drm_amdgpu_ctx {
 /* user queue IOCTL operations */
 #define AMDGPU_USERQ_OP_CREATE  1
 #define AMDGPU_USERQ_OP_FREE    2
+#define AMDGPU_USERQ_OP_CREATE_GANG    3
 
 /* queue priority levels */
 /* low < normal low < normal high < high */
@@ -422,6 +423,15 @@ struct drm_amdgpu_userq_in {
         __u64 mqd_size;
 };
 
+struct drm_amdgpu_userq_in_create_gang {
+       /** AMDGPU_USERQ_OP_* */
+       __u32   op;
+       __u32   pad;
+       /** Queue ids passed for operation USERQ_OP_CREATE_GANG */
+       __u32   primary_queue_id;
+       __u32   secondary_queue_id;
+};
+
 /* The structure to carry output of userqueue ops */
 struct drm_amdgpu_userq_out {
         /**
@@ -435,6 +445,7 @@ struct drm_amdgpu_userq_out {
 
 union drm_amdgpu_userq {
         struct drm_amdgpu_userq_in in;
+       struct drm_amdgpu_userq_in_create_gang in_cg;
         struct drm_amdgpu_userq_out out;
 };
 

--
2.56.1

#12 lore raw

Re: [PATCH 09/11] drm/amdgpu: add UAPI to create user queue gangs

Quoted 6 lines · Yogesh

AMD General

MES firmware names the ganged queues as slave and master. Mesa radv calls it as follower and leader.

Can the primary/secondary naming please be changed into and existing name slave/master.

I went with primary and secondary because some people find master/slave objectionable. There was an effort in the kernel a few years ago to remove the usage of those terms. If you feel strongly, I can change them to align with mesa.

Alex

Quoted 58 lines

Thank you,
Yogesh

________________________________
From: Alex Deucher <[email protected]>
Sent: Thursday, November 19, 2026 4:07 AM
To: [email protected] <[email protected]>
Cc: Deucher, Alexander <[email protected]>; Mohan Marimuthu, Yogesh <[email protected]>
Subject: [PATCH 09/11] drm/amdgpu: add UAPI to create user queue gangs

Queues in a gang will schedule together.

Cc Yogesh Mohan Marimuthu <[email protected]>
---
 include/uapi/drm/amdgpu_drm.h | 11 +++++++++++
 1 file changed, 11 insertions(+)
include/uapi/drm/amdgpu_drm.h +11 -1
index 86d42a5d683c0..bfb982f1d1757 100644
@@ -340,6 +340,7 @@ union drm_amdgpu_ctx {
 /* user queue IOCTL operations */
 #define AMDGPU_USERQ_OP_CREATE  1
 #define AMDGPU_USERQ_OP_FREE    2
+#define AMDGPU_USERQ_OP_CREATE_GANG    3
 
 /* queue priority levels */
 /* low < normal low < normal high < high */
@@ -422,6 +423,15 @@ struct drm_amdgpu_userq_in {
         __u64 mqd_size;
 };
 
+struct drm_amdgpu_userq_in_create_gang {
+       /** AMDGPU_USERQ_OP_* */
+       __u32   op;
+       __u32   pad;
+       /** Queue ids passed for operation USERQ_OP_CREATE_GANG */
+       __u32   primary_queue_id;
+       __u32   secondary_queue_id;
+};
+
 /* The structure to carry output of userqueue ops */
 struct drm_amdgpu_userq_out {
         /**
@@ -435,6 +445,7 @@ struct drm_amdgpu_userq_out {
 
 union drm_amdgpu_userq {
         struct drm_amdgpu_userq_in in;
+       struct drm_amdgpu_userq_in_create_gang in_cg;
         struct drm_amdgpu_userq_out out;
 };
 
--

2.56.1

#13 lore raw

Re: [PATCH 09/11] drm/amdgpu: add UAPI to create user queue gangs

Public

Hi Alex,

Yes, please align Kernel to mesa radv follower/leader terminology.

Thank you,
Yogesh

________________________________
From: Alex Deucher <[email protected]>
Sent: Saturday, November 21, 2026 12:58 AM
To: Mohan Marimuthu, Yogesh <[email protected]>
Cc: Deucher, Alexander <[email protected]>; [email protected] <[email protected]>
Subject: Re: [PATCH 09/11] drm/amdgpu: add UAPI to create user queue gangs

Quoted 6 lines · Yogesh

AMD General

MES firmware names the ganged queues as slave and master. Mesa radv calls it as follower and leader.

Can the primary/secondary naming please be changed into and existing name slave/master.

I went with primary and secondary because some people find master/slave objectionable. There was an effort in the kernel a few years ago to remove the usage of those terms. If you feel strongly, I can change them to align with mesa.

Alex

Quoted 58 lines

Thank you,
Yogesh

________________________________
From: Alex Deucher <[email protected]>
Sent: Thursday, November 19, 2026 4:07 AM
To: [email protected] <[email protected]>
Cc: Deucher, Alexander <[email protected]>; Mohan Marimuthu, Yogesh <[email protected]>
Subject: [PATCH 09/11] drm/amdgpu: add UAPI to create user queue gangs

Queues in a gang will schedule together.

Cc Yogesh Mohan Marimuthu <[email protected]>
---
 include/uapi/drm/amdgpu_drm.h | 11 +++++++++++
 1 file changed, 11 insertions(+)
include/uapi/drm/amdgpu_drm.h +11 -1
index 86d42a5d683c0..bfb982f1d1757 100644
@@ -340,6 +340,7 @@ union drm_amdgpu_ctx {
 /* user queue IOCTL operations */
 #define AMDGPU_USERQ_OP_CREATE  1
 #define AMDGPU_USERQ_OP_FREE    2
+#define AMDGPU_USERQ_OP_CREATE_GANG    3
 
 /* queue priority levels */
 /* low < normal low < normal high < high */
@@ -422,6 +423,15 @@ struct drm_amdgpu_userq_in {
         __u64 mqd_size;
 };
 
+struct drm_amdgpu_userq_in_create_gang {
+       /** AMDGPU_USERQ_OP_* */
+       __u32   op;
+       __u32   pad;
+       /** Queue ids passed for operation USERQ_OP_CREATE_GANG */
+       __u32   primary_queue_id;
+       __u32   secondary_queue_id;
+};
+
 /* The structure to carry output of userqueue ops */
 struct drm_amdgpu_userq_out {
         /**
@@ -435,6 +445,7 @@ struct drm_amdgpu_userq_out {
 
 union drm_amdgpu_userq {
         struct drm_amdgpu_userq_in in;
+       struct drm_amdgpu_userq_in_create_gang in_cg;
         struct drm_amdgpu_userq_out out;
 };
 
--

2.56.1

author maintainer
#14 lore raw

PATCH 10/11 drm/amdgpu/userq: enable support for creating gangs

In reply to Alex Deucher

Gangs are sets of userqs that schedule together. You specify the primary and secondary queues and the scheduler will make sure they always run at the same time.

v2: handle setting up the gang again after unmaps
v3: split the UAPI part from the internal tracking
v4: rework

Cc Yogesh Mohan Marimuthu <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 122 +++++++++++++++++++++-
 1 file changed, 121 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +121 -1
index 3afd17af28548..388385380d8bc 100644
@@ -566,6 +566,7 @@ amdgpu_userq_destroy(struct amdgpu_userq_mgr *uq_mgr, struct amdgpu_usermode_que
 {
 	struct amdgpu_device *adev = uq_mgr->adev;
 	const struct amdgpu_userq_funcs *uq_funcs = adev->userq_funcs[queue->queue_type];
+	struct amdgpu_usermode_queue *other_gang_queue;
 	int r = 0;
 
 	trace_amdgpu_userq_destroy_start(queue);
@@ -585,6 +586,17 @@ amdgpu_userq_destroy(struct amdgpu_userq_mgr *uq_mgr, struct amdgpu_usermode_que
 	debugfs_remove_recursive(queue->debugfs_queue);
 #endif
 	r = amdgpu_userq_unmap_helper(queue);
+	/* clean up gangs */
+	if (queue->gang_primary) {
+		other_gang_queue = queue->gang_primary;
+		other_gang_queue->gang_secondary = NULL;
+		queue->gang_primary = NULL;
+	}
+	if (queue->gang_secondary) {
+		other_gang_queue = queue->gang_secondary;
+		other_gang_queue->gang_primary = NULL;
+		queue->gang_secondary = NULL;
+	}
 	atomic_dec(&uq_mgr->userq_count[queue->queue_type]);
 	amdgpu_userq_fence_driver_free(queue);
 	queue->fence_drv = NULL;
@@ -838,6 +850,106 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
 	return r;
 }
 
+static int
+amdgpu_userq_create_gang(struct drm_file *filp, union drm_amdgpu_userq *args)
+{
+	struct amdgpu_usermode_queue *primary_queue, *secondary_queue;
+	struct amdgpu_fpriv *fpriv = filp->driver_priv;
+	struct amdgpu_userq_mgr *uq_mgr = &fpriv->userq_mgr;
+	const struct amdgpu_userq_funcs *userq_funcs;
+	struct amdgpu_device *adev = uq_mgr->adev;
+	int primary_queue_id = args->in_cg.primary_queue_id;
+	int secondary_queue_id = args->in_cg.secondary_queue_id;
+	int r;
+
+	/* The reset changes the queue state and the MES is unusable meanwhile */
+	guard(rwsem_read)(&adev->reset_domain->sem);
+
+	mutex_lock(&uq_mgr->userq_mutex);
+	primary_queue = amdgpu_userq_get(uq_mgr, primary_queue_id);
+	secondary_queue = amdgpu_userq_get(uq_mgr, secondary_queue_id);
+
+	if (!primary_queue) {
+		drm_file_err(uq_mgr->file, "Invalid gang primary queue id\n");
+		r = -EINVAL;
+		goto out;
+	}
+
+	if (!secondary_queue) {
+		drm_file_err(uq_mgr->file, "Invalid gang secondary queue id\n");
+		r = -EINVAL;
+		goto out;
+	}
+
+	if (primary_queue == secondary_queue) {
+		drm_file_err(uq_mgr->file, "Trying to gang the same queue\n");
+		r = -EINVAL;
+		goto out;
+	}
+
+	if ((primary_queue->queue_type != AMDGPU_HW_IP_GFX) &&
+	    (primary_queue->queue_type != AMDGPU_HW_IP_COMPUTE) &&
+	    (primary_queue->queue_type != AMDGPU_HW_IP_DMA)) {
+		drm_file_err(uq_mgr->file, "Invalid gang primary queue type\n");
+		r = -EINVAL;
+		goto out;
+	}
+
+	if (primary_queue->gang_primary || primary_queue->gang_secondary) {
+		drm_file_err(uq_mgr->file, "Primary queue already in a gang\n");
+		r = -EINVAL;
+		goto out;
+	}
+	if (primary_queue->state == AMDGPU_USERQ_STATE_HUNG) {
+		drm_file_err(uq_mgr->file, "Primary queue is hung\n");
+		r = -EINVAL;
+		goto out;
+	}
+
+	if ((secondary_queue->queue_type != AMDGPU_HW_IP_GFX) &&
+	    (secondary_queue->queue_type != AMDGPU_HW_IP_COMPUTE) &&
+	    (secondary_queue->queue_type != AMDGPU_HW_IP_DMA)) {
+		drm_file_err(uq_mgr->file, "Invalid gang secondary queue type\n");
+		r = -EINVAL;
+		goto out;
+	}
+
+	if (secondary_queue->gang_primary || secondary_queue->gang_secondary) {
+		drm_file_err(uq_mgr->file, "Secondary queue already in a gang\n");
+		r = -EINVAL;
+		goto out;
+	}
+
+	if (secondary_queue->state == AMDGPU_USERQ_STATE_HUNG) {
+		drm_file_err(uq_mgr->file, "Secondary queue is hung\n");
+		r = -EINVAL;
+		goto out;
+	}
+
+	userq_funcs = adev->userq_funcs[primary_queue->queue_type];
+	if ((primary_queue->state == AMDGPU_USERQ_STATE_MAPPED ||
+	     primary_queue->state == AMDGPU_USERQ_STATE_PREEMPTED) &&
+	    (secondary_queue->state == AMDGPU_USERQ_STATE_MAPPED ||
+	     secondary_queue->state == AMDGPU_USERQ_STATE_PREEMPTED))
+		r = userq_funcs->set_gang(primary_queue, secondary_queue);
+	else
+		/* if the queues are currently unmapped, the gang state will get
+		 * updated when they get mapped.
+		 */
+		r = 0;
+	if (!r) {
+		primary_queue->gang_secondary = secondary_queue;
+		secondary_queue->gang_primary = primary_queue;
+	}
+
+out:
+	mutex_unlock(&uq_mgr->userq_mutex);
+	amdgpu_userq_put(primary_queue);
+	amdgpu_userq_put(secondary_queue);
+
+	return r;
+}
+
 static int amdgpu_userq_input_args_validate(struct drm_device *dev,
 					union drm_amdgpu_userq *args,
 					struct drm_file *filp)
@@ -907,6 +1019,10 @@ static int amdgpu_userq_input_args_validate(struct drm_device *dev,
 		    args->in.mqd_size)
 			return -EINVAL;
 		break;
+	case AMDGPU_USERQ_OP_CREATE_GANG:
+		if (args->in_cg.pad)
+			return -EINVAL;
+		break;
 	default:
 		return -EINVAL;
 	}
@@ -958,7 +1074,11 @@ int amdgpu_userq_ioctl(struct drm_device *dev, void *data,
 		amdgpu_userq_put(queue);
 		break;
 	}
-
+	case AMDGPU_USERQ_OP_CREATE_GANG:
+		r = amdgpu_userq_create_gang(filp, args);
+		if (r)
+			drm_file_err(filp, "Failed to create usermode queue gang\n");
+		break;
 	default:
 		drm_dbg_driver(dev, "Invalid user queue op specified: %d\n", args->in.op);
 		return -EINVAL;
Signature · 1 line
2.56.1
#15 lore raw

Re: [PATCH 10/11] drm/amdgpu/userq: enable support for creating gangs

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [High] drm/amdgpu/userq: lock order inversion in amdgpu_userq_create_gang() - [High] drm/amdgpu/userq: race…

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [High] drm/amdgpu/userq: lock order inversion in amdgpu_userq_create_gang()
- [High] drm/amdgpu/userq: race between amdgpu_userq_destroy() and post_reset()
- [Medium] drm/amdgpu/userq: missing INVALID_VA check in amdgpu_userq_create_gang()
--

commit 8106c51d4a0229062b3c666f8f9004011c7d6501
Author: Alex Deucher <[email protected]>

drm/amdgpu/userq: enable support for creating gangs

This commit adds the AMDGPU_USERQ_OP_CREATE_GANG ioctl operation to link primary and secondary usermode queues into a gang and cleans up the gang links when a queue is destroyed.

Quoted 20 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5 -0
index 3afd17af28548..388385380d8bc 100644
@@ -566,6 +566,7 @@ amdgpu_userq_destroy(struct amdgpu_userq_mgr *uq_mgr, struct amdgpu_usermode_que
 {
 	struct amdgpu_device *adev = uq_mgr->adev;
 	const struct amdgpu_userq_funcs *uq_funcs = adev->userq_funcs[queue->queue_type];
+	struct amdgpu_usermode_queue *other_gang_queue;
 	int r = 0;
 
 	trace_amdgpu_userq_destroy_start(queue);
@@ -585,6 +586,17 @@ amdgpu_userq_destroy(struct amdgpu_userq_mgr *uq_mgr, struct amdgpu_usermode_que
 	debugfs_remove_recursive(queue->debugfs_queue);
 #endif
 	r = amdgpu_userq_unmap_helper(queue);
+	/* clean up gangs */
+	if (queue->gang_primary) {
+		other_gang_queue = queue->gang_primary;
+		other_gang_queue->gang_secondary = NULL;

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_destroy(struct amdgpu_userq_mgr *uq_mgr, struct amdgpu_usermode_que · line 602

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +7 -0
+		queue->gang_primary = NULL;
+	}
+	if (queue->gang_secondary) {
+		other_gang_queue = queue->gang_secondary;
+		other_gang_queue->gang_primary = NULL;
+		queue->gang_secondary = NULL;
+	}
 	atomic_dec(&uq_mgr->userq_count[queue->queue_type]);
 	amdgpu_userq_fence_driver_free(queue);
 	queue->fence_drv = NULL;

[Severity: High]
Can amdgpu_userq_destroy() race with amdgpu_userq_post_reset() on the partner queue's gang_secondary pointer and cause a use-after-free?

amdgpu_userq_destroy() holds uq_mgr->userq_mutex, while amdgpu_userq_detach_doorbell() and amdgpu_userq_unmap_helper() each take and release adev->reset_domain->sem individually. During a GPU reset, amdgpu_userq_post_reset() iterates adev->userq_doorbell_xa under down_write(&adev->reset_domain->sem) without taking uq_mgr->userq_mutex:

CPU0 CPU1 (GPU reset)
amdgpu_userq_destroy(secondary)
mutex_lock(&uq_mgr->userq_mutex)
amdgpu_userq_detach_doorbell()
amdgpu_userq_unmap_helper()

    /* releases reset_domain->sem */
                                      down_write(&adev->reset_domain->sem)
                                      amdgpu_userq_post_reset()
                                        amdgpu_userq_set_gang_helper(primary)
                                          sec = primary->gang_secondary
  primary->gang_secondary = NULL
  mutex_unlock(&uq_mgr->userq_mutex)
  uq_funcs->mqd_destroy(secondary)
  kfree(secondary)
                                          if (sec->state == ...)
                                            <-- use-after-free

In addition, if the reset begins between amdgpu_userq_detach_doorbell() and amdgpu_userq_unmap_helper(), secondary is already erased from adev->userq_doorbell_xa while still in AMDGPU_USERQ_STATE_MAPPED, and amdgpu_userq_destroy() blocks on reset_domain->sem inside amdgpu_userq_unmap_helper() before clearing primary->gang_secondary. amdgpu_userq_post_reset() then remaps primary and calls userq_funcs->set_gang(primary, secondary) on a secondary queue that was never remapped after the reset and is about to be destroyed.

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +6 -0
@@ -838,6 +850,106 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
 	return r;
 }
 
+static int
+amdgpu_userq_create_gang(struct drm_file *filp, union drm_amdgpu_userq *args)
+{
+	struct amdgpu_usermode_queue *primary_queue, *secondary_queue;
+	struct amdgpu_fpriv *fpriv = filp->driver_priv;
+	struct amdgpu_userq_mgr *uq_mgr = &fpriv->userq_mgr;

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) · line 868

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +10 -0
+	const struct amdgpu_userq_funcs *userq_funcs;
+	struct amdgpu_device *adev = uq_mgr->adev;
+	int primary_queue_id = args->in_cg.primary_queue_id;
+	int secondary_queue_id = args->in_cg.secondary_queue_id;
+	int r;
+
+	/* The reset changes the queue state and the MES is unusable meanwhile */
+	guard(rwsem_read)(&adev->reset_domain->sem);
+
+	mutex_lock(&uq_mgr->userq_mutex);

[Severity: High]
Does taking adev->reset_domain->sem before uq_mgr->userq_mutex invert the lock hierarchy and risk an ABBA deadlock?

amdgpu_lockdep_init() and the rest of amdgpu_userq.c establish uq_mgr->userq_mutex outer to adev->reset_domain->sem:

CPU0 CPU1
---- ----
lock(&uq_mgr->userq_mutex)

                                  down_read(&adev->reset_domain->sem)
down_read(&adev->reset_domain->sem)
                                  lock(&uq_mgr->userq_mutex)

*** DEADLOCK ***

CPU0: amdgpu_userq_destroy() -> amdgpu_userq_unmap_helper()
CPU1: amdgpu_userq_create_gang()
(when a GPU reset writer is queued on adev->reset_domain->sem)

Also, because guard(rwsem_read)(&adev->reset_domain->sem) has function scope, it is still held at the out label when amdgpu_userq_put() runs. If a concurrent AMDGPU_USERQ_OP_FREE dropped the queue's base reference, amdgpu_userq_put() enters amdgpu_userq_destroy() while holding reset_domain->sem, waiting on cancel_delayed_work_sync() under the reset semaphore and taking reset_domain->sem recursively in amdgpu_userq_detach_doorbell() and amdgpu_userq_unmap_helper().

[ ... ]

Quoted 27 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +27 -0
+	if (primary_queue->state == AMDGPU_USERQ_STATE_HUNG) {
+		drm_file_err(uq_mgr->file, "Primary queue is hung\n");
+		r = -EINVAL;
+		goto out;
+	}
+
+	if ((secondary_queue->queue_type != AMDGPU_HW_IP_GFX) &&
+	    (secondary_queue->queue_type != AMDGPU_HW_IP_COMPUTE) &&
+	    (secondary_queue->queue_type != AMDGPU_HW_IP_DMA)) {
+		drm_file_err(uq_mgr->file, "Invalid gang secondary queue type\n");
+		r = -EINVAL;
+		goto out;
+	}
+
+	if (secondary_queue->gang_primary || secondary_queue->gang_secondary) {
+		drm_file_err(uq_mgr->file, "Secondary queue already in a gang\n");
+		r = -EINVAL;
+		goto out;
+	}
+
+	if (secondary_queue->state == AMDGPU_USERQ_STATE_HUNG) {
+		drm_file_err(uq_mgr->file, "Secondary queue is hung\n");
+		r = -EINVAL;
+		goto out;
+	}
+
+	userq_funcs = adev->userq_funcs[primary_queue->queue_type];

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) · line 939

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +10 -0
+	if ((primary_queue->state == AMDGPU_USERQ_STATE_MAPPED ||
+	     primary_queue->state == AMDGPU_USERQ_STATE_PREEMPTED) &&
+	    (secondary_queue->state == AMDGPU_USERQ_STATE_MAPPED ||
+	     secondary_queue->state == AMDGPU_USERQ_STATE_PREEMPTED))
+		r = userq_funcs->set_gang(primary_queue, secondary_queue);
+	else
+		/* if the queues are currently unmapped, the gang state will get
+		 * updated when they get mapped.
+		 */
+		r = 0;

[Severity: Medium]
Should amdgpu_userq_create_gang() also reject queues in AMDGPU_USERQ_STATE_INVALID_VA?

If a queue was marked AMDGPU_USERQ_STATE_INVALID_VA in amdgpu_userq_restore_all(), amdgpu_userq_map_helper() will never remap it (it only maps queues in AMDGPU_USERQ_STATE_UNMAPPED). Here it passes the HUNG checks and falls into the else branch with r = 0, linking a dead queue into the partner queue's gang pointer without ever programming the gang in MES.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
author maintainer
#16 lore raw

PATCH 11/11 drm/amdgpu: bump driver version for userq updates

In reply to Alex Deucher

Support for ganging userqs and doorbell offset queries. Ganged userqs are guaranteed to schedule together and the new doorbell offset query allows the UMD to query what offsets in each dorbell page get routed to which engine.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +2 -1
index d2db20c172c69..b1d7f9332b55d 100644
@@ -129,9 +129,10 @@
  * - 3.64.0 - Userq IP support query
  * - 3.65.0 - Add userq syncobj timeline signaling support
  * - 3.66.0 - Allow jpeg type 4 packets and VCE SEI command
+ * - 3.67.0 - Add userq gang support and doorbell offset query
  */
 #define KMS_DRIVER_MAJOR	3
-#define KMS_DRIVER_MINOR	66
+#define KMS_DRIVER_MINOR	67
 #define KMS_DRIVER_PATCHLEVEL	0
 
 /*
Signature · 1 line
2.56.1
#17 lore raw
In reply to Alex Deucher
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] drm/amdgpu: amdgpu_info_ioctl() returns combined GFX/compute doorbell range --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] drm/amdgpu: amdgpu_info_ioctl() returns combined GFX/compute doorbell range
--

commit 81484698f4a74ac4cd4b7b1d2435342b60989454
Author: Alex Deucher <[email protected]>

drm/amdgpu/userq: Add INFO IOCTL query for doorbell ranges

This commit adds the AMDGPU_INFO_UQ_DOORBELL_RANGE subquery to amdgpu_info_ioctl() so userspace can query the per-page doorbell byte offset and size for GFX, compute, and SDMA user queues.

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +0 -0
index 8e7e522a89e42..23858589dd15c 100644
@@ -1452,6 +1452,26 @@ int amdgpu_info_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
 			return -EINVAL;
 		}

drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c int amdgpu_info_ioctl(struct drm_device *dev, void *data, struct drm_file *filp) · line 1463

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +9 -0
 	}
+	case AMDGPU_INFO_UQ_DOORBELL_RANGE: {
+		struct drm_amdgpu_info_doorbell_range db_range = {};
+
+		switch (info->query_hw_ip.type) {
+		case AMDGPU_HW_IP_GFX:
+		case AMDGPU_HW_IP_COMPUTE:
+			db_range.offset_bytes = adev->doorbell_index.kiq << 3;
+			db_range.size_bytes = (adev->doorbell_index.sdma_engine[0] -
+					       adev->doorbell_index.kiq) << 3;

[Severity: High]
Should AMDGPU_HW_IP_GFX and AMDGPU_HW_IP_COMPUTE return separate doorbell sub-ranges instead of the combined [kiq, sdma_engine[0]) range?

On GFX11 and GFX12, the CP doorbell range registers partition each doorbell page into disjoint ranges for compute (kiq..userqueue_end) and graphics (gfx_ring0..gfx_userqueue_end):

drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c:gfx_v11_0_cp_set_doorbell_range() {
	...
	/* set graphics engine doorbell range */
	WREG32_SOC15(GC, 0, regCP_RB_DOORBELL_RANGE_LOWER,
		     (adev->doorbell_index.gfx_ring0 * 2) << 2);
	WREG32_SOC15(GC, 0, regCP_RB_DOORBELL_RANGE_UPPER,
		     (adev->doorbell_index.gfx_userqueue_end * 2) << 2);

	/* set compute engine doorbell range */
	WREG32_SOC15(GC, 0, regCP_MEC_DOORBELL_RANGE_LOWER,
		     (adev->doorbell_index.kiq * 2) << 2);
	WREG32_SOC15(GC, 0, regCP_MEC_DOORBELL_RANGE_UPPER,
		     (adev->doorbell_index.userqueue_end * 2) << 2);
}

As noted in fd0ed91ae8a2 ("drm/amdgpu: correct cp doorbell range"), hardware and firmware rely on these ranges to distinguish GFX and compute doorbell activity for power-saving states. Returning the combined range for both IP types causes userspace to allocate GFX doorbells inside the MEC compute range and compute doorbells inside the CP_RB graphics range.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1