drm/amdkfd: Allocate SDMA engines more fairly
Subsystems (3)
PATCH 1/3 drm/amdkfd: Allocate SDMA engines more fairly
Give every process at most one queue from each SDMA engine. Previously, we allocated all SDMA engines and queues on a first- come-first-serve basis. This meant that it was possible for two processes racing on their allocation requests to each end up with two queues on the same SDMA engine. That could serialize the performance of commands to different queues.
This new mechanism allows each process at most a single queue on each SDMA engine. Processes will check for queue availability on SDMA engine 0, then 1, then 2, etc. and only allocate on that engine if there is an available queue slot. If there are no queue slots available (or if this process has already allocated a queue on this engine), it moves on and tries the next engine.
The Aldebaran chip has a small quirk that SDMA0 should only be used by the very first allocation from each process. It is OK to use any other SDMA engine at any time, but after the first SDMA allocation request from a process, the resulting engine must be from SDMA1 or above. This patch handles this case as well.
--- .../drm/amd/amdkfd/kfd_device_queue_manager.c | 135 +++++++++++++----- drivers/gpu/drm/amd/amdkfd/kfd_priv.h | 2 + drivers/gpu/drm/amd/amdkfd/kfd_process.c | 3 + 3 files changed, 107 insertions(+), 33 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +102 -33
@@ -52,12 +52,14 @@ static int unmap_queues_cpsch(struct device_queue_manager *dqm,static int map_queues_cpsch (struct device_queue_manager *dqm);static void deallocate_sdma_queue (struct device_queue_manager *dqm, +struct qcm_process_device *qpd,struct queue *q);static inline void deallocate_hqd (struct device_queue_manager *dqm,struct queue *q);static int allocate_hqd (struct device_queue_manager *dqm,struct queue *q);static int allocate_sdma_queue (struct device_queue_manager *dqm, +struct qcm_process_device *qpd,struct queue *q);static void kfd_process_hw_exception (struct work_struct *work);
@@ -349,7 +351,7 @@ static int create_queue_nocpsch(struct device_queue_manager *dqm, q->pipe, q->queue); }else if (q->properties.type ==KFD_QUEUE_TYPE_SDMA || q->properties.type ==KFD_QUEUE_TYPE_SDMA_XGMI ) { - retval =allocate_sdma_queue (dqm, q); + retval =allocate_sdma_queue (dqm, qpd, q);if (retval) goto deallocate_vmid; dqm->asic_ops.init_sdma_vm (dqm, q, qpd);
@@ -410,7 +412,7 @@ static int create_queue_nocpsch(struct device_queue_manager *dqm,deallocate_hqd (dqm, q); else if (q->properties.type == KFD_QUEUE_TYPE_SDMA || q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) - deallocate_sdma_queue(dqm, q); + deallocate_sdma_queue(dqm, qpd, q); deallocate_vmid:if (list_empty (&qpd->queues_list))deallocate_vmid (dqm, qpd, q);
@@ -475,9 +477,9 @@ static int destroy_queue_nocpsch_locked(struct device_queue_manager *dqm,if (q->properties.type ==KFD_QUEUE_TYPE_COMPUTE )deallocate_hqd (dqm, q);else if (q->properties.type ==KFD_QUEUE_TYPE_SDMA ) -deallocate_sdma_queue (dqm, q); +deallocate_sdma_queue (dqm, qpd, q);else if (q->properties.type ==KFD_QUEUE_TYPE_SDMA_XGMI ) -deallocate_sdma_queue (dqm, q); +deallocate_sdma_queue (dqm, qpd, q);else {pr_debug ("q->properties.type %d is invalid\n" , q->properties.type);
@@ -1039,42 +1041,93 @@ static void pre_reset(struct device_queue_manager *dqm)dqm_unlock (dqm); } +static uint64_t sdma_engine_mask (int engine,int num_engines) +{ +uint64_t mask =0 ; + + engine %= num_engines; +while (engine < (sizeof (uint64_t )*8 )) { + mask |=1ULL << engine; + engine += num_engines; + } +return mask; +} +static int allocate_sdma_queue (struct device_queue_manager *dqm, +struct qcm_process_device *qpd,struct queue *q) { -int bit; +uint64_t available_queue_bitmap; +unsigned int bit, engine, num_engines; +bool found_sdma = false;if (q->properties.type ==KFD_QUEUE_TYPE_SDMA ) { -if (dqm->sdma_bitmap ==0 ) { + num_engines =get_num_sdma_engines (dqm); +for_each_set_bit (engine, &(qpd->sdma_engine_bitmap), num_engines) { +/* Do not reuse SDMA0 for any subsequent SDMA queue +* requests on Aldebaran. If SDMA0's queues are all +* full, then this process should never use SDMA0 +* for any further requests +*/ +if (dqm->dev->device_info->asic_family ==CHIP_ALDEBARAN && + engine ==0 ) + qpd->sdma_engine_bitmap &= ~(1ULL << engine); + + available_queue_bitmap =sdma_engine_mask (engine, num_engines); + available_queue_bitmap &= dqm->sdma_bitmap; + +if (!available_queue_bitmap) +continue ; +/* Take the selected engine off the list so we will not +* allocate two queues onto the same engine +*/ + qpd->sdma_engine_bitmap &= ~(1ULL << engine); + found_sdma = true; + + bit =__ffs64 (available_queue_bitmap); + dqm->sdma_bitmap &= ~(1ULL << bit); + q->sdma_id = bit; + q->properties.sdma_engine_id = q->sdma_id % num_engines; + q->properties.sdma_queue_id = q->sdma_id / num_engines; +break ; + } +if (!found_sdma) {pr_err ("No more SDMA queue to allocate\n" );return -ENOMEM ; } - - bit =__ffs64 (dqm->sdma_bitmap); - dqm->sdma_bitmap &= ~(1ULL << bit); - q->sdma_id = bit; - q->properties.sdma_engine_id = q->sdma_id % -get_num_sdma_engines (dqm); - q->properties.sdma_queue_id = q->sdma_id / -get_num_sdma_engines (dqm); }else if (q->properties.type ==KFD_QUEUE_TYPE_SDMA_XGMI ) { -if (dqm->xgmi_sdma_bitmap ==0 ) { + num_engines =get_num_xgmi_sdma_engines (dqm); +for_each_set_bit (engine, &(qpd->xgmi_sdma_engine_bitmap), num_engines) { + available_queue_bitmap =sdma_engine_mask (engine, num_engines); + available_queue_bitmap &= dqm->xgmi_sdma_bitmap; + +if (!available_queue_bitmap) +continue ; +/* Take the selected engine off the list so we will not +* allocate two queues onto the same engine +*/ + qpd->xgmi_sdma_engine_bitmap &= ~(1ULL << engine); + found_sdma = true; + + bit =__ffs64 (available_queue_bitmap); + dqm->xgmi_sdma_bitmap &= ~(1ULL << bit); + q->sdma_id = bit; +/* sdma_engine_id is sdma id including +* both PCIe-optimized SDMAs and XGMI- +* optimized SDMAs. The calculation below +* assumes the first N engines are always +* PCIe-optimized ones +*/ + q->properties.sdma_engine_id =get_num_sdma_engines (dqm) + + q->sdma_id %get_num_xgmi_sdma_engines (dqm); + q->properties.sdma_queue_id = q->sdma_id / +get_num_xgmi_sdma_engines (dqm); +break ; + } +if (!found_sdma) {pr_err ("No more XGMI SDMA queue to allocate\n" );return -ENOMEM ; } - bit =__ffs64 (dqm->xgmi_sdma_bitmap); - dqm->xgmi_sdma_bitmap &= ~(1ULL << bit); - q->sdma_id = bit; -/* sdma_engine_id is sdma id including -* both PCIe-optimized SDMAs and XGMI- -* optimized SDMAs. The calculation below -* assumes the first N engines are always -* PCIe-optimized ones -*/ - q->properties.sdma_engine_id =get_num_sdma_engines (dqm) + - q->sdma_id %get_num_xgmi_sdma_engines (dqm); - q->properties.sdma_queue_id = q->sdma_id / -get_num_xgmi_sdma_engines (dqm); }pr_debug ("SDMA engine id: %d\n" , q->properties.sdma_engine_id);
@@ -1084,16 +1137,32 @@ static int allocate_sdma_queue(struct device_queue_manager *dqm, }static void deallocate_sdma_queue (struct device_queue_manager *dqm, +struct qcm_process_device *qpd,struct queue *q) { +uint32_t engine = q->properties.sdma_engine_id; +if (q->properties.type ==KFD_QUEUE_TYPE_SDMA ) {if (q->sdma_id >=get_num_sdma_queues (dqm))return ; dqm->sdma_bitmap |= (1ULL << q->sdma_id); +/* Don't give SDMA0 back to be reallocated on Aldebaran. +* It is only OK to use this engine from the first allocation +* within a process. +*/ +if (!(dqm->dev->device_info->asic_family ==CHIP_ALDEBARAN && + engine ==0 )) + qpd->sdma_engine_bitmap |= (1ULL << engine); + }else if (q->properties.type ==KFD_QUEUE_TYPE_SDMA_XGMI ) {if (q->sdma_id >=get_num_xgmi_sdma_queues (dqm))return ; dqm->xgmi_sdma_bitmap |= (1ULL << q->sdma_id); +/* engine ID in the queue properties is the global engine ID. +* The XGMI engine bitmap ignores the PCIe-optimized engines. +*/ + engine -=get_num_sdma_engines (dqm); + qpd->xgmi_sdma_engine_bitmap |= (1ULL << engine); } }
@@ -1303,7 +1372,7 @@ static int create_queue_cpsch(struct device_queue_manager *dqm, struct queue *q,if (q->properties.type ==KFD_QUEUE_TYPE_SDMA || q->properties.type ==KFD_QUEUE_TYPE_SDMA_XGMI ) {dqm_lock (dqm); - retval =allocate_sdma_queue (dqm, q); + retval =allocate_sdma_queue (dqm, qpd, q);dqm_unlock (dqm);if (retval) goto out;
@@ -1365,7 +1434,7 @@ static int create_queue_cpsch(struct device_queue_manager *dqm, struct queue *q,if (q->properties.type ==KFD_QUEUE_TYPE_SDMA || q->properties.type ==KFD_QUEUE_TYPE_SDMA_XGMI ) {dqm_lock (dqm); -deallocate_sdma_queue (dqm, q); +deallocate_sdma_queue (dqm, qpd, q);dqm_unlock (dqm); } out:
@@ -1536,7 +1605,7 @@ static int destroy_queue_cpsch(struct device_queue_manager *dqm,if ((q->properties.type ==KFD_QUEUE_TYPE_SDMA ) || (q->properties.type ==KFD_QUEUE_TYPE_SDMA_XGMI )) { -deallocate_sdma_queue (dqm, q); +deallocate_sdma_queue (dqm, qpd, q); pdd->sdma_past_activity_counter += sdma_val; }
@@ -1751,9 +1820,9 @@ static int process_termination_cpsch(struct device_queue_manager *dqm,/* Clear all user mode queues */ list_for_each_entry (q, &qpd->queues_list, list) {if (q->properties.type ==KFD_QUEUE_TYPE_SDMA ) -deallocate_sdma_queue (dqm, q); +deallocate_sdma_queue (dqm, qpd, q);else if (q->properties.type ==KFD_QUEUE_TYPE_SDMA_XGMI ) -deallocate_sdma_queue (dqm, q); +deallocate_sdma_queue (dqm, qpd, q);if (q->properties.is_active) {decrement_queue_count (dqm, q->properties.type);
drivers/gpu/drm/amd/amdkfd/kfd_priv.h +2 -0
@@ -576,6 +576,8 @@ struct qcm_process_device {struct list_head priv_queue_list;unsigned int queue_count; +unsigned long sdma_engine_bitmap; +unsigned long xgmi_sdma_engine_bitmap;unsigned int vmid;bool is_debug;unsigned int evicted;/* eviction counter, 0=active */
drivers/gpu/drm/amd/amdkfd/kfd_process.c +3 -0
@@ -1422,6 +1422,7 @@ struct kfd_process_device *kfd_create_process_device_data(struct kfd_dev *dev, struct kfd_process *p) {struct kfd_process_device *pdd =NULL ; +const struct kfd_device_info *dev_info = dev->dqm->dev->device_info;if (WARN_ON_ONCE (p->n_pdds >=MAX_GPU_INSTANCE ))return NULL ;
@@ -1446,6 +1447,8 @@ struct kfd_process_device *kfd_create_process_device_data(struct kfd_dev *dev, pdd->qpd.pqm = &p->pqm; pdd->qpd.evicted =0 ; pdd->qpd.mapped_gws_queue = false; + pdd->qpd.sdma_engine_bitmap =BIT_ULL (dev_info->num_sdma_engines) -1 ; + pdd->qpd.xgmi_sdma_engine_bitmap =BIT_ULL (dev_info->num_xgmi_sdma_engines) -1 ; pdd->process = p; pdd->bound =PDD_UNBOUND ; pdd->already_dequeued = false;
Signature · 1 line
2.20.1
PATCH 2/3 drm/amdgpu: Use SDMA1 for buffer movement on Aldebaran
Aldebaran should not use SDMA0 for buffer funcs such as page migration. Instead, we move over to SDMA1 for these features. Leave SDMA0 in charge for all other existing chips to avoid any possibility of regressions.
--- drivers/gpu/drm/amd/amdgpu/sdma_v4_0.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/sdma_v4_0.c +6 -2
@@ -2689,11 +2689,15 @@ static const struct amdgpu_buffer_funcs sdma_v4_0_buffer_funcs = {static void sdma_v4_0_set_buffer_funcs (struct amdgpu_device *adev) { +int engine =0 ; + +if (adev->asic_type ==CHIP_ALDEBARAN ) + engine =1 ; adev->mman.buffer_funcs = &sdma_v4_0_buffer_funcs;if (adev->sdma.has_page_queue) - adev->mman.buffer_funcs_ring = &adev->sdma.instance[0 ].page; + adev->mman.buffer_funcs_ring = &adev->sdma.instance[engine].page;else - adev->mman.buffer_funcs_ring = &adev->sdma.instance[0 ].ring; + adev->mman.buffer_funcs_ring = &adev->sdma.instance[engine].ring; }static const struct amdgpu_vm_pte_funcs sdma_v4_0_vm_pte_funcs = {
Signature · 1 line
2.20.1
Re: [PATCH 2/3] drm/amdgpu: Use SDMA1 for buffer movement on Aldebaran
Am 20.08.21 um 07:32 schrieb Joseph Greathouse:
Quoted 4 lines
Aldebaran should not use SDMA0 for buffer funcs such as page migration. Instead, we move over to SDMA1 for these features. Leave SDMA0 in charge for all other existing chips to avoid any possibility of regressions.
The part why we do this is missing, apart from that looks good to me.
Christian.
Quoted 28 lines
Signed-off-by Joseph Greathouse <[email protected]>--- drivers/gpu/drm/amd/amdgpu/sdma_v4_0.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)drivers/gpu/drm/amd/amdgpu/sdma_v4_0.c +6
-2@@ -2689,11 +2689,15 @@ static const struct amdgpu_buffer_funcs sdma_v4_0_buffer_funcs = {static void sdma_v4_0_set_buffer_funcs (struct amdgpu_device *adev) { +int engine =0 ; + +if (adev->asic_type ==CHIP_ALDEBARAN ) + engine =1 ; adev->mman.buffer_funcs = &sdma_v4_0_buffer_funcs;if (adev->sdma.has_page_queue) - adev->mman.buffer_funcs_ring = &adev->sdma.instance[0 ].page; + adev->mman.buffer_funcs_ring = &adev->sdma.instance[engine].page;else - adev->mman.buffer_funcs_ring = &adev->sdma.instance[0 ].ring; + adev->mman.buffer_funcs_ring = &adev->sdma.instance[engine].ring; }static const struct amdgpu_vm_pte_funcs sdma_v4_0_vm_pte_funcs = {
RE: [PATCH 2/3] drm/amdgpu: Use SDMA1 for buffer movement on Aldebaran
Quoted 7 lines
-----Original Message-----
From: Christian König <[email protected]>
Sent: Friday, August 20, 2021 2:00 AM
To: Greathouse, Joseph <[email protected]>; [email protected]
Subject: Re: [PATCH 2/3] drm/amdgpu: Use SDMA1 for buffer movement on AldebaranAm 20.08.21 um 07:32 schrieb Joseph Greathouse:
Quoted 8 lines
Quoted 4 lines
Aldebaran should not use SDMA0 for buffer funcs such as page migration. Instead, we move over to SDMA1 for these features. Leave SDMA0 in charge for all other existing chips to avoid any possibility of regressions.
The part why we do this is missing, apart from that looks good to me.
Christian.
How about this for a replacement description?
Because of sharing an MMHUB port with other engines, the hardware design team has advised that Aldebaran should not use SDMA0 for buffer funcs such as page migration. Instead, we move over to SDMA1 for these features. Leave SDMA0 in charge for all other existing chips to avoid any possibility of regressions.
Thanks,
-Joe
Quoted 28 lines
Quoted 28 lines
Signed-off-by Joseph Greathouse <[email protected]>--- drivers/gpu/drm/amd/amdgpu/sdma_v4_0.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)drivers/gpu/drm/amd/amdgpu/sdma_v4_0.c +6
-2@@ -2689,11 +2689,15 @@ static const struct amdgpu_buffer_funcs sdma_v4_0_buffer_funcs = {static void sdma_v4_0_set_buffer_funcs (struct amdgpu_device *adev) { +int engine =0 ; + +if (adev->asic_type ==CHIP_ALDEBARAN ) + engine =1 ; adev->mman.buffer_funcs = &sdma_v4_0_buffer_funcs;if (adev->sdma.has_page_queue) - adev->mman.buffer_funcs_ring = &adev->sdma.instance[0 ].page; + adev->mman.buffer_funcs_ring = &adev->sdma.instance[engine].page;else - adev->mman.buffer_funcs_ring = &adev->sdma.instance[0 ].ring; + adev->mman.buffer_funcs_ring = &adev->sdma.instance[engine].ring; }static const struct amdgpu_vm_pte_funcs sdma_v4_0_vm_pte_funcs = {
PATCH 3/3 drm/amdkfd: Spread out XGMI SDMA allocations
In reply to Joseph GreathouseAvoid hotspotting of allocations of SDMA engines from the XGMI pool by making each process attempt to allocate engines starting from the engine after the last one that was allocated.
--- drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c | 8 +++++++- drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.h | 2 ++ drivers/gpu/drm/amd/amdkfd/kfd_priv.h | 1 + drivers/gpu/drm/amd/amdkfd/kfd_process.c | 1 + 4 files changed, 11 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +7 -1
@@ -1096,9 +1096,12 @@ static int allocate_sdma_queue(struct device_queue_manager *dqm,return -ENOMEM ; } }else if (q->properties.type ==KFD_QUEUE_TYPE_SDMA_XGMI ) { +if (qpd->initial_xgmi_engine ==-1 ) + qpd->initial_xgmi_engine = dqm->next_xgmi_engine; num_engines =get_num_xgmi_sdma_engines (dqm);for_each_set_bit (engine, &(qpd->xgmi_sdma_engine_bitmap), num_engines) { - available_queue_bitmap =sdma_engine_mask (engine, num_engines); + available_queue_bitmap =sdma_engine_mask ( + qpd->initial_xgmi_engine + engine, num_engines); available_queue_bitmap &= dqm->xgmi_sdma_bitmap;if (!available_queue_bitmap)
@@ -1109,6 +1112,9 @@ static int allocate_sdma_queue(struct device_queue_manager *dqm, qpd->xgmi_sdma_engine_bitmap &= ~(1ULL << engine); found_sdma = true; + dqm->next_xgmi_engine = qpd->initial_xgmi_engine + engine +1 ; + dqm->next_xgmi_engine %= num_engines; + bit =__ffs64 (available_queue_bitmap); dqm->xgmi_sdma_bitmap &= ~(1ULL << bit); q->sdma_id = bit;
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.h +2 -0
@@ -183,6 +183,8 @@ struct device_queue_manager {unsigned int *allocated_queues;uint64_t sdma_bitmap;uint64_t xgmi_sdma_bitmap; +/* which XGMI engine the next process should attempt to start on */ +unsigned int next_xgmi_engine;/* the pasid mapping for each kfd vmid */ uint16_t vmid_pasid[VMID_NUM ];uint64_t pipelines_addr;
drivers/gpu/drm/amd/amdkfd/kfd_priv.h +1 -0
@@ -578,6 +578,7 @@ struct qcm_process_device {unsigned int queue_count;unsigned long sdma_engine_bitmap;unsigned long xgmi_sdma_engine_bitmap; +int initial_xgmi_engine;unsigned int vmid;bool is_debug;unsigned int evicted;/* eviction counter, 0=active */
drivers/gpu/drm/amd/amdkfd/kfd_process.c +1 -0
@@ -1449,6 +1449,7 @@ struct kfd_process_device *kfd_create_process_device_data(struct kfd_dev *dev, pdd->qpd.mapped_gws_queue = false; pdd->qpd.sdma_engine_bitmap =BIT_ULL (dev_info->num_sdma_engines) -1 ; pdd->qpd.xgmi_sdma_engine_bitmap =BIT_ULL (dev_info->num_xgmi_sdma_engines) -1 ; + pdd->qpd.initial_xgmi_engine =-1 ; pdd->process = p; pdd->bound =PDD_UNBOUND ; pdd->already_dequeued = false;
Signature · 1 line
2.20.1
Am 2021-08-20 um 1:32 a.m. schrieb Joseph Greathouse:
Quoted 14 lines
Give every process at most one queue from each SDMA engine. Previously, we allocated all SDMA engines and queues on a first- come-first-serve basis. This meant that it was possible for two processes racing on their allocation requests to each end up with two queues on the same SDMA engine. That could serialize the performance of commands to different queues.
This new mechanism allows each process at most a single queue on each SDMA engine. Processes will check for queue availability on SDMA engine 0, then 1, then 2, etc. and only allocate on that engine if there is an available queue slot. If there are no queue slots available (or if this process has already allocated a queue on this engine), it moves on and tries the next engine.
Quoted 7 lines
The Aldebaran chip has a small quirk that SDMA0 should only be used by the very first allocation from each process. It is OK to use any other SDMA engine at any time, but after the first SDMA allocation request from a process, the resulting engine must be from SDMA1 or above. This patch handles this case as well.
Signed-off-by Joseph Greathouse <[email protected]>
One final nit-pick inline. With that fixed, the series is
Quoted 141 lines
--- .../drm/amd/amdkfd/kfd_device_queue_manager.c | 135 +++++++++++++----- drivers/gpu/drm/amd/amdkfd/kfd_priv.h | 2 + drivers/gpu/drm/amd/amdkfd/kfd_process.c | 3 + 3 files changed, 107 insertions(+), 33 deletions(-)drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +68
-15@@ -52,12 +52,14 @@ static int unmap_queues_cpsch(struct device_queue_manager *dqm,static int map_queues_cpsch (struct device_queue_manager *dqm);static void deallocate_sdma_queue (struct device_queue_manager *dqm, +struct qcm_process_device *qpd,struct queue *q);static inline void deallocate_hqd (struct device_queue_manager *dqm,struct queue *q);static int allocate_hqd (struct device_queue_manager *dqm,struct queue *q);static int allocate_sdma_queue (struct device_queue_manager *dqm, +struct qcm_process_device *qpd,struct queue *q);static void kfd_process_hw_exception (struct work_struct *work);@@ -349,7 +351,7 @@ static int create_queue_nocpsch(struct device_queue_manager *dqm, q->pipe, q->queue); }else if (q->properties.type ==KFD_QUEUE_TYPE_SDMA || q->properties.type ==KFD_QUEUE_TYPE_SDMA_XGMI ) { - retval =allocate_sdma_queue (dqm, q); + retval =allocate_sdma_queue (dqm, qpd, q);if (retval) goto deallocate_vmid; dqm->asic_ops.init_sdma_vm (dqm, q, qpd);@@ -410,7 +412,7 @@ static int create_queue_nocpsch(struct device_queue_manager *dqm,deallocate_hqd (dqm, q); else if (q->properties.type == KFD_QUEUE_TYPE_SDMA || q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) - deallocate_sdma_queue(dqm, q); + deallocate_sdma_queue(dqm, qpd, q); deallocate_vmid:if (list_empty (&qpd->queues_list))deallocate_vmid (dqm, qpd, q);@@ -475,9 +477,9 @@ static int destroy_queue_nocpsch_locked(struct device_queue_manager *dqm,if (q->properties.type ==KFD_QUEUE_TYPE_COMPUTE )deallocate_hqd (dqm, q);else if (q->properties.type ==KFD_QUEUE_TYPE_SDMA ) -deallocate_sdma_queue (dqm, q); +deallocate_sdma_queue (dqm, qpd, q);else if (q->properties.type ==KFD_QUEUE_TYPE_SDMA_XGMI ) -deallocate_sdma_queue (dqm, q); +deallocate_sdma_queue (dqm, qpd, q);else {pr_debug ("q->properties.type %d is invalid\n" , q->properties.type);@@ -1039,42 +1041,93 @@ static void pre_reset(struct device_queue_manager *dqm)dqm_unlock (dqm); } +static uint64_t sdma_engine_mask (int engine,int num_engines) +{ +uint64_t mask =0 ; + + engine %= num_engines; +while (engine < (sizeof (uint64_t )*8 )) { + mask |=1ULL << engine; + engine += num_engines; + } +return mask; +} +static int allocate_sdma_queue (struct device_queue_manager *dqm, +struct qcm_process_device *qpd,struct queue *q) { -int bit; +uint64_t available_queue_bitmap; +unsigned int bit, engine, num_engines; +bool found_sdma = false;if (q->properties.type ==KFD_QUEUE_TYPE_SDMA ) { -if (dqm->sdma_bitmap ==0 ) { + num_engines =get_num_sdma_engines (dqm); +for_each_set_bit (engine, &(qpd->sdma_engine_bitmap), num_engines) { +/* Do not reuse SDMA0 for any subsequent SDMA queue +* requests on Aldebaran. If SDMA0's queues are all +* full, then this process should never use SDMA0 +* for any further requests +*/ +if (dqm->dev->device_info->asic_family ==CHIP_ALDEBARAN && + engine ==0 ) + qpd->sdma_engine_bitmap &= ~(1ULL << engine); + + available_queue_bitmap =sdma_engine_mask (engine, num_engines); + available_queue_bitmap &= dqm->sdma_bitmap; + +if (!available_queue_bitmap) +continue ; +/* Take the selected engine off the list so we will not +* allocate two queues onto the same engine +*/ + qpd->sdma_engine_bitmap &= ~(1ULL << engine); + found_sdma = true; + + bit =__ffs64 (available_queue_bitmap); + dqm->sdma_bitmap &= ~(1ULL << bit); + q->sdma_id = bit; + q->properties.sdma_engine_id = q->sdma_id % num_engines; + q->properties.sdma_queue_id = q->sdma_id / num_engines; +break ; + } +if (!found_sdma) {pr_err ("No more SDMA queue to allocate\n" );return -ENOMEM ; } - - bit =__ffs64 (dqm->sdma_bitmap); - dqm->sdma_bitmap &= ~(1ULL << bit); - q->sdma_id = bit; - q->properties.sdma_engine_id = q->sdma_id % -get_num_sdma_engines (dqm); - q->properties.sdma_queue_id = q->sdma_id / -get_num_sdma_engines (dqm); }else if (q->properties.type ==KFD_QUEUE_TYPE_SDMA_XGMI ) { -if (dqm->xgmi_sdma_bitmap ==0 ) { + num_engines =get_num_xgmi_sdma_engines (dqm); +for_each_set_bit (engine, &(qpd->xgmi_sdma_engine_bitmap), num_engines) { + available_queue_bitmap =sdma_engine_mask (engine, num_engines); + available_queue_bitmap &= dqm->xgmi_sdma_bitmap; + +if (!available_queue_bitmap) +continue ; +/* Take the selected engine off the list so we will not +* allocate two queues onto the same engine +*/ + qpd->xgmi_sdma_engine_bitmap &= ~(1ULL << engine); + found_sdma = true; + + bit =__ffs64 (available_queue_bitmap); + dqm->xgmi_sdma_bitmap &= ~(1ULL << bit); + q->sdma_id = bit;
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c static void pre_reset(struct device_queue_manager *dqm) · line 1124
Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +10
-0+/* sdma_engine_id is sdma id including +* both PCIe-optimized SDMAs and XGMI- +* optimized SDMAs. The calculation below +* assumes the first N engines are always +* PCIe-optimized ones +*/ + q->properties.sdma_engine_id =get_num_sdma_engines (dqm) + + q->sdma_id %get_num_xgmi_sdma_engines (dqm); + q->properties.sdma_queue_id = q->sdma_id / +get_num_xgmi_sdma_engines (dqm);
You could use "num_engines" here instead of get_num_xgmi_sdma_engines(dqm).
Regards,
Felix
Quoted 128 lines
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +24
-18+break ; + } +if (!found_sdma) {pr_err ("No more XGMI SDMA queue to allocate\n" );return -ENOMEM ; } - bit =__ffs64 (dqm->xgmi_sdma_bitmap); - dqm->xgmi_sdma_bitmap &= ~(1ULL << bit); - q->sdma_id = bit; -/* sdma_engine_id is sdma id including -* both PCIe-optimized SDMAs and XGMI- -* optimized SDMAs. The calculation below -* assumes the first N engines are always -* PCIe-optimized ones -*/ - q->properties.sdma_engine_id =get_num_sdma_engines (dqm) + - q->sdma_id %get_num_xgmi_sdma_engines (dqm); - q->properties.sdma_queue_id = q->sdma_id / -get_num_xgmi_sdma_engines (dqm); }pr_debug ("SDMA engine id: %d\n" , q->properties.sdma_engine_id);@@ -1084,16 +1137,32 @@ static int allocate_sdma_queue(struct device_queue_manager *dqm, }static void deallocate_sdma_queue (struct device_queue_manager *dqm, +struct qcm_process_device *qpd,struct queue *q) { +uint32_t engine = q->properties.sdma_engine_id; +if (q->properties.type ==KFD_QUEUE_TYPE_SDMA ) {if (q->sdma_id >=get_num_sdma_queues (dqm))return ; dqm->sdma_bitmap |= (1ULL << q->sdma_id); +/* Don't give SDMA0 back to be reallocated on Aldebaran. +* It is only OK to use this engine from the first allocation +* within a process. +*/ +if (!(dqm->dev->device_info->asic_family ==CHIP_ALDEBARAN && + engine ==0 )) + qpd->sdma_engine_bitmap |= (1ULL << engine); + }else if (q->properties.type ==KFD_QUEUE_TYPE_SDMA_XGMI ) {if (q->sdma_id >=get_num_xgmi_sdma_queues (dqm))return ; dqm->xgmi_sdma_bitmap |= (1ULL << q->sdma_id); +/* engine ID in the queue properties is the global engine ID. +* The XGMI engine bitmap ignores the PCIe-optimized engines. +*/ + engine -=get_num_sdma_engines (dqm); + qpd->xgmi_sdma_engine_bitmap |= (1ULL << engine); } }@@ -1303,7 +1372,7 @@ static int create_queue_cpsch(struct device_queue_manager *dqm, struct queue *q,if (q->properties.type ==KFD_QUEUE_TYPE_SDMA || q->properties.type ==KFD_QUEUE_TYPE_SDMA_XGMI ) {dqm_lock (dqm); - retval =allocate_sdma_queue (dqm, q); + retval =allocate_sdma_queue (dqm, qpd, q);dqm_unlock (dqm);if (retval) goto out;@@ -1365,7 +1434,7 @@ static int create_queue_cpsch(struct device_queue_manager *dqm, struct queue *q,if (q->properties.type ==KFD_QUEUE_TYPE_SDMA || q->properties.type ==KFD_QUEUE_TYPE_SDMA_XGMI ) {dqm_lock (dqm); -deallocate_sdma_queue (dqm, q); +deallocate_sdma_queue (dqm, qpd, q);dqm_unlock (dqm); } out:@@ -1536,7 +1605,7 @@ static int destroy_queue_cpsch(struct device_queue_manager *dqm,if ((q->properties.type ==KFD_QUEUE_TYPE_SDMA ) || (q->properties.type ==KFD_QUEUE_TYPE_SDMA_XGMI )) { -deallocate_sdma_queue (dqm, q); +deallocate_sdma_queue (dqm, qpd, q); pdd->sdma_past_activity_counter += sdma_val; }@@ -1751,9 +1820,9 @@ static int process_termination_cpsch(struct device_queue_manager *dqm,/* Clear all user mode queues */ list_for_each_entry (q, &qpd->queues_list, list) {if (q->properties.type ==KFD_QUEUE_TYPE_SDMA ) -deallocate_sdma_queue (dqm, q); +deallocate_sdma_queue (dqm, qpd, q);else if (q->properties.type ==KFD_QUEUE_TYPE_SDMA_XGMI ) -deallocate_sdma_queue (dqm, q); +deallocate_sdma_queue (dqm, qpd, q);if (q->properties.is_active) {decrement_queue_count (dqm, q->properties.type);drivers/gpu/drm/amd/amdkfd/kfd_priv.h +2
-0@@ -576,6 +576,8 @@ struct qcm_process_device {struct list_head priv_queue_list;unsigned int queue_count; +unsigned long sdma_engine_bitmap; +unsigned long xgmi_sdma_engine_bitmap;unsigned int vmid;bool is_debug;unsigned int evicted;/* eviction counter, 0=active */ drivers/gpu/drm/amd/amdkfd/kfd_process.c +3
-0@@ -1422,6 +1422,7 @@ struct kfd_process_device *kfd_create_process_device_data(struct kfd_dev *dev, struct kfd_process *p) {struct kfd_process_device *pdd =NULL ; +const struct kfd_device_info *dev_info = dev->dqm->dev->device_info;if (WARN_ON_ONCE (p->n_pdds >=MAX_GPU_INSTANCE ))return NULL ;@@ -1446,6 +1447,8 @@ struct kfd_process_device *kfd_create_process_device_data(struct kfd_dev *dev, pdd->qpd.pqm = &p->pqm; pdd->qpd.evicted =0 ; pdd->qpd.mapped_gws_queue = false; + pdd->qpd.sdma_engine_bitmap =BIT_ULL (dev_info->num_sdma_engines) -1 ; + pdd->qpd.xgmi_sdma_engine_bitmap =BIT_ULL (dev_info->num_xgmi_sdma_engines) -1 ; pdd->process = p; pdd->bound =PDD_UNBOUND ; pdd->already_dequeued = false;
Quoted 77 lines · Joseph Greathouse
Give every process at most one queue from each SDMA engine. Previously, we allocated all SDMA engines and queues on a first- come-first-serve basis. This meant that it was possible for two processes racing on their allocation requests to each end up with two queues on the same SDMA engine. That could serialize the performance of commands to different queues.
This new mechanism allows each process at most a single queue on each SDMA engine. Processes will check for queue availability on SDMA engine 0, then 1, then 2, etc. and only allocate on that engine if there is an available queue slot. If there are no queue slots available (or if this process has already allocated a queue on this engine), it moves on and tries the next engine.
The Aldebaran chip has a small quirk that SDMA0 should only be used by the very first allocation from each process. It is OK to use any other SDMA engine at any time, but after the first SDMA allocation request from a process, the resulting engine must be from SDMA1 or above. This patch handles this case as well.
Signed-off-by Joseph Greathouse <[email protected]>--- .../drm/amd/amdkfd/kfd_device_queue_manager.c | 135 +++++++++++++----- drivers/gpu/drm/amd/amdkfd/kfd_priv.h | 2 + drivers/gpu/drm/amd/amdkfd/kfd_process.c | 3 + 3 files changed, 107 insertions(+), 33 deletions(-)drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +6
-4@@ -52,12 +52,14 @@ static int unmap_queues_cpsch(struct device_queue_manager *dqm,static int map_queues_cpsch (struct device_queue_manager *dqm);static void deallocate_sdma_queue (struct device_queue_manager *dqm, +struct qcm_process_device *qpd,struct queue *q);static inline void deallocate_hqd (struct device_queue_manager *dqm,struct queue *q);static int allocate_hqd (struct device_queue_manager *dqm,struct queue *q);static int allocate_sdma_queue (struct device_queue_manager *dqm, +struct qcm_process_device *qpd,struct queue *q);static void kfd_process_hw_exception (struct work_struct *work);@@ -349,7 +351,7 @@ static int create_queue_nocpsch(struct device_queue_manager *dqm, q->pipe, q->queue); }else if (q->properties.type ==KFD_QUEUE_TYPE_SDMA || q->properties.type ==KFD_QUEUE_TYPE_SDMA_XGMI ) { - retval =allocate_sdma_queue (dqm, q); + retval =allocate_sdma_queue (dqm, qpd, q);if (retval) goto deallocate_vmid; dqm->asic_ops.init_sdma_vm (dqm, q, qpd);@@ -410,7 +412,7 @@ static int create_queue_nocpsch(struct device_queue_manager *dqm,deallocate_hqd (dqm, q); else if (q->properties.type == KFD_QUEUE_TYPE_SDMA || q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) - deallocate_sdma_queue(dqm, q); + deallocate_sdma_queue(dqm, qpd, q); deallocate_vmid:if (list_empty (&qpd->queues_list))deallocate_vmid (dqm, qpd, q);@@ -475,9 +477,9 @@ static int destroy_queue_nocpsch_locked(struct device_queue_manager *dqm,if (q->properties.type ==KFD_QUEUE_TYPE_COMPUTE )deallocate_hqd (dqm, q);else if (q->properties.type ==KFD_QUEUE_TYPE_SDMA ) -deallocate_sdma_queue (dqm, q); +deallocate_sdma_queue (dqm, qpd, q);else if (q->properties.type ==KFD_QUEUE_TYPE_SDMA_XGMI ) -deallocate_sdma_queue (dqm, q); +deallocate_sdma_queue (dqm, qpd, q);else {pr_debug ("q->properties.type %d is invalid\n" , q->properties.type);
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c static void pre_reset(struct device_queue_manager *dqm) · line 1044
Quoted 4 lines
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +1
-0@@ -1039,42 +1041,93 @@ static void pre_reset(struct device_queue_manager *dqm)dqm_unlock (dqm); } +static uint64_t sdma_engine_mask (int engine,int num_engines)
Looks more like the queue mask for an engine, the name doesn't make it clear.
Quoted 12 lines
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +11
-0+{ +uint64_t mask =0 ; + + engine %= num_engines; +while (engine < (sizeof (uint64_t )*8 )) { + mask |=1ULL << engine; + engine += num_engines; + } +return mask; +} + static int allocate_sdma_queue(struct device_queue_manager *dqm,
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c static void pre_reset(struct device_queue_manager *dqm) · line 1048
Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +4
-2+struct qcm_process_device *qpd, struct queue *q) { -int bit; + uint64_t available_queue_bitmap; +unsigned int bit, engine, num_engines; +bool found_sdma = false;if (q->properties.type ==KFD_QUEUE_TYPE_SDMA ) { -if (dqm->sdma_bitmap ==0 ) {
This is still a valid optimization and no need to loop through if nothing is available anyway. Valid for XGMI loop also.
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c static void pre_reset(struct device_queue_manager *dqm) · line 1066
Quoted 2 lines
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +2
-0+ num_engines =get_num_sdma_engines (dqm); +for_each_set_bit (engine, &(qpd->sdma_engine_bitmap), num_engines) {
Probably a naive question -
Theoretically there are 8 queues per engine as per the mask code. In the below code, there is an assumption that a process will use at best number of queues=max num of sdma engines or xgmi engines simultaneously. Is that true always? For ex: there are 2 sdma engines and 4 queues per engine. Can't multiple threads of a process initiate multiple sdma transactions > number of sdma engines available? This code limits that, but I don't know if that is a possible case.
Thanks,
Lijo
Quoted 196 lines
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +78
-27+/* Do not reuse SDMA0 for any subsequent SDMA queue +* requests on Aldebaran. If SDMA0's queues are all +* full, then this process should never use SDMA0 +* for any further requests +*/ +if (dqm->dev->device_info->asic_family ==CHIP_ALDEBARAN && + engine ==0 ) + qpd->sdma_engine_bitmap &= ~(1ULL << engine); + + available_queue_bitmap =sdma_engine_mask (engine, num_engines); + available_queue_bitmap &= dqm->sdma_bitmap; + +if (!available_queue_bitmap) +continue ; +/* Take the selected engine off the list so we will not +* allocate two queues onto the same engine +*/ + qpd->sdma_engine_bitmap &= ~(1ULL << engine); + found_sdma = true; + + bit =__ffs64 (available_queue_bitmap); + dqm->sdma_bitmap &= ~(1ULL << bit); + q->sdma_id = bit; + q->properties.sdma_engine_id = q->sdma_id % num_engines; + q->properties.sdma_queue_id = q->sdma_id / num_engines; +break ; + } +if (!found_sdma) {pr_err ("No more SDMA queue to allocate\n" );return -ENOMEM ; } - - bit =__ffs64 (dqm->sdma_bitmap); - dqm->sdma_bitmap &= ~(1ULL << bit); - q->sdma_id = bit; - q->properties.sdma_engine_id = q->sdma_id % -get_num_sdma_engines (dqm); - q->properties.sdma_queue_id = q->sdma_id / -get_num_sdma_engines (dqm); }else if (q->properties.type ==KFD_QUEUE_TYPE_SDMA_XGMI ) { -if (dqm->xgmi_sdma_bitmap ==0 ) { + num_engines =get_num_xgmi_sdma_engines (dqm); +for_each_set_bit (engine, &(qpd->xgmi_sdma_engine_bitmap), num_engines) { + available_queue_bitmap =sdma_engine_mask (engine, num_engines); + available_queue_bitmap &= dqm->xgmi_sdma_bitmap; + +if (!available_queue_bitmap) +continue ; +/* Take the selected engine off the list so we will not +* allocate two queues onto the same engine +*/ + qpd->xgmi_sdma_engine_bitmap &= ~(1ULL << engine); + found_sdma = true; + + bit =__ffs64 (available_queue_bitmap); + dqm->xgmi_sdma_bitmap &= ~(1ULL << bit); + q->sdma_id = bit; +/* sdma_engine_id is sdma id including +* both PCIe-optimized SDMAs and XGMI- +* optimized SDMAs. The calculation below +* assumes the first N engines are always +* PCIe-optimized ones +*/ + q->properties.sdma_engine_id =get_num_sdma_engines (dqm) + + q->sdma_id %get_num_xgmi_sdma_engines (dqm); + q->properties.sdma_queue_id = q->sdma_id / +get_num_xgmi_sdma_engines (dqm); +break ; + } +if (!found_sdma) {pr_err ("No more XGMI SDMA queue to allocate\n" );return -ENOMEM ; } - bit =__ffs64 (dqm->xgmi_sdma_bitmap); - dqm->xgmi_sdma_bitmap &= ~(1ULL << bit); - q->sdma_id = bit; -/* sdma_engine_id is sdma id including -* both PCIe-optimized SDMAs and XGMI- -* optimized SDMAs. The calculation below -* assumes the first N engines are always -* PCIe-optimized ones -*/ - q->properties.sdma_engine_id =get_num_sdma_engines (dqm) + - q->sdma_id %get_num_xgmi_sdma_engines (dqm); - q->properties.sdma_queue_id = q->sdma_id / -get_num_xgmi_sdma_engines (dqm); }pr_debug ("SDMA engine id: %d\n" , q->properties.sdma_engine_id);@@ -1084,16 +1137,32 @@ static int allocate_sdma_queue(struct device_queue_manager *dqm, }static void deallocate_sdma_queue (struct device_queue_manager *dqm, +struct qcm_process_device *qpd,struct queue *q) { +uint32_t engine = q->properties.sdma_engine_id; +if (q->properties.type ==KFD_QUEUE_TYPE_SDMA ) {if (q->sdma_id >=get_num_sdma_queues (dqm))return ; dqm->sdma_bitmap |= (1ULL << q->sdma_id); +/* Don't give SDMA0 back to be reallocated on Aldebaran. +* It is only OK to use this engine from the first allocation +* within a process. +*/ +if (!(dqm->dev->device_info->asic_family ==CHIP_ALDEBARAN && + engine ==0 )) + qpd->sdma_engine_bitmap |= (1ULL << engine); + }else if (q->properties.type ==KFD_QUEUE_TYPE_SDMA_XGMI ) {if (q->sdma_id >=get_num_xgmi_sdma_queues (dqm))return ; dqm->xgmi_sdma_bitmap |= (1ULL << q->sdma_id); +/* engine ID in the queue properties is the global engine ID. +* The XGMI engine bitmap ignores the PCIe-optimized engines. +*/ + engine -=get_num_sdma_engines (dqm); + qpd->xgmi_sdma_engine_bitmap |= (1ULL << engine); } }@@ -1303,7 +1372,7 @@ static int create_queue_cpsch(struct device_queue_manager *dqm, struct queue *q,if (q->properties.type ==KFD_QUEUE_TYPE_SDMA || q->properties.type ==KFD_QUEUE_TYPE_SDMA_XGMI ) {dqm_lock (dqm); - retval =allocate_sdma_queue (dqm, q); + retval =allocate_sdma_queue (dqm, qpd, q);dqm_unlock (dqm);if (retval) goto out;@@ -1365,7 +1434,7 @@ static int create_queue_cpsch(struct device_queue_manager *dqm, struct queue *q,if (q->properties.type ==KFD_QUEUE_TYPE_SDMA || q->properties.type ==KFD_QUEUE_TYPE_SDMA_XGMI ) {dqm_lock (dqm); -deallocate_sdma_queue (dqm, q); +deallocate_sdma_queue (dqm, qpd, q);dqm_unlock (dqm); } out:@@ -1536,7 +1605,7 @@ static int destroy_queue_cpsch(struct device_queue_manager *dqm,if ((q->properties.type ==KFD_QUEUE_TYPE_SDMA ) || (q->properties.type ==KFD_QUEUE_TYPE_SDMA_XGMI )) { -deallocate_sdma_queue (dqm, q); +deallocate_sdma_queue (dqm, qpd, q); pdd->sdma_past_activity_counter += sdma_val; }@@ -1751,9 +1820,9 @@ static int process_termination_cpsch(struct device_queue_manager *dqm,/* Clear all user mode queues */ list_for_each_entry (q, &qpd->queues_list, list) {if (q->properties.type ==KFD_QUEUE_TYPE_SDMA ) -deallocate_sdma_queue (dqm, q); +deallocate_sdma_queue (dqm, qpd, q);else if (q->properties.type ==KFD_QUEUE_TYPE_SDMA_XGMI ) -deallocate_sdma_queue (dqm, q); +deallocate_sdma_queue (dqm, qpd, q);if (q->properties.is_active) {decrement_queue_count (dqm, q->properties.type);drivers/gpu/drm/amd/amdkfd/kfd_priv.h +2
-0@@ -576,6 +576,8 @@ struct qcm_process_device {struct list_head priv_queue_list;unsigned int queue_count; +unsigned long sdma_engine_bitmap; +unsigned long xgmi_sdma_engine_bitmap;unsigned int vmid;bool is_debug;unsigned int evicted;/* eviction counter, 0=active */ drivers/gpu/drm/amd/amdkfd/kfd_process.c +3
-0@@ -1422,6 +1422,7 @@ struct kfd_process_device *kfd_create_process_device_data(struct kfd_dev *dev, struct kfd_process *p) {struct kfd_process_device *pdd =NULL ; +const struct kfd_device_info *dev_info = dev->dqm->dev->device_info;if (WARN_ON_ONCE (p->n_pdds >=MAX_GPU_INSTANCE ))return NULL ;@@ -1446,6 +1447,8 @@ struct kfd_process_device *kfd_create_process_device_data(struct kfd_dev *dev, pdd->qpd.pqm = &p->pqm; pdd->qpd.evicted =0 ; pdd->qpd.mapped_gws_queue = false; + pdd->qpd.sdma_engine_bitmap =BIT_ULL (dev_info->num_sdma_engines) -1 ; + pdd->qpd.xgmi_sdma_engine_bitmap =BIT_ULL (dev_info->num_xgmi_sdma_engines) -1 ; pdd->process = p; pdd->bound =PDD_UNBOUND ; pdd->already_dequeued = false;
Felix Kuehling
maintainerAm 2021-08-23 um 3:08 a.m. schrieb Lazar, Lijo:
Quoted 122 lines · Joseph Greathouse
Quoted 80 lines
Give every process at most one queue from each SDMA engine. Previously, we allocated all SDMA engines and queues on a first- come-first-serve basis. This meant that it was possible for two processes racing on their allocation requests to each end up with two queues on the same SDMA engine. That could serialize the performance of commands to different queues.
This new mechanism allows each process at most a single queue on each SDMA engine. Processes will check for queue availability on SDMA engine 0, then 1, then 2, etc. and only allocate on that engine if there is an available queue slot. If there are no queue slots available (or if this process has already allocated a queue on this engine), it moves on and tries the next engine.
The Aldebaran chip has a small quirk that SDMA0 should only be used by the very first allocation from each process. It is OK to use any other SDMA engine at any time, but after the first SDMA allocation request from a process, the resulting engine must be from SDMA1 or above. This patch handles this case as well.
Signed-off-by Joseph Greathouse <[email protected]>---
.../drm/amd/amdkfd/kfd_device_queue_manager.c| 135 +++++++++++++----- drivers/gpu/drm/amd/amdkfd/kfd_priv.h | 2 + drivers/gpu/drm/amd/amdkfd/kfd_process.c | 3 +3 files changed, 107 insertions(+), 33 deletions(-)drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +1
-0@@ -52,12 +52,14 @@ static int unmap_queues_cpsch(structdevice_queue_manager *dqm, static int map_queues_cpsch(struct device_queue_manager *dqm); static void deallocate_sdma_queue(struct device_queue_manager *dqm, + struct qcm_process_device *qpd, struct queue *q); static inline void deallocate_hqd(struct device_queue_manager *dqm, struct queue *q);static int
allocate_hqd(struct device_queue_manager *dqm, struct queue *q);
static int allocate_sdma_queue(structdevice_queue_manager*dqm, + structqcm_process_device*qpd,
struct queue *q);
static voidkfd_process_hw_exception(struct work_struct *work); @@ -349,7 +351,7 @@ static int create_queue_nocpsch(structdevice_queue_manager*dqm,
q->pipe,q->queue);
} else if (q->properties.type==KFD_QUEUE_TYPE_SDMA||q->properties.type==KFD_QUEUE_TYPE_SDMA_XGMI) { - retval =allocate_sdma_queue(dqm, q);
+ retval =allocate_sdma_queue(dqm, qpd, q);
if (retval)
gotodeallocate_vmid;dqm->asic_ops.init_sdma_vm(dqm, q, qpd);drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +0
-0@@ -410,7 +412,7 @@ static int create_queue_nocpsch(structdevice_queue_manager *dqm, deallocate_hqd(dqm, q);else if (
q->properties.type==KFD_QUEUE_TYPE_SDMA||q->properties.type==KFD_QUEUE_TYPE_SDMA_XGMI) -deallocate_sdma_queue(dqm, q);
+deallocate_sdma_queue(dqm, qpd, q);deallocate_vmid:
if (list_empty(&qpd->queues_list))deallocate_vmid(dqm, qpd, q);drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +2
-2@@ -475,9 +477,9 @@ static int destroy_queue_nocpsch_locked(struct device_queue_manager *dqm, if (q->properties.type == KFD_QUEUE_TYPE_COMPUTE) deallocate_hqd(dqm, q); else if (q->properties.type == KFD_QUEUE_TYPE_SDMA) - deallocate_sdma_queue(dqm, q); + deallocate_sdma_queue(dqm, qpd, q); else if (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) - deallocate_sdma_queue(dqm, q); + deallocate_sdma_queue(dqm, qpd, q); else { pr_debug("q->properties.type %d is invalid\n",
q->properties.type);drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c static void pre_reset(struct · line 1044
Quoted 4 lines
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +0
-0@@ -1039,42 +1041,93 @@ static void pre_reset(struct device_queue_manager *dqm) dqm_unlock(dqm); } +static uint64_t sdma_engine_mask(int engine, int num_engines)Looks more like the queue mask for an engine, the name doesn't make it clear.
Quoted 11 lines
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +11
-0+{ + uint64_t mask = 0; + + engine %= num_engines; + while (engine < (sizeof(uint64_t)*8)) { + mask |= 1ULL << engine; + engine += num_engines; + } + return mask; +} +drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c static void pre_reset(struct device_queue_manager *dqm) · line 1048
Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +4
-2static int allocate_sdma_queue(struct device_queue_manager *dqm, + struct qcm_process_device *qpd, struct queue *q) { - int bit; + uint64_t available_queue_bitmap; + unsigned int bit, engine, num_engines; + bool found_sdma = false; if (q->properties.type == KFD_QUEUE_TYPE_SDMA) { - if (dqm->sdma_bitmap == 0) {This is still a valid optimization and no need to loop through if nothing is available anyway. Valid for XGMI loop also.
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c static void pre_reset(struct · line 1066
Quoted 3 lines
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +2
-0+ num_engines = get_num_sdma_engines(dqm); + for_each_set_bit(engine, &(qpd->sdma_engine_bitmap), num_engines) {Probably a naive question -
Quoted 7 lines
Theoretically there are 8 queues per engine as per the mask code. In the below code, there is an assumption that a process will use at best number of queues=max num of sdma engines or xgmi engines simultaneously. Is that true always? For ex: there are 2 sdma engines and 4 queues per engine. Can't multiple threads of a process initiate multiple sdma transactions > number of sdma engines available? This code limits that, but I don't know if that is a possible case.
When you use multiple SDMA queues on the same engine, the work from the queues gets serialized. You can either let the SDMA engine serialize work from multiple queues, or let ROCr serialize work from multiple threads on the same SDMA queue. There is no obvious benefit to let the SDMA engine do it. But there is a drawback: Fewer processes get to use SDMA.
Regards,
Felix
Quoted 207 lines
Thanks,
LijoQuoted 203 lines
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +57
-22+ /* Do not reuse SDMA0 for any subsequent SDMA queue + * requests on Aldebaran. If SDMA0's queues are all + * full, then this process should never use SDMA0 + * for any further requests + */ + if (dqm->dev->device_info->asic_family == CHIP_ALDEBARAN && + engine == 0) + qpd->sdma_engine_bitmap &= ~(1ULL << engine); + + available_queue_bitmap = sdma_engine_mask(engine, num_engines); + available_queue_bitmap &= dqm->sdma_bitmap; + + if (!available_queue_bitmap) + continue; + /* Take the selected engine off the list so we will not + * allocate two queues onto the same engine + */ + qpd->sdma_engine_bitmap &= ~(1ULL << engine); + found_sdma = true; + + bit = __ffs64(available_queue_bitmap); + dqm->sdma_bitmap &= ~(1ULL << bit); + q->sdma_id = bit; + q->properties.sdma_engine_id = q->sdma_id % num_engines; + q->properties.sdma_queue_id = q->sdma_id / num_engines; + break; + } + if (!found_sdma) { pr_err("No more SDMA queue to allocate\n"); return -ENOMEM; } - - bit = __ffs64(dqm->sdma_bitmap); - dqm->sdma_bitmap &= ~(1ULL << bit); - q->sdma_id = bit; - q->properties.sdma_engine_id = q->sdma_id % - get_num_sdma_engines(dqm); - q->properties.sdma_queue_id = q->sdma_id / - get_num_sdma_engines(dqm); } else if (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) { - if (dqm->xgmi_sdma_bitmap == 0) { + num_engines = get_num_xgmi_sdma_engines(dqm); + for_each_set_bit(engine, &(qpd->xgmi_sdma_engine_bitmap), num_engines) { + available_queue_bitmap = sdma_engine_mask(engine, num_engines); + available_queue_bitmap &= dqm->xgmi_sdma_bitmap; + + if (!available_queue_bitmap) + continue; + /* Take the selected engine off the list so we will not + * allocate two queues onto the same engine + */ + qpd->xgmi_sdma_engine_bitmap &= ~(1ULL << engine); + found_sdma = true; + + bit = __ffs64(available_queue_bitmap); + dqm->xgmi_sdma_bitmap &= ~(1ULL << bit); + q->sdma_id = bit; + /* sdma_engine_id is sdma id including + * both PCIe-optimized SDMAs and XGMI- + * optimized SDMAs. The calculation below + * assumes the first N engines are always + * PCIe-optimized ones + */ + q->properties.sdma_engine_id = get_num_sdma_engines(dqm) + + q->sdma_id % get_num_xgmi_sdma_engines(dqm); + q->properties.sdma_queue_id = q->sdma_id / + get_num_xgmi_sdma_engines(dqm); + break; + } + if (!found_sdma) { pr_err("No more XGMI SDMA queue to allocate\n"); return -ENOMEM; } - bit = __ffs64(dqm->xgmi_sdma_bitmap); - dqm->xgmi_sdma_bitmap &= ~(1ULL << bit); - q->sdma_id = bit; - /* sdma_engine_id is sdma id including - * both PCIe-optimized SDMAs and XGMI- - * optimized SDMAs. The calculation below - * assumes the first N engines are always - * PCIe-optimized ones - */ - q->properties.sdma_engine_id = get_num_sdma_engines(dqm) + - q->sdma_id % get_num_xgmi_sdma_engines(dqm); - q->properties.sdma_queue_id = q->sdma_id / - get_num_xgmi_sdma_engines(dqm);}
pr_debug("SDMA engine id: %d\n", q->properties.sdma_engine_id);drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +16
-0@@ -1084,16 +1137,32 @@ static int allocate_sdma_queue(structdevice_queue_manager *dqm, } static void deallocate_sdma_queue(struct device_queue_manager *dqm, + struct qcm_process_device *qpd, struct queue *q) { + uint32_t engine = q->properties.sdma_engine_id; + if (q->properties.type == KFD_QUEUE_TYPE_SDMA) { if (q->sdma_id >= get_num_sdma_queues(dqm)) return; dqm->sdma_bitmap |= (1ULL << q->sdma_id); + /* Don't give SDMA0 back to be reallocated on Aldebaran. + * It is only OK to use this engine from the first allocation + * within a process. + */ + if (!(dqm->dev->device_info->asic_family == CHIP_ALDEBARAN && + engine == 0)) + qpd->sdma_engine_bitmap |= (1ULL << engine); + } else if (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) { if (q->sdma_id >= get_num_xgmi_sdma_queues(dqm)) return; dqm->xgmi_sdma_bitmap |= (1ULL << q->sdma_id); + /* engine ID in the queue properties is the global engine ID. + * The XGMI engine bitmap ignores the PCIe-optimized engines. + */ + engine -= get_num_sdma_engines(dqm); + qpd->xgmi_sdma_engine_bitmap |= (1ULL << engine); } }@@ -1303,7 +1372,7 @@ static int create_queue_cpsch(struct
device_queue_manager*dqm, struct queue *q,
if (q->properties.type==KFD_QUEUE_TYPE_SDMA||q->properties.type==KFD_QUEUE_TYPE_SDMA_XGMI) {dqm_lock(dqm);
- retval =allocate_sdma_queue(dqm, q);
+ retval =allocate_sdma_queue(dqm, qpd, q);dqm_unlock(dqm);
if (retval)
goto out;drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +0
-0@@ -1365,7 +1434,7 @@ static int create_queue_cpsch(struct device_queue_manager *dqm, struct queue *q,if (
q->properties.type==KFD_QUEUE_TYPE_SDMA||q->properties.type==KFD_QUEUE_TYPE_SDMA_XGMI) {dqm_lock(dqm);
-deallocate_sdma_queue(dqm, q);
+deallocate_sdma_queue(dqm, qpd, q);dqm_unlock(dqm);
}
out:drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +0
-0@@ -1536,7 +1605,7 @@ static int destroy_queue_cpsch(struct device_queue_manager *dqm,if ((
q->properties.type==KFD_QUEUE_TYPE_SDMA) || (q->properties.type==KFD_QUEUE_TYPE_SDMA_XGMI)) { -deallocate_sdma_queue(dqm, q);
+deallocate_sdma_queue(dqm, qpd, q);pdd->sdma_past_activity_counter+=sdma_val;
}
@@ -1751,9 +1820,9 @@ static int process_termination_cpsch(structdevice_queue_manager*dqm,
/* Clear all user mode queues */list_for_each_entry(q, &qpd->queues_list, list){ if (q->properties.type==KFD_QUEUE_TYPE_SDMA) -deallocate_sdma_queue(dqm, q);
+deallocate_sdma_queue(dqm, qpd, q);
else if (q->properties.type==KFD_QUEUE_TYPE_SDMA_XGMI) -deallocate_sdma_queue(dqm, q);
+deallocate_sdma_queue(dqm, qpd, q);
if (q->properties.is_active) {decrement_queue_count(dqm, q->properties.type);drivers/gpu/drm/amd/amdkfd/kfd_priv.h +2
-0@@ -576,6 +576,8 @@ struct qcm_process_device { struct list_head priv_queue_list; unsigned int queue_count; + unsigned long sdma_engine_bitmap; + unsigned long xgmi_sdma_engine_bitmap; unsigned int vmid; bool is_debug;unsigned int evicted; /* eviction counter, 0=active */
drivers/gpu/drm/amd/amdkfd/kfd_process.c +1
-0@@ -1422,6 +1422,7 @@ struct kfd_process_device *kfd_create_process_device_data(struct kfd_dev *dev, struct kfd_process *p) { struct kfd_process_device *pdd = NULL; + const struct kfd_device_info *dev_info = dev->dqm->dev->device_info; if (WARN_ON_ONCE(p->n_pdds >= MAX_GPU_INSTANCE))return NULL;
drivers/gpu/drm/amd/amdkfd/kfd_process.c +2
-0@@ -1446,6 +1447,8 @@ struct kfd_process_device *kfd_create_process_device_data(struct kfd_dev *dev, pdd->qpd.pqm = &p->pqm; pdd->qpd.evicted = 0; pdd->qpd.mapped_gws_queue = false; + pdd->qpd.sdma_engine_bitmap = BIT_ULL(dev_info->num_sdma_engines) - 1; + pdd->qpd.xgmi_sdma_engine_bitmap = BIT_ULL(dev_info->num_xgmi_sdma_engines) - 1;
pdd->process= p;
pdd->bound=PDD_UNBOUND;
pdd->already_dequeued= false;
Quoted 131 lines · Felix Kuehling
Am 2021-08-23 um 3:08 a.m. schrieb Lazar, Lijo:
Quoted 122 lines · Joseph Greathouse
Quoted 80 lines
Give every process at most one queue from each SDMA engine. Previously, we allocated all SDMA engines and queues on a first- come-first-serve basis. This meant that it was possible for two processes racing on their allocation requests to each end up with two queues on the same SDMA engine. That could serialize the performance of commands to different queues.
This new mechanism allows each process at most a single queue on each SDMA engine. Processes will check for queue availability on SDMA engine 0, then 1, then 2, etc. and only allocate on that engine if there is an available queue slot. If there are no queue slots available (or if this process has already allocated a queue on this engine), it moves on and tries the next engine.
The Aldebaran chip has a small quirk that SDMA0 should only be used by the very first allocation from each process. It is OK to use any other SDMA engine at any time, but after the first SDMA allocation request from a process, the resulting engine must be from SDMA1 or above. This patch handles this case as well.
Signed-off-by Joseph Greathouse <[email protected]>--- .../drm/amd/amdkfd/kfd_device_queue_manager.c | 135 +++++++++++++----- drivers/gpu/drm/amd/amdkfd/kfd_priv.h | 2 + drivers/gpu/drm/amd/amdkfd/kfd_process.c | 3 + 3 files changed, 107 insertions(+), 33 deletions(-)drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +2
-0@@ -52,12 +52,14 @@ static int unmap_queues_cpsch(structdevice_queue_manager *dqm, static int map_queues_cpsch(struct device_queue_manager *dqm); static void deallocate_sdma_queue(struct device_queue_manager *dqm, + struct qcm_process_device *qpd, struct queue *q); static inline void deallocate_hqd(struct device_queue_manager *dqm, struct queue *q); static int allocate_hqd(struct device_queue_manager *dqm, structqueue *q); static int allocate_sdma_queue(struct device_queue_manager *dqm, + struct qcm_process_device *qpd, struct queue *q); static void kfd_process_hw_exception(struct work_struct *work); @@ -349,7 +351,7 @@ static int create_queue_nocpsch(struct
device_queue_manager*dqm,
q->pipe,q->queue);
} else if (q->properties.type==KFD_QUEUE_TYPE_SDMA||q->properties.type==KFD_QUEUE_TYPE_SDMA_XGMI) {
- retval =allocate_sdma_queue(dqm, q);
+ retval =allocate_sdma_queue(dqm, qpd, q);
if (retval)
gotodeallocate_vmid;dqm->asic_ops.init_sdma_vm(dqm, q, qpd);drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +1
-1@@ -410,7 +412,7 @@ static int create_queue_nocpsch(structdevice_queue_manager *dqm, deallocate_hqd(dqm, q); else if (q->properties.type == KFD_QUEUE_TYPE_SDMA || q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) - deallocate_sdma_queue(dqm, q); + deallocate_sdma_queue(dqm, qpd, q); deallocate_vmid: if (list_empty(&qpd->queues_list))
deallocate_vmid(dqm, qpd, q);drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +2
-2@@ -475,9 +477,9 @@ static int destroy_queue_nocpsch_locked(struct device_queue_manager *dqm, if (q->properties.type == KFD_QUEUE_TYPE_COMPUTE) deallocate_hqd(dqm, q); else if (q->properties.type == KFD_QUEUE_TYPE_SDMA) - deallocate_sdma_queue(dqm, q); + deallocate_sdma_queue(dqm, qpd, q); else if (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) - deallocate_sdma_queue(dqm, q); + deallocate_sdma_queue(dqm, qpd, q); else { pr_debug("q->properties.type %d is invalid\n",
q->properties.type);drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c static void pre_reset(struct · line 1044
Quoted 4 lines
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +0
-0@@ -1039,42 +1041,93 @@ static void pre_reset(struct device_queue_manager *dqm) dqm_unlock(dqm); } +static uint64_t sdma_engine_mask(int engine, int num_engines)Looks more like the queue mask for an engine, the name doesn't make it clear.
Quoted 11 lines
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +11
-0+{ + uint64_t mask = 0; + + engine %= num_engines; + while (engine < (sizeof(uint64_t)*8)) { + mask |= 1ULL << engine; + engine += num_engines; + } + return mask; +} +drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c static void pre_reset(struct device_queue_manager *dqm) · line 1048
Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +4
-2static int allocate_sdma_queue(struct device_queue_manager *dqm, + struct qcm_process_device *qpd, struct queue *q) { - int bit; + uint64_t available_queue_bitmap; + unsigned int bit, engine, num_engines; + bool found_sdma = false; if (q->properties.type == KFD_QUEUE_TYPE_SDMA) { - if (dqm->sdma_bitmap == 0) {This is still a valid optimization and no need to loop through if nothing is available anyway. Valid for XGMI loop also.
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c static void pre_reset(struct · line 1066
Quoted 3 lines
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +2
-0+ num_engines = get_num_sdma_engines(dqm); + for_each_set_bit(engine, &(qpd->sdma_engine_bitmap), num_engines) {Probably a naive question -
Quoted 7 lines
Theoretically there are 8 queues per engine as per the mask code. In the below code, there is an assumption that a process will use at best number of queues=max num of sdma engines or xgmi engines simultaneously. Is that true always? For ex: there are 2 sdma engines and 4 queues per engine. Can't multiple threads of a process initiate multiple sdma transactions > number of sdma engines available? This code limits that, but I don't know if that is a possible case.
Quoted 6 lines
When you use multiple SDMA queues on the same engine, the work from the queues gets serialized. You can either let the SDMA engine serialize work from multiple queues, or let ROCr serialize work from multiple threads on the same SDMA queue. There is no obvious benefit to let the SDMA engine do it. But there is a drawback: Fewer processes get to use SDMA.
The fact that there exists multiple queues and there is no such assumption made in existing logic (prior to the patch) tells that there is indeed some advantage of making use of hardware queues. For ex: switching to different ring buffer may not need context save/resubmission.
Just like different processes, different threads of same process may take advantage of multiple queues. More interested to see the impact in cases where the focus is on single process ex: impact on running some benchmarks or in HPC where entire hardware is dedicated for a specific compute purpose.
Thanks,
Lijo
Quoted 211 lines
Regards,
FelixQuoted 207 lines
Thanks,
LijoQuoted 203 lines
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +73
-22+ /* Do not reuse SDMA0 for any subsequent SDMA queue + * requests on Aldebaran. If SDMA0's queues are all + * full, then this process should never use SDMA0 + * for any further requests + */ + if (dqm->dev->device_info->asic_family == CHIP_ALDEBARAN && + engine == 0) + qpd->sdma_engine_bitmap &= ~(1ULL << engine); + + available_queue_bitmap = sdma_engine_mask(engine, num_engines); + available_queue_bitmap &= dqm->sdma_bitmap; + + if (!available_queue_bitmap) + continue; + /* Take the selected engine off the list so we will not + * allocate two queues onto the same engine + */ + qpd->sdma_engine_bitmap &= ~(1ULL << engine); + found_sdma = true; + + bit = __ffs64(available_queue_bitmap); + dqm->sdma_bitmap &= ~(1ULL << bit); + q->sdma_id = bit; + q->properties.sdma_engine_id = q->sdma_id % num_engines; + q->properties.sdma_queue_id = q->sdma_id / num_engines; + break; + } + if (!found_sdma) { pr_err("No more SDMA queue to allocate\n"); return -ENOMEM; } - - bit = __ffs64(dqm->sdma_bitmap); - dqm->sdma_bitmap &= ~(1ULL << bit); - q->sdma_id = bit; - q->properties.sdma_engine_id = q->sdma_id % - get_num_sdma_engines(dqm); - q->properties.sdma_queue_id = q->sdma_id / - get_num_sdma_engines(dqm); } else if (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) { - if (dqm->xgmi_sdma_bitmap == 0) { + num_engines = get_num_xgmi_sdma_engines(dqm); + for_each_set_bit(engine, &(qpd->xgmi_sdma_engine_bitmap), num_engines) { + available_queue_bitmap = sdma_engine_mask(engine, num_engines); + available_queue_bitmap &= dqm->xgmi_sdma_bitmap; + + if (!available_queue_bitmap) + continue; + /* Take the selected engine off the list so we will not + * allocate two queues onto the same engine + */ + qpd->xgmi_sdma_engine_bitmap &= ~(1ULL << engine); + found_sdma = true; + + bit = __ffs64(available_queue_bitmap); + dqm->xgmi_sdma_bitmap &= ~(1ULL << bit); + q->sdma_id = bit; + /* sdma_engine_id is sdma id including + * both PCIe-optimized SDMAs and XGMI- + * optimized SDMAs. The calculation below + * assumes the first N engines are always + * PCIe-optimized ones + */ + q->properties.sdma_engine_id = get_num_sdma_engines(dqm) + + q->sdma_id % get_num_xgmi_sdma_engines(dqm); + q->properties.sdma_queue_id = q->sdma_id / + get_num_xgmi_sdma_engines(dqm); + break; + } + if (!found_sdma) { pr_err("No more XGMI SDMA queue to allocate\n"); return -ENOMEM; } - bit = __ffs64(dqm->xgmi_sdma_bitmap); - dqm->xgmi_sdma_bitmap &= ~(1ULL << bit); - q->sdma_id = bit; - /* sdma_engine_id is sdma id including - * both PCIe-optimized SDMAs and XGMI- - * optimized SDMAs. The calculation below - * assumes the first N engines are always - * PCIe-optimized ones - */ - q->properties.sdma_engine_id = get_num_sdma_engines(dqm) + - q->sdma_id % get_num_xgmi_sdma_engines(dqm); - q->properties.sdma_queue_id = q->sdma_id / - get_num_xgmi_sdma_engines(dqm); } pr_debug("SDMA engine id: %d\n", q->properties.sdma_engine_id);@@ -1084,16 +1137,32 @@ static int allocate_sdma_queue(structdevice_queue_manager *dqm, } static void deallocate_sdma_queue(struct device_queue_manager *dqm, + struct qcm_process_device *qpd, struct queue *q) { + uint32_t engine = q->properties.sdma_engine_id; + if (q->properties.type == KFD_QUEUE_TYPE_SDMA) { if (q->sdma_id >= get_num_sdma_queues(dqm)) return; dqm->sdma_bitmap |= (1ULL << q->sdma_id); + /* Don't give SDMA0 back to be reallocated on Aldebaran. + * It is only OK to use this engine from the first allocation + * within a process. + */ + if (!(dqm->dev->device_info->asic_family == CHIP_ALDEBARAN && + engine == 0)) + qpd->sdma_engine_bitmap |= (1ULL << engine); + } else if (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) { if (q->sdma_id >= get_num_xgmi_sdma_queues(dqm)) return; dqm->xgmi_sdma_bitmap |= (1ULL << q->sdma_id); + /* engine ID in the queue properties is the global engine ID. + * The XGMI engine bitmap ignores the PCIe-optimized engines. + */ + engine -= get_num_sdma_engines(dqm); + qpd->xgmi_sdma_engine_bitmap |= (1ULL << engine); } } @@ -1303,7 +1372,7 @@ static int create_queue_cpsch(struct
device_queue_manager*dqm, struct queue *q,
if (q->properties.type==KFD_QUEUE_TYPE_SDMA||q->properties.type==KFD_QUEUE_TYPE_SDMA_XGMI) {dqm_lock(dqm);
- retval =allocate_sdma_queue(dqm, q);
+ retval =allocate_sdma_queue(dqm, qpd, q);
dqm_unlock(dqm);
if (retval)
goto out;drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +1
-1@@ -1365,7 +1434,7 @@ static int create_queue_cpsch(struct device_queue_manager *dqm, struct queue *q, if (q->properties.type == KFD_QUEUE_TYPE_SDMA || q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) { dqm_lock(dqm); - deallocate_sdma_queue(dqm, q); + deallocate_sdma_queue(dqm, qpd, q); dqm_unlock(dqm); }out:
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +1
-1@@ -1536,7 +1605,7 @@ static int destroy_queue_cpsch(struct device_queue_manager *dqm, if ((q->properties.type == KFD_QUEUE_TYPE_SDMA) || (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)) { - deallocate_sdma_queue(dqm, q); + deallocate_sdma_queue(dqm, qpd, q); pdd->sdma_past_activity_counter += sdma_val; } @@ -1751,9 +1820,9 @@ static int process_termination_cpsch(struct
device_queue_manager*dqm,
/* Clear all user mode queues */list_for_each_entry(q, &qpd->queues_list, list){ if (q->properties.type==KFD_QUEUE_TYPE_SDMA)
-deallocate_sdma_queue(dqm, q);
+deallocate_sdma_queue(dqm, qpd, q);
else if (q->properties.type==KFD_QUEUE_TYPE_SDMA_XGMI)
-deallocate_sdma_queue(dqm, q);
+deallocate_sdma_queue(dqm, qpd, q);
if (q->properties.is_active) {decrement_queue_count(dqm, q->properties.type);drivers/gpu/drm/amd/amdkfd/kfd_priv.h +2
-0@@ -576,6 +576,8 @@ struct qcm_process_device { struct list_head priv_queue_list; unsigned int queue_count; + unsigned long sdma_engine_bitmap; + unsigned long xgmi_sdma_engine_bitmap; unsigned int vmid; bool is_debug; unsigned int evicted; /* eviction counter, 0=active */drivers/gpu/drm/amd/amdkfd/kfd_process.c +1
-0@@ -1422,6 +1422,7 @@ struct kfd_process_device *kfd_create_process_device_data(struct kfd_dev *dev, struct kfd_process *p) { struct kfd_process_device *pdd = NULL; + const struct kfd_device_info *dev_info = dev->dqm->dev->device_info; if (WARN_ON_ONCE(p->n_pdds >= MAX_GPU_INSTANCE))return NULL;
drivers/gpu/drm/amd/amdkfd/kfd_process.c +2
-0@@ -1446,6 +1447,8 @@ struct kfd_process_device *kfd_create_process_device_data(struct kfd_dev *dev, pdd->qpd.pqm = &p->pqm; pdd->qpd.evicted = 0; pdd->qpd.mapped_gws_queue = false; + pdd->qpd.sdma_engine_bitmap = BIT_ULL(dev_info->num_sdma_engines) - 1; + pdd->qpd.xgmi_sdma_engine_bitmap = BIT_ULL(dev_info->num_xgmi_sdma_engines) - 1;
pdd->process= p;
pdd->bound=PDD_UNBOUND;
pdd->already_dequeued= false;
Quoted 151 lines
-----Original Message-----
From: Lazar, Lijo <[email protected]>
Sent: Monday, August 23, 2021 11:37 PM
To: Kuehling, Felix <[email protected]>; Greathouse, Joseph <[email protected]>; amd- [email protected]
Subject: Re: [PATCH 1/3] drm/amdkfd: Allocate SDMA engines more fairlyQuoted 131 lines · Felix Kuehling
Am 2021-08-23 um 3:08 a.m. schrieb Lazar, Lijo:
Quoted 122 lines · Joseph Greathouse
Quoted 80 lines
Give every process at most one queue from each SDMA engine. Previously, we allocated all SDMA engines and queues on a first- come-first-serve basis. This meant that it was possible for two processes racing on their allocation requests to each end up with two queues on the same SDMA engine. That could serialize the performance of commands to different queues.
This new mechanism allows each process at most a single queue on each SDMA engine. Processes will check for queue availability on SDMA engine 0, then 1, then 2, etc. and only allocate on that engine if there is an available queue slot. If there are no queue slots available (or if this process has already allocated a queue on this engine), it moves on and tries the next engine.
The Aldebaran chip has a small quirk that SDMA0 should only be used by the very first allocation from each process. It is OK to use any other SDMA engine at any time, but after the first SDMA allocation request from a process, the resulting engine must be from SDMA1 or above. This patch handles this case as well.
Signed-off-by Joseph Greathouse <[email protected]>--- .../drm/amd/amdkfd/kfd_device_queue_manager.c | 135 +++++++++++++----- drivers/gpu/drm/amd/amdkfd/kfd_priv.h | 2 + drivers/gpu/drm/amd/amdkfd/kfd_process.c | 3 + 3 files changed, 107 insertions(+), 33 deletions(-)drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +2
-0@@ -52,12 +52,14 @@ static int unmap_queues_cpsch(structdevice_queue_manager *dqm, static int map_queues_cpsch(struct device_queue_manager *dqm); static void deallocate_sdma_queue(struct device_queue_manager *dqm, + struct qcm_process_device *qpd, struct queue *q); static inline void deallocate_hqd(struct device_queue_manager *dqm, struct queue *q); static int allocate_hqd(struct device_queue_manager *dqm, structqueue *q); static int allocate_sdma_queue(struct device_queue_manager *dqm, + struct qcm_process_device *qpd, struct queue *q); static void kfd_process_hw_exception(struct work_struct *work); @@ -349,7 +351,7 @@ static int create_queue_nocpsch(struct
device_queue_manager*dqm,
q->pipe,q->queue);
} else if (q->properties.type==KFD_QUEUE_TYPE_SDMA||q->properties.type==KFD_QUEUE_TYPE_SDMA_XGMI) {
- retval =allocate_sdma_queue(dqm, q);
+ retval =allocate_sdma_queue(dqm, qpd, q);
if (retval)
gotodeallocate_vmid;dqm->asic_ops.init_sdma_vm(dqm, q, qpd);drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +1
-1@@ -410,7 +412,7 @@ static int create_queue_nocpsch(structdevice_queue_manager *dqm, deallocate_hqd(dqm, q); else if (q->properties.type == KFD_QUEUE_TYPE_SDMA || q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) - deallocate_sdma_queue(dqm, q); + deallocate_sdma_queue(dqm, qpd, q); deallocate_vmid: if (list_empty(&qpd->queues_list))
deallocate_vmid(dqm, qpd, q);drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +2
-2@@ -475,9 +477,9 @@ static int destroy_queue_nocpsch_locked(struct device_queue_manager *dqm, if (q->properties.type == KFD_QUEUE_TYPE_COMPUTE) deallocate_hqd(dqm, q); else if (q->properties.type == KFD_QUEUE_TYPE_SDMA) - deallocate_sdma_queue(dqm, q); + deallocate_sdma_queue(dqm, qpd, q); else if (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) - deallocate_sdma_queue(dqm, q); + deallocate_sdma_queue(dqm, qpd, q); else { pr_debug("q->properties.type %d is invalid\n",
q->properties.type);drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c static void pre_reset(struct · line 1044
Quoted 4 lines
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +0
-0@@ -1039,42 +1041,93 @@ static void pre_reset(struct device_queue_manager *dqm) dqm_unlock(dqm); } +static uint64_t sdma_engine_mask(int engine, int num_engines)Looks more like the queue mask for an engine, the name doesn't make it clear.
Quoted 11 lines
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +11
-0+{ + uint64_t mask = 0; + + engine %= num_engines; + while (engine < (sizeof(uint64_t)*8)) { + mask |= 1ULL << engine; + engine += num_engines; + } + return mask; +} +drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c static void pre_reset(struct device_queue_manager *dqm) · line 1048
Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +4
-2static int allocate_sdma_queue(struct device_queue_manager *dqm, + struct qcm_process_device *qpd, struct queue *q) { - int bit; + uint64_t available_queue_bitmap; + unsigned int bit, engine, num_engines; + bool found_sdma = false; if (q->properties.type == KFD_QUEUE_TYPE_SDMA) { - if (dqm->sdma_bitmap == 0) {This is still a valid optimization and no need to loop through if nothing is available anyway. Valid for XGMI loop also.
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c static void pre_reset(struct · line 1066
Quoted 3 lines
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +2
-0+ num_engines = get_num_sdma_engines(dqm); + for_each_set_bit(engine, &(qpd->sdma_engine_bitmap), num_engines) {Probably a naive question -
Quoted 7 lines
Theoretically there are 8 queues per engine as per the mask code. In the below code, there is an assumption that a process will use at best number of queues=max num of sdma engines or xgmi engines simultaneously. Is that true always? For ex: there are 2 sdma engines and 4 queues per engine. Can't multiple threads of a process initiate multiple sdma transactions > number of sdma engines available? This code limits that, but I don't know if that is a possible case.
Quoted 6 lines
When you use multiple SDMA queues on the same engine, the work from the queues gets serialized. You can either let the SDMA engine serialize work from multiple queues, or let ROCr serialize work from multiple threads on the same SDMA queue. There is no obvious benefit to let the SDMA engine do it. But there is a drawback: Fewer processes get to use SDMA.
The fact that there exists multiple queues and there is no such assumption made in existing logic (prior to the patch) tells that there is indeed some advantage of making use of hardware queues. For ex:
switching to different ring buffer may not need context save/resubmission.
Quoted 5 lines
Just like different processes, different threads of same process may take advantage of multiple queues. More interested to see the impact in cases where the focus is on single process ex: impact on running some benchmarks or in HPC where entire hardware is dedicated for a specific compute purpose.
Each engine has multiple user-mode queues because we expect to see multiple processes sharing the device at the same time. The internal datasheets from the hardware team strongly suggest using one user-mode queue per engine per process.
With that in mind, the ROCr user-mode runtime attempts to create, at most, one queue per SDMA engine within each process. For instance, ROCr creates one queue for H->D transfers and one queue for D->H transfers. We want these queues to be on different engines so that we can get full-duplex PCIe transfers. If both queues are on the same engine, the engine will serialize the transfers (because it can only work on one command at a time), and we will get half-duplex.
The queue allocation algorithm that this patch replaces could accidentally yield two queues on the same engine. If Process A allocates its first queue, it would receive sdma0_queue0. Process B could then allocate its first queue, and get sdma1_queue0. Then Process A could allocate its second queue and get sdma0_queue1. This would yield undesirable and unexpected serialization between H->D and D->H transfers in Process A. In addition, I believe the old algorithm would allow an uncooperative process to allocate all SDMA queues to itself. Looking at the git history, I believe the old allocator is basically the same as the initial KFD SDMA allocator built for Kaveri. I'm confident we've learned more about how multiple processes should share our GPUs since then. :)
To your last point: even if we have different threads in the same process submitting transfer requests, the ROCr runtime within the process will submit all like transfers (e.g. all H->D transfers) into the same queue. Submitting those same commands into different queues on the same engine will not yield any clear performance benefit since the engine will serialize the commands. And it will likely yield some performance loss due to queue-switching overheads in the engine. I think this patch yields a more fair allocation mechanism (e.g., no process can starve out other processes by taking all the queues on all engines), and it also more closely match what user-space expects (e.g., different SDMA queues can run commands in parallel).
And thanks for the code suggestions up above, I will make those changes as requested.
Thanks!
-Joe
Quoted 214 lines
Thanks,
LijoQuoted 211 lines
Regards,
FelixQuoted 207 lines
Thanks,
LijoQuoted 203 lines
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +73
-22+ /* Do not reuse SDMA0 for any subsequent SDMA queue + * requests on Aldebaran. If SDMA0's queues are all + * full, then this process should never use SDMA0 + * for any further requests + */ + if (dqm->dev->device_info->asic_family == CHIP_ALDEBARAN && + engine == 0) + qpd->sdma_engine_bitmap &= ~(1ULL << engine); + + available_queue_bitmap = sdma_engine_mask(engine, num_engines); + available_queue_bitmap &= dqm->sdma_bitmap; + + if (!available_queue_bitmap) + continue; + /* Take the selected engine off the list so we will not + * allocate two queues onto the same engine + */ + qpd->sdma_engine_bitmap &= ~(1ULL << engine); + found_sdma = true; + + bit = __ffs64(available_queue_bitmap); + dqm->sdma_bitmap &= ~(1ULL << bit); + q->sdma_id = bit; + q->properties.sdma_engine_id = q->sdma_id % num_engines; + q->properties.sdma_queue_id = q->sdma_id / num_engines; + break; + } + if (!found_sdma) { pr_err("No more SDMA queue to allocate\n"); return -ENOMEM; } - - bit = __ffs64(dqm->sdma_bitmap); - dqm->sdma_bitmap &= ~(1ULL << bit); - q->sdma_id = bit; - q->properties.sdma_engine_id = q->sdma_id % - get_num_sdma_engines(dqm); - q->properties.sdma_queue_id = q->sdma_id / - get_num_sdma_engines(dqm); } else if (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) { - if (dqm->xgmi_sdma_bitmap == 0) { + num_engines = get_num_xgmi_sdma_engines(dqm); + for_each_set_bit(engine, &(qpd->xgmi_sdma_engine_bitmap), num_engines) { + available_queue_bitmap = sdma_engine_mask(engine, num_engines); + available_queue_bitmap &= dqm->xgmi_sdma_bitmap; + + if (!available_queue_bitmap) + continue; + /* Take the selected engine off the list so we will not + * allocate two queues onto the same engine + */ + qpd->xgmi_sdma_engine_bitmap &= ~(1ULL << engine); + found_sdma = true; + + bit = __ffs64(available_queue_bitmap); + dqm->xgmi_sdma_bitmap &= ~(1ULL << bit); + q->sdma_id = bit; + /* sdma_engine_id is sdma id including + * both PCIe-optimized SDMAs and XGMI- + * optimized SDMAs. The calculation below + * assumes the first N engines are always + * PCIe-optimized ones + */ + q->properties.sdma_engine_id = get_num_sdma_engines(dqm) + + q->sdma_id % get_num_xgmi_sdma_engines(dqm); + q->properties.sdma_queue_id = q->sdma_id / + get_num_xgmi_sdma_engines(dqm); + break; + } + if (!found_sdma) { pr_err("No more XGMI SDMA queue to allocate\n"); return -ENOMEM; } - bit = __ffs64(dqm->xgmi_sdma_bitmap); - dqm->xgmi_sdma_bitmap &= ~(1ULL << bit); - q->sdma_id = bit; - /* sdma_engine_id is sdma id including - * both PCIe-optimized SDMAs and XGMI- - * optimized SDMAs. The calculation below - * assumes the first N engines are always - * PCIe-optimized ones - */ - q->properties.sdma_engine_id = get_num_sdma_engines(dqm) + - q->sdma_id % get_num_xgmi_sdma_engines(dqm); - q->properties.sdma_queue_id = q->sdma_id / - get_num_xgmi_sdma_engines(dqm); } pr_debug("SDMA engine id: %d\n", q->properties.sdma_engine_id);@@ -1084,16 +1137,32 @@ static int allocate_sdma_queue(structdevice_queue_manager *dqm, } static void deallocate_sdma_queue(struct device_queue_manager *dqm, + struct qcm_process_device *qpd, struct queue *q) { + uint32_t engine = q->properties.sdma_engine_id; + if (q->properties.type == KFD_QUEUE_TYPE_SDMA) { if (q->sdma_id >= get_num_sdma_queues(dqm)) return; dqm->sdma_bitmap |= (1ULL << q->sdma_id); + /* Don't give SDMA0 back to be reallocated on Aldebaran. + * It is only OK to use this engine from the first allocation + * within a process. + */ + if (!(dqm->dev->device_info->asic_family == CHIP_ALDEBARAN && + engine == 0)) + qpd->sdma_engine_bitmap |= (1ULL << engine); + } else if (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) { if (q->sdma_id >= get_num_xgmi_sdma_queues(dqm)) return; dqm->xgmi_sdma_bitmap |= (1ULL << q->sdma_id); + /* engine ID in the queue properties is the global engine ID. + * The XGMI engine bitmap ignores the PCIe-optimized engines. + */ + engine -= get_num_sdma_engines(dqm); + qpd->xgmi_sdma_engine_bitmap |= (1ULL << engine); } } @@ -1303,7 +1372,7 @@ static int create_queue_cpsch(struct
device_queue_manager*dqm, struct queue *q,
if (q->properties.type==KFD_QUEUE_TYPE_SDMA||q->properties.type==KFD_QUEUE_TYPE_SDMA_XGMI) {dqm_lock(dqm);
- retval =allocate_sdma_queue(dqm, q);
+ retval =allocate_sdma_queue(dqm, qpd, q);
dqm_unlock(dqm);
if (retval)
goto out;drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +1
-1@@ -1365,7 +1434,7 @@ static int create_queue_cpsch(struct device_queue_manager *dqm, struct queue *q, if (q->properties.type == KFD_QUEUE_TYPE_SDMA || q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) { dqm_lock(dqm); - deallocate_sdma_queue(dqm, q); + deallocate_sdma_queue(dqm, qpd, q); dqm_unlock(dqm); }out:
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +1
-1@@ -1536,7 +1605,7 @@ static int destroy_queue_cpsch(struct device_queue_manager *dqm, if ((q->properties.type == KFD_QUEUE_TYPE_SDMA) || (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)) { - deallocate_sdma_queue(dqm, q); + deallocate_sdma_queue(dqm, qpd, q); pdd->sdma_past_activity_counter += sdma_val; } @@ -1751,9 +1820,9 @@ static int process_termination_cpsch(struct
device_queue_manager*dqm,
/* Clear all user mode queues */list_for_each_entry(q, &qpd->queues_list, list){ if (q->properties.type==KFD_QUEUE_TYPE_SDMA)
-deallocate_sdma_queue(dqm, q);
+deallocate_sdma_queue(dqm, qpd, q);
else if (q->properties.type==KFD_QUEUE_TYPE_SDMA_XGMI)
-deallocate_sdma_queue(dqm, q);
+deallocate_sdma_queue(dqm, qpd, q);
if (q->properties.is_active) {decrement_queue_count(dqm, q->properties.type);drivers/gpu/drm/amd/amdkfd/kfd_priv.h +2
-0@@ -576,6 +576,8 @@ struct qcm_process_device { struct list_head priv_queue_list; unsigned int queue_count; + unsigned long sdma_engine_bitmap; + unsigned long xgmi_sdma_engine_bitmap; unsigned int vmid; bool is_debug; unsigned int evicted; /* eviction counter, 0=active */drivers/gpu/drm/amd/amdkfd/kfd_process.c +1
-0@@ -1422,6 +1422,7 @@ struct kfd_process_device *kfd_create_process_device_data(struct kfd_dev *dev, struct kfd_process *p) { struct kfd_process_device *pdd = NULL; + const struct kfd_device_info *dev_info = dev->dqm->dev->device_info; if (WARN_ON_ONCE(p->n_pdds >= MAX_GPU_INSTANCE))return NULL;
drivers/gpu/drm/amd/amdkfd/kfd_process.c +2
-0@@ -1446,6 +1447,8 @@ struct kfd_process_device *kfd_create_process_device_data(struct kfd_dev *dev, pdd->qpd.pqm = &p->pqm; pdd->qpd.evicted = 0; pdd->qpd.mapped_gws_queue = false; + pdd->qpd.sdma_engine_bitmap = BIT_ULL(dev_info->num_sdma_engines) - 1; + pdd->qpd.xgmi_sdma_engine_bitmap = BIT_ULL(dev_info->num_xgmi_sdma_engines) - 1;
pdd->process= p;
pdd->bound=PDD_UNBOUND;
pdd->already_dequeued= false;
Quoted 157 lines · Joseph
Quoted 151 lines
-----Original Message-----
From: Lazar, Lijo <[email protected]>
Sent: Monday, August 23, 2021 11:37 PM
To: Kuehling, Felix <[email protected]>; Greathouse, Joseph <[email protected]>; amd- [email protected]
Subject: Re: [PATCH 1/3] drm/amdkfd: Allocate SDMA engines more fairlyQuoted 131 lines · Felix Kuehling
Am 2021-08-23 um 3:08 a.m. schrieb Lazar, Lijo:
Quoted 122 lines · Joseph Greathouse
Quoted 80 lines
Give every process at most one queue from each SDMA engine. Previously, we allocated all SDMA engines and queues on a first- come-first-serve basis. This meant that it was possible for two processes racing on their allocation requests to each end up with two queues on the same SDMA engine. That could serialize the performance of commands to different queues.
This new mechanism allows each process at most a single queue on each SDMA engine. Processes will check for queue availability on SDMA engine 0, then 1, then 2, etc. and only allocate on that engine if there is an available queue slot. If there are no queue slots available (or if this process has already allocated a queue on this engine), it moves on and tries the next engine.
The Aldebaran chip has a small quirk that SDMA0 should only be used by the very first allocation from each process. It is OK to use any other SDMA engine at any time, but after the first SDMA allocation request from a process, the resulting engine must be from SDMA1 or above. This patch handles this case as well.
Signed-off-by Joseph Greathouse <[email protected]>--- .../drm/amd/amdkfd/kfd_device_queue_manager.c | 135 +++++++++++++----- drivers/gpu/drm/amd/amdkfd/kfd_priv.h | 2 + drivers/gpu/drm/amd/amdkfd/kfd_process.c | 3 + 3 files changed, 107 insertions(+), 33 deletions(-)drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +2
-0@@ -52,12 +52,14 @@ static int unmap_queues_cpsch(structdevice_queue_manager *dqm, static int map_queues_cpsch(struct device_queue_manager *dqm); static void deallocate_sdma_queue(struct device_queue_manager *dqm, + struct qcm_process_device *qpd, struct queue *q); static inline void deallocate_hqd(struct device_queue_manager *dqm, struct queue *q); static int allocate_hqd(struct device_queue_manager *dqm, structqueue *q); static int allocate_sdma_queue(struct device_queue_manager *dqm, + struct qcm_process_device *qpd, struct queue *q); static void kfd_process_hw_exception(struct work_struct *work); @@ -349,7 +351,7 @@ static int create_queue_nocpsch(struct
device_queue_manager*dqm,
q->pipe,q->queue);
} else if (q->properties.type==KFD_QUEUE_TYPE_SDMA||q->properties.type==KFD_QUEUE_TYPE_SDMA_XGMI) {
- retval =allocate_sdma_queue(dqm, q);
+ retval =allocate_sdma_queue(dqm, qpd, q);
if (retval)
gotodeallocate_vmid;dqm->asic_ops.init_sdma_vm(dqm, q, qpd);drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +1
-1@@ -410,7 +412,7 @@ static int create_queue_nocpsch(structdevice_queue_manager *dqm, deallocate_hqd(dqm, q); else if (q->properties.type == KFD_QUEUE_TYPE_SDMA || q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) - deallocate_sdma_queue(dqm, q); + deallocate_sdma_queue(dqm, qpd, q); deallocate_vmid: if (list_empty(&qpd->queues_list))deallocate_vmid(dqm, qpd, q);drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +2
-2@@ -475,9 +477,9 @@ static int destroy_queue_nocpsch_locked(struct device_queue_manager *dqm, if (q->properties.type == KFD_QUEUE_TYPE_COMPUTE) deallocate_hqd(dqm, q); else if (q->properties.type == KFD_QUEUE_TYPE_SDMA) - deallocate_sdma_queue(dqm, q); + deallocate_sdma_queue(dqm, qpd, q); else if (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) - deallocate_sdma_queue(dqm, q); + deallocate_sdma_queue(dqm, qpd, q); else { pr_debug("q->properties.type %d is invalid\n",
q->properties.type);drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c static void pre_reset(struct · line 1044
Quoted 4 lines
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +0
-0@@ -1039,42 +1041,93 @@ static void pre_reset(struct device_queue_manager *dqm) dqm_unlock(dqm); } +static uint64_t sdma_engine_mask(int engine, int num_engines)Looks more like the queue mask for an engine, the name doesn't make it clear.
Quoted 11 lines
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +11
-0+{ + uint64_t mask = 0; + + engine %= num_engines; + while (engine < (sizeof(uint64_t)*8)) { + mask |= 1ULL << engine; + engine += num_engines; + } + return mask; +} +drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c static void pre_reset(struct device_queue_manager *dqm) · line 1048
Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +4
-2static int allocate_sdma_queue(struct device_queue_manager *dqm, + struct qcm_process_device *qpd, struct queue *q) { - int bit; + uint64_t available_queue_bitmap; + unsigned int bit, engine, num_engines; + bool found_sdma = false; if (q->properties.type == KFD_QUEUE_TYPE_SDMA) { - if (dqm->sdma_bitmap == 0) {This is still a valid optimization and no need to loop through if nothing is available anyway. Valid for XGMI loop also.
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c static void pre_reset(struct · line 1066
Quoted 3 lines
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +2
-0+ num_engines = get_num_sdma_engines(dqm); + for_each_set_bit(engine, &(qpd->sdma_engine_bitmap), num_engines) {Probably a naive question -
Quoted 7 lines
Theoretically there are 8 queues per engine as per the mask code. In the below code, there is an assumption that a process will use at best number of queues=max num of sdma engines or xgmi engines simultaneously. Is that true always? For ex: there are 2 sdma engines and 4 queues per engine. Can't multiple threads of a process initiate multiple sdma transactions > number of sdma engines available? This code limits that, but I don't know if that is a possible case.
Quoted 6 lines
When you use multiple SDMA queues on the same engine, the work from the queues gets serialized. You can either let the SDMA engine serialize work from multiple queues, or let ROCr serialize work from multiple threads on the same SDMA queue. There is no obvious benefit to let the SDMA engine do it. But there is a drawback: Fewer processes get to use SDMA.
The fact that there exists multiple queues and there is no such assumption made in existing logic (prior to the patch) tells that there is indeed some advantage of making use of hardware queues. For ex:
switching to different ring buffer may not need context save/resubmission.Quoted 5 lines
Just like different processes, different threads of same process may take advantage of multiple queues. More interested to see the impact in cases where the focus is on single process ex: impact on running some benchmarks or in HPC where entire hardware is dedicated for a specific compute purpose.
Quoted 8 lines
Each engine has multiple user-mode queues because we expect to see multiple processes sharing the device at the same time. The internal datasheets from the hardware team strongly suggest using one user-mode queue per engine per process.
With that in mind, the ROCr user-mode runtime attempts to create, at most, one queue per SDMA engine within each process. For instance, ROCr creates one queue for
H->Dtransfers and one queue forD->Htransfers. We want these queues to be on different engines so that we can get full-duplex PCIe transfers. If both queues are on the same engine, the engine will serialize the transfers (because it can only work on one command at a time), and we will get half-duplex.The queue allocation algorithm that this patch replaces could accidentally yield two queues on the same engine. If Process A allocates its first queue, it would receive
sdma0_queue0. Process B could then allocate its first queue, and getsdma1_queue0. Then Process A could allocate its second queue and getsdma0_queue1. This would yield undesirable and unexpected serialization betweenH->DandD->Htransfers in Process A. In addition, I believe the old algorithm would allow an uncooperative process to allocate all SDMA queues to itself. Looking at the git history, I believe the old allocator is basically the same as the initial KFD SDMA allocator built for Kaveri. I'm confident we've learned more about how multiple processes should share our GPUs since then. :)To your last point: even if we have different threads in the same process submitting transfer requests, the ROCr runtime within the process will submit all like transfers (e.g. all
H->Dtransfers) into the same queue. Submitting those same commands into different queues on the same engine will not yield any clear performance benefit since the engine will serialize the commands. And it will likely yield some performance loss due to queue-switching overheads in the engine. I think this patch yields a more fair allocation mechanism (e.g., no process can starve out other processes by taking all the queues on all engines), and it also more closely match what user-space expects (e.g., different SDMA queues can run commands in parallel).
Thanks Joe for providing more info related to ROCr.
One other thing is - seems the process will keep waiting for the second queue (assuming first is still in use) to be alloted always on a separate engine. In the example cited above, do you expect a case where sdma1 queues are all full and Process A has to wait till a sdma1 queue is free even though there is a free queue available in sdma0 (by virtue of long/short jobs submitted earlier by other processes). Is that situation managed by ROCr? Or, as worst case allocate on sdma0 itself if nothing is available on other engines? Rather than going in any order, maybe another way is to order engines based on max free queues and give preference to the first engine not actively used by the process.
Thanks,
Lijo
Quoted 219 lines
And thanks for the code suggestions up above, I will make those changes as requested.
Thanks!
-JoeQuoted 214 lines
Thanks,
LijoQuoted 211 lines
Regards,
FelixQuoted 207 lines
Thanks,
LijoQuoted 203 lines
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +73
-22+ /* Do not reuse SDMA0 for any subsequent SDMA queue + * requests on Aldebaran. If SDMA0's queues are all + * full, then this process should never use SDMA0 + * for any further requests + */ + if (dqm->dev->device_info->asic_family == CHIP_ALDEBARAN && + engine == 0) + qpd->sdma_engine_bitmap &= ~(1ULL << engine); + + available_queue_bitmap = sdma_engine_mask(engine, num_engines); + available_queue_bitmap &= dqm->sdma_bitmap; + + if (!available_queue_bitmap) + continue; + /* Take the selected engine off the list so we will not + * allocate two queues onto the same engine + */ + qpd->sdma_engine_bitmap &= ~(1ULL << engine); + found_sdma = true; + + bit = __ffs64(available_queue_bitmap); + dqm->sdma_bitmap &= ~(1ULL << bit); + q->sdma_id = bit; + q->properties.sdma_engine_id = q->sdma_id % num_engines; + q->properties.sdma_queue_id = q->sdma_id / num_engines; + break; + } + if (!found_sdma) { pr_err("No more SDMA queue to allocate\n"); return -ENOMEM; } - - bit = __ffs64(dqm->sdma_bitmap); - dqm->sdma_bitmap &= ~(1ULL << bit); - q->sdma_id = bit; - q->properties.sdma_engine_id = q->sdma_id % - get_num_sdma_engines(dqm); - q->properties.sdma_queue_id = q->sdma_id / - get_num_sdma_engines(dqm); } else if (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) { - if (dqm->xgmi_sdma_bitmap == 0) { + num_engines = get_num_xgmi_sdma_engines(dqm); + for_each_set_bit(engine, &(qpd->xgmi_sdma_engine_bitmap), num_engines) { + available_queue_bitmap = sdma_engine_mask(engine, num_engines); + available_queue_bitmap &= dqm->xgmi_sdma_bitmap; + + if (!available_queue_bitmap) + continue; + /* Take the selected engine off the list so we will not + * allocate two queues onto the same engine + */ + qpd->xgmi_sdma_engine_bitmap &= ~(1ULL << engine); + found_sdma = true; + + bit = __ffs64(available_queue_bitmap); + dqm->xgmi_sdma_bitmap &= ~(1ULL << bit); + q->sdma_id = bit; + /* sdma_engine_id is sdma id including + * both PCIe-optimized SDMAs and XGMI- + * optimized SDMAs. The calculation below + * assumes the first N engines are always + * PCIe-optimized ones + */ + q->properties.sdma_engine_id = get_num_sdma_engines(dqm) + + q->sdma_id % get_num_xgmi_sdma_engines(dqm); + q->properties.sdma_queue_id = q->sdma_id / + get_num_xgmi_sdma_engines(dqm); + break; + } + if (!found_sdma) { pr_err("No more XGMI SDMA queue to allocate\n"); return -ENOMEM; } - bit = __ffs64(dqm->xgmi_sdma_bitmap); - dqm->xgmi_sdma_bitmap &= ~(1ULL << bit); - q->sdma_id = bit; - /* sdma_engine_id is sdma id including - * both PCIe-optimized SDMAs and XGMI- - * optimized SDMAs. The calculation below - * assumes the first N engines are always - * PCIe-optimized ones - */ - q->properties.sdma_engine_id = get_num_sdma_engines(dqm) + - q->sdma_id % get_num_xgmi_sdma_engines(dqm); - q->properties.sdma_queue_id = q->sdma_id / - get_num_xgmi_sdma_engines(dqm); } pr_debug("SDMA engine id: %d\n", q->properties.sdma_engine_id);@@ -1084,16 +1137,32 @@ static int allocate_sdma_queue(structdevice_queue_manager *dqm, } static void deallocate_sdma_queue(struct device_queue_manager *dqm, + struct qcm_process_device *qpd, struct queue *q) { + uint32_t engine = q->properties.sdma_engine_id; + if (q->properties.type == KFD_QUEUE_TYPE_SDMA) { if (q->sdma_id >= get_num_sdma_queues(dqm)) return; dqm->sdma_bitmap |= (1ULL << q->sdma_id); + /* Don't give SDMA0 back to be reallocated on Aldebaran. + * It is only OK to use this engine from the first allocation + * within a process. + */ + if (!(dqm->dev->device_info->asic_family == CHIP_ALDEBARAN && + engine == 0)) + qpd->sdma_engine_bitmap |= (1ULL << engine); + } else if (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) { if (q->sdma_id >= get_num_xgmi_sdma_queues(dqm)) return; dqm->xgmi_sdma_bitmap |= (1ULL << q->sdma_id); + /* engine ID in the queue properties is the global engine ID. + * The XGMI engine bitmap ignores the PCIe-optimized engines. + */ + engine -= get_num_sdma_engines(dqm); + qpd->xgmi_sdma_engine_bitmap |= (1ULL << engine); } } @@ -1303,7 +1372,7 @@ static int create_queue_cpsch(struct
device_queue_manager*dqm, struct queue *q,
if (q->properties.type==KFD_QUEUE_TYPE_SDMA||q->properties.type==KFD_QUEUE_TYPE_SDMA_XGMI) {dqm_lock(dqm);
- retval =allocate_sdma_queue(dqm, q);
+ retval =allocate_sdma_queue(dqm, qpd, q);
dqm_unlock(dqm);
if (retval)
goto out;drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +1
-1@@ -1365,7 +1434,7 @@ static int create_queue_cpsch(struct device_queue_manager *dqm, struct queue *q, if (q->properties.type == KFD_QUEUE_TYPE_SDMA || q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) { dqm_lock(dqm); - deallocate_sdma_queue(dqm, q); + deallocate_sdma_queue(dqm, qpd, q); dqm_unlock(dqm); }out:
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +1
-1@@ -1536,7 +1605,7 @@ static int destroy_queue_cpsch(struct device_queue_manager *dqm, if ((q->properties.type == KFD_QUEUE_TYPE_SDMA) || (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)) { - deallocate_sdma_queue(dqm, q); + deallocate_sdma_queue(dqm, qpd, q); pdd->sdma_past_activity_counter += sdma_val; } @@ -1751,9 +1820,9 @@ static int process_termination_cpsch(struct
device_queue_manager*dqm,
/* Clear all user mode queues */list_for_each_entry(q, &qpd->queues_list, list){ if (q->properties.type==KFD_QUEUE_TYPE_SDMA)
-deallocate_sdma_queue(dqm, q);
+deallocate_sdma_queue(dqm, qpd, q);
else if (q->properties.type==KFD_QUEUE_TYPE_SDMA_XGMI)
-deallocate_sdma_queue(dqm, q);
+deallocate_sdma_queue(dqm, qpd, q);
if (q->properties.is_active) {decrement_queue_count(dqm, q->properties.type);drivers/gpu/drm/amd/amdkfd/kfd_priv.h +2
-0@@ -576,6 +576,8 @@ struct qcm_process_device { struct list_head priv_queue_list; unsigned int queue_count; + unsigned long sdma_engine_bitmap; + unsigned long xgmi_sdma_engine_bitmap; unsigned int vmid; bool is_debug; unsigned int evicted; /* eviction counter, 0=active */drivers/gpu/drm/amd/amdkfd/kfd_process.c +1
-0@@ -1422,6 +1422,7 @@ struct kfd_process_device *kfd_create_process_device_data(struct kfd_dev *dev, struct kfd_process *p) { struct kfd_process_device *pdd = NULL; + const struct kfd_device_info *dev_info = dev->dqm->dev->device_info; if (WARN_ON_ONCE(p->n_pdds >= MAX_GPU_INSTANCE))return NULL;
drivers/gpu/drm/amd/amdkfd/kfd_process.c +2
-0@@ -1446,6 +1447,8 @@ struct kfd_process_device *kfd_create_process_device_data(struct kfd_dev *dev, pdd->qpd.pqm = &p->pqm; pdd->qpd.evicted = 0; pdd->qpd.mapped_gws_queue = false; + pdd->qpd.sdma_engine_bitmap = BIT_ULL(dev_info->num_sdma_engines) - 1; + pdd->qpd.xgmi_sdma_engine_bitmap = BIT_ULL(dev_info->num_xgmi_sdma_engines) - 1;pdd->process = p; pdd->bound = PDD_UNBOUND; pdd->already_dequeued = false;
Quoted 192 lines
-----Original Message-----
From: Lazar, Lijo <[email protected]>
Sent: Tuesday, August 24, 2021 2:24 AM
To: Greathouse, Joseph <[email protected]>; Kuehling, Felix <[email protected]>; amd- [email protected]
Subject: Re: [PATCH 1/3] drm/amdkfd: Allocate SDMA engines more fairlyQuoted 151 lines · Joseph
Quoted 151 lines
-----Original Message-----
From: Lazar, Lijo <[email protected]>
Sent: Monday, August 23, 2021 11:37 PM
To: Kuehling, Felix <[email protected]>; Greathouse, Joseph <[email protected]>; amd- [email protected]
Subject: Re: [PATCH 1/3] drm/amdkfd: Allocate SDMA engines more fairlyQuoted 131 lines · Felix Kuehling
Am 2021-08-23 um 3:08 a.m. schrieb Lazar, Lijo:
Quoted 122 lines · Joseph Greathouse
Quoted 80 lines
Give every process at most one queue from each SDMA engine. Previously, we allocated all SDMA engines and queues on a first- come-first-serve basis. This meant that it was possible for two processes racing on their allocation requests to each end up with two queues on the same SDMA engine. That could serialize the performance of commands to different queues.
This new mechanism allows each process at most a single queue on each SDMA engine. Processes will check for queue availability on SDMA engine 0, then 1, then 2, etc. and only allocate on that engine if there is an available queue slot. If there are no queue slots available (or if this process has already allocated a queue on this engine), it moves on and tries the next engine.
The Aldebaran chip has a small quirk that SDMA0 should only be used by the very first allocation from each process. It is OK to use any other SDMA engine at any time, but after the first SDMA allocation request from a process, the resulting engine must be from SDMA1 or above. This patch handles this case as well.
Signed-off-by Joseph Greathouse <[email protected]>--- .../drm/amd/amdkfd/kfd_device_queue_manager.c | 135 +++++++++++++----- drivers/gpu/drm/amd/amdkfd/kfd_priv.h | 2 + drivers/gpu/drm/amd/amdkfd/kfd_process.c | 3 + 3 files changed, 107 insertions(+), 33 deletions(-)drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +2
-0@@ -52,12 +52,14 @@ static int unmap_queues_cpsch(structdevice_queue_manager *dqm, static int map_queues_cpsch(struct device_queue_manager *dqm); static void deallocate_sdma_queue(struct device_queue_manager *dqm, + struct qcm_process_device *qpd, struct queue *q); static inline void deallocate_hqd(struct device_queue_manager *dqm, struct queue *q); static int allocate_hqd(struct device_queue_manager *dqm, structqueue *q); static int allocate_sdma_queue(struct device_queue_manager *dqm, + struct qcm_process_device *qpd, struct queue *q); static void kfd_process_hw_exception(struct work_struct *work); @@ -349,7 +351,7 @@ static int create_queue_nocpsch(struct
device_queue_manager*dqm,
q->pipe,q->queue);
} else if (q->properties.type==KFD_QUEUE_TYPE_SDMA||q->properties.type==KFD_QUEUE_TYPE_SDMA_XGMI) {
- retval =allocate_sdma_queue(dqm, q);
+ retval =allocate_sdma_queue(dqm, qpd, q);
if (retval)
gotodeallocate_vmid;dqm->asic_ops.init_sdma_vm(dqm, q, qpd);drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +1
-1@@ -410,7 +412,7 @@ static int create_queue_nocpsch(structdevice_queue_manager *dqm, deallocate_hqd(dqm, q); else if (q->properties.type == KFD_QUEUE_TYPE_SDMA || q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) - deallocate_sdma_queue(dqm, q); + deallocate_sdma_queue(dqm, qpd, q); deallocate_vmid: if (list_empty(&qpd->queues_list))deallocate_vmid(dqm, qpd, q);drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +2
-2@@ -475,9 +477,9 @@ static int destroy_queue_nocpsch_locked(struct device_queue_manager *dqm, if (q->properties.type == KFD_QUEUE_TYPE_COMPUTE) deallocate_hqd(dqm, q); else if (q->properties.type == KFD_QUEUE_TYPE_SDMA) - deallocate_sdma_queue(dqm, q); + deallocate_sdma_queue(dqm, qpd, q); else if (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) - deallocate_sdma_queue(dqm, q); + deallocate_sdma_queue(dqm, qpd, q); else { pr_debug("q->properties.type %d is invalid\n",
q->properties.type);drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c static void pre_reset(struct · line 1044
Quoted 4 lines
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +0
-0@@ -1039,42 +1041,93 @@ static void pre_reset(struct device_queue_manager *dqm) dqm_unlock(dqm); } +static uint64_t sdma_engine_mask(int engine, int num_engines)Looks more like the queue mask for an engine, the name doesn't make it clear.
Quoted 11 lines
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +11
-0+{ + uint64_t mask = 0; + + engine %= num_engines; + while (engine < (sizeof(uint64_t)*8)) { + mask |= 1ULL << engine; + engine += num_engines; + } + return mask; +} +drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c static void pre_reset(struct device_queue_manager *dqm) · line 1048
Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +4
-2static int allocate_sdma_queue(struct device_queue_manager *dqm, + struct qcm_process_device *qpd, struct queue *q) { - int bit; + uint64_t available_queue_bitmap; + unsigned int bit, engine, num_engines; + bool found_sdma = false; if (q->properties.type == KFD_QUEUE_TYPE_SDMA) { - if (dqm->sdma_bitmap == 0) {This is still a valid optimization and no need to loop through if nothing is available anyway. Valid for XGMI loop also.
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c static void pre_reset(struct · line 1066
Quoted 3 lines
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +2
-0+ num_engines = get_num_sdma_engines(dqm); + for_each_set_bit(engine, &(qpd->sdma_engine_bitmap), num_engines) {Probably a naive question -
Quoted 7 lines
Theoretically there are 8 queues per engine as per the mask code. In the below code, there is an assumption that a process will use at best number of queues=max num of sdma engines or xgmi engines simultaneously. Is that true always? For ex: there are 2 sdma engines and 4 queues per engine. Can't multiple threads of a process initiate multiple sdma transactions > number of sdma engines available? This code limits that, but I don't know if that is a possible case.
Quoted 6 lines
When you use multiple SDMA queues on the same engine, the work from the queues gets serialized. You can either let the SDMA engine serialize work from multiple queues, or let ROCr serialize work from multiple threads on the same SDMA queue. There is no obvious benefit to let the SDMA engine do it. But there is a drawback: Fewer processes get to use SDMA.
The fact that there exists multiple queues and there is no such assumption made in existing logic (prior to the patch) tells that there is indeed some advantage of making use of hardware queues. For ex:
switching to different ring buffer may not need context save/resubmission.Quoted 7 lines
Quoted 5 lines
Just like different processes, different threads of same process may take advantage of multiple queues. More interested to see the impact in cases where the focus is on single process ex: impact on running some benchmarks or in HPC where entire hardware is dedicated for a specific compute purpose.
Each engine has multiple user-mode queues because we expect to see multiple processes sharing the device at the same time. The
internal datasheets from the hardware team strongly suggest using one user-mode queue per engine per process.
Quoted 2 lines
With that in mind, the ROCr user-mode runtime attempts to create, at most, one queue per SDMA engine within each process. For
instance, ROCr creates one queue for
H->Dtransfers and one queue forD->Htransfers. We want these queues to be on different engines so that we can get full-duplex PCIe transfers. If both queues are on the same engine, the engine will serialize the transfers (because it can only work on one command at a time), and we will get half-duplex.Quoted 2 lines
The queue allocation algorithm that this patch replaces could accidentally yield two queues on the same engine. If Process A
allocates its first queue, it would receive
sdma0_queue0. Process B could then allocate its first queue, and getsdma1_queue0. Then Process A could allocate its second queue and getsdma0_queue1. This would yield undesirable and unexpected serialization betweenH->DandD->Htransfers in Process A. In addition, I believe the old algorithm would allow an uncooperative process to allocate all SDMA queues to itself. Looking at the git history, I believe the old allocator is basically the same as the initial KFD SDMA allocator built for Kaveri. I'm confident we've learned more about how multiple processes should share our GPUs since then. :)Quoted 2 lines
To your last point: even if we have different threads in the same process submitting transfer requests, the ROCr runtime within the
process will submit all like transfers (e.g. all
H->Dtransfers) into the same queue. Submitting those same commands into different queues on the same engine will not yield any clear performance benefit since the engine will serialize the commands. And it will likely yield some performance loss due to queue-switching overheads in the engine. I think this patch yields a more fair allocation mechanism (e.g., no process can starve out other processes by taking all the queues on all engines), and it also more closely match what user-space expects (e.g., different SDMA queues can run commands in parallel).Quoted 1 line
Thanks Joe for providing more info related to ROCr.
Quoted 10 lines
One other thing is - seems the process will keep waiting for the second queue (assuming first is still in use) to be alloted always on a separate engine. In the example cited above, do you expect a case where sdma1 queues are all full and Process A has to wait till a sdma1 queue is free even though there is a free queue available in sdma0 (by virtue of long/short jobs submitted earlier by other processes). Is that situation managed by ROCr? Or, as worst case allocate on sdma0 itself if nothing is available on other engines? Rather than going in any order, maybe another way is to order engines based on max free queues and give preference to the first engine not actively used by the process.
I think there are a few parts to this answer:
User-space has always been responsible for handling an allocation failure and falling back in some capacity, even before this patch. If the process receives a NAK and has no SDMA queues, it may need to fall back to a blit copy-kernel submitted to a compute queue. ROCr does this today.
Another example of when this allocator could NAK is (for example) if this process tries to allocate a 2nd PCIe queue, it already has a queue on SDMA0, and SDMA1 is full. User-space could also use a blit kernel to handle this, if it wants transfers to run in parallel (one direction in SDMA, one with a blit). Or it could decide to serialize all transfers commands into the single queue it already has.
A third situation would be if Process A initially requests a queue while SDMA0 is full, so its first queue is allocated on SDMA1. Later, another process ends and one of the SDMA0 queues is freed. If the Process A then requests a second SDMA queue, it will receive it on SDMA0. (Except on Aldebaran, due to the hardware quirk mentioned in the commit message. On Aldebaran, you would end up in the situation from the paragraph above, with one SDMA1 queue).
In none of these cases would we expect user-space to wait until a queue is free and keep trying to allocate. For all one process knows, such an allocation may never succeed. Imagine, for instance, 8 other processes that run perpetually and never relinquish their queue allocations. This SDMA allocations are not time-sliced on today's GPUs.
As for the "rather than going in any order" suggestion, the 3rd patch in this series attempts to address this concern without adding a lot of extra code complexity. I worry that ordering engines based on max free queues: a) would requiring more complexity than the current bitmap-based free-queue list, and b) doesn't allow us to easily handle the Aldebaran quirk mentioned above, which specifically deals with SDMA0.
Thanks again,
-Joe
Quoted 223 lines
Thanks,
LijoQuoted 219 lines
And thanks for the code suggestions up above, I will make those changes as requested.
Thanks!
-JoeQuoted 214 lines
Thanks,
LijoQuoted 211 lines
Regards,
FelixQuoted 207 lines
Thanks,
LijoQuoted 203 lines
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +73
-22+ /* Do not reuse SDMA0 for any subsequent SDMA queue + * requests on Aldebaran. If SDMA0's queues are all + * full, then this process should never use SDMA0 + * for any further requests + */ + if (dqm->dev->device_info->asic_family == CHIP_ALDEBARAN && + engine == 0) + qpd->sdma_engine_bitmap &= ~(1ULL << engine); + + available_queue_bitmap = sdma_engine_mask(engine, num_engines); + available_queue_bitmap &= dqm->sdma_bitmap; + + if (!available_queue_bitmap) + continue; + /* Take the selected engine off the list so we will not + * allocate two queues onto the same engine + */ + qpd->sdma_engine_bitmap &= ~(1ULL << engine); + found_sdma = true; + + bit = __ffs64(available_queue_bitmap); + dqm->sdma_bitmap &= ~(1ULL << bit); + q->sdma_id = bit; + q->properties.sdma_engine_id = q->sdma_id % num_engines; + q->properties.sdma_queue_id = q->sdma_id / num_engines; + break; + } + if (!found_sdma) { pr_err("No more SDMA queue to allocate\n"); return -ENOMEM; } - - bit = __ffs64(dqm->sdma_bitmap); - dqm->sdma_bitmap &= ~(1ULL << bit); - q->sdma_id = bit; - q->properties.sdma_engine_id = q->sdma_id % - get_num_sdma_engines(dqm); - q->properties.sdma_queue_id = q->sdma_id / - get_num_sdma_engines(dqm); } else if (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) { - if (dqm->xgmi_sdma_bitmap == 0) { + num_engines = get_num_xgmi_sdma_engines(dqm); + for_each_set_bit(engine, &(qpd->xgmi_sdma_engine_bitmap), num_engines) { + available_queue_bitmap = sdma_engine_mask(engine, num_engines); + available_queue_bitmap &= dqm->xgmi_sdma_bitmap; + + if (!available_queue_bitmap) + continue; + /* Take the selected engine off the list so we will not + * allocate two queues onto the same engine + */ + qpd->xgmi_sdma_engine_bitmap &= ~(1ULL << engine); + found_sdma = true; + + bit = __ffs64(available_queue_bitmap); + dqm->xgmi_sdma_bitmap &= ~(1ULL << bit); + q->sdma_id = bit; + /* sdma_engine_id is sdma id including + * both PCIe-optimized SDMAs and XGMI- + * optimized SDMAs. The calculation below + * assumes the first N engines are always + * PCIe-optimized ones + */ + q->properties.sdma_engine_id = get_num_sdma_engines(dqm) + + q->sdma_id % get_num_xgmi_sdma_engines(dqm); + q->properties.sdma_queue_id = q->sdma_id / + get_num_xgmi_sdma_engines(dqm); + break; + } + if (!found_sdma) { pr_err("No more XGMI SDMA queue to allocate\n"); return -ENOMEM; } - bit = __ffs64(dqm->xgmi_sdma_bitmap); - dqm->xgmi_sdma_bitmap &= ~(1ULL << bit); - q->sdma_id = bit; - /* sdma_engine_id is sdma id including - * both PCIe-optimized SDMAs and XGMI- - * optimized SDMAs. The calculation below - * assumes the first N engines are always - * PCIe-optimized ones - */ - q->properties.sdma_engine_id = get_num_sdma_engines(dqm) + - q->sdma_id % get_num_xgmi_sdma_engines(dqm); - q->properties.sdma_queue_id = q->sdma_id / - get_num_xgmi_sdma_engines(dqm); } pr_debug("SDMA engine id: %d\n", q->properties.sdma_engine_id);@@ -1084,16 +1137,32 @@ static int allocate_sdma_queue(structdevice_queue_manager *dqm, } static void deallocate_sdma_queue(struct device_queue_manager *dqm, + struct qcm_process_device *qpd, struct queue *q) { + uint32_t engine = q->properties.sdma_engine_id; + if (q->properties.type == KFD_QUEUE_TYPE_SDMA) { if (q->sdma_id >= get_num_sdma_queues(dqm)) return; dqm->sdma_bitmap |= (1ULL << q->sdma_id); + /* Don't give SDMA0 back to be reallocated on Aldebaran. + * It is only OK to use this engine from the first allocation + * within a process. + */ + if (!(dqm->dev->device_info->asic_family == CHIP_ALDEBARAN && + engine == 0)) + qpd->sdma_engine_bitmap |= (1ULL << engine); + } else if (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) { if (q->sdma_id >= get_num_xgmi_sdma_queues(dqm)) return; dqm->xgmi_sdma_bitmap |= (1ULL << q->sdma_id); + /* engine ID in the queue properties is the global engine ID. + * The XGMI engine bitmap ignores the PCIe-optimized engines. + */ + engine -= get_num_sdma_engines(dqm); + qpd->xgmi_sdma_engine_bitmap |= (1ULL << engine); } } @@ -1303,7 +1372,7 @@ static int create_queue_cpsch(struct
device_queue_manager*dqm, struct queue *q,
if (q->properties.type==KFD_QUEUE_TYPE_SDMA||q->properties.type==KFD_QUEUE_TYPE_SDMA_XGMI) {dqm_lock(dqm);
- retval =allocate_sdma_queue(dqm, q);
+ retval =allocate_sdma_queue(dqm, qpd, q);
dqm_unlock(dqm);
if (retval)
goto out;drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +1
-1@@ -1365,7 +1434,7 @@ static int create_queue_cpsch(struct device_queue_manager *dqm, struct queue *q, if (q->properties.type == KFD_QUEUE_TYPE_SDMA || q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) { dqm_lock(dqm); - deallocate_sdma_queue(dqm, q); + deallocate_sdma_queue(dqm, qpd, q); dqm_unlock(dqm); }out:
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +1
-1@@ -1536,7 +1605,7 @@ static int destroy_queue_cpsch(struct device_queue_manager *dqm, if ((q->properties.type == KFD_QUEUE_TYPE_SDMA) || (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)) { - deallocate_sdma_queue(dqm, q); + deallocate_sdma_queue(dqm, qpd, q); pdd->sdma_past_activity_counter += sdma_val; } @@ -1751,9 +1820,9 @@ static int process_termination_cpsch(struct
device_queue_manager*dqm,
/* Clear all user mode queues */list_for_each_entry(q, &qpd->queues_list, list){ if (q->properties.type==KFD_QUEUE_TYPE_SDMA)
-deallocate_sdma_queue(dqm, q);
+deallocate_sdma_queue(dqm, qpd, q);
else if (q->properties.type==KFD_QUEUE_TYPE_SDMA_XGMI)
-deallocate_sdma_queue(dqm, q);
+deallocate_sdma_queue(dqm, qpd, q);
if (q->properties.is_active) {decrement_queue_count(dqm, q->properties.type);drivers/gpu/drm/amd/amdkfd/kfd_priv.h +2
-0@@ -576,6 +576,8 @@ struct qcm_process_device { struct list_head priv_queue_list; unsigned int queue_count; + unsigned long sdma_engine_bitmap; + unsigned long xgmi_sdma_engine_bitmap; unsigned int vmid; bool is_debug; unsigned int evicted; /* eviction counter, 0=active */drivers/gpu/drm/amd/amdkfd/kfd_process.c +1
-0@@ -1422,6 +1422,7 @@ struct kfd_process_device *kfd_create_process_device_data(struct kfd_dev *dev, struct kfd_process *p) { struct kfd_process_device *pdd = NULL; + const struct kfd_device_info *dev_info = dev->dqm->dev->device_info; if (WARN_ON_ONCE(p->n_pdds >= MAX_GPU_INSTANCE))return NULL;
drivers/gpu/drm/amd/amdkfd/kfd_process.c +2
-0@@ -1446,6 +1447,8 @@ struct kfd_process_device *kfd_create_process_device_data(struct kfd_dev *dev, pdd->qpd.pqm = &p->pqm; pdd->qpd.evicted = 0; pdd->qpd.mapped_gws_queue = false; + pdd->qpd.sdma_engine_bitmap = BIT_ULL(dev_info->num_sdma_engines) - 1; + pdd->qpd.xgmi_sdma_engine_bitmap = BIT_ULL(dev_info->num_xgmi_sdma_engines) - 1;pdd->process = p; pdd->bound = PDD_UNBOUND; pdd->already_dequeued = false;
Quoted 205 lines · Joseph
Quoted 192 lines
-----Original Message-----
From: Lazar, Lijo <[email protected]>
Sent: Tuesday, August 24, 2021 2:24 AM
To: Greathouse, Joseph <[email protected]>; Kuehling, Felix <[email protected]>; amd- [email protected]
Subject: Re: [PATCH 1/3] drm/amdkfd: Allocate SDMA engines more fairlyQuoted 151 lines · Joseph
Quoted 151 lines
-----Original Message-----
From: Lazar, Lijo <[email protected]>
Sent: Monday, August 23, 2021 11:37 PM
To: Kuehling, Felix <[email protected]>; Greathouse, Joseph <[email protected]>; amd- [email protected]
Subject: Re: [PATCH 1/3] drm/amdkfd: Allocate SDMA engines more fairlyQuoted 131 lines · Felix Kuehling
Am 2021-08-23 um 3:08 a.m. schrieb Lazar, Lijo:
Quoted 122 lines · Joseph Greathouse
Quoted 80 lines
Give every process at most one queue from each SDMA engine. Previously, we allocated all SDMA engines and queues on a first- come-first-serve basis. This meant that it was possible for two processes racing on their allocation requests to each end up with two queues on the same SDMA engine. That could serialize the performance of commands to different queues.
This new mechanism allows each process at most a single queue on each SDMA engine. Processes will check for queue availability on SDMA engine 0, then 1, then 2, etc. and only allocate on that engine if there is an available queue slot. If there are no queue slots available (or if this process has already allocated a queue on this engine), it moves on and tries the next engine.
The Aldebaran chip has a small quirk that SDMA0 should only be used by the very first allocation from each process. It is OK to use any other SDMA engine at any time, but after the first SDMA allocation request from a process, the resulting engine must be from SDMA1 or above. This patch handles this case as well.
Signed-off-by Joseph Greathouse <[email protected]>--- .../drm/amd/amdkfd/kfd_device_queue_manager.c | 135 +++++++++++++----- drivers/gpu/drm/amd/amdkfd/kfd_priv.h | 2 + drivers/gpu/drm/amd/amdkfd/kfd_process.c | 3 + 3 files changed, 107 insertions(+), 33 deletions(-)drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +2
-0@@ -52,12 +52,14 @@ static int unmap_queues_cpsch(structdevice_queue_manager *dqm, static int map_queues_cpsch(struct device_queue_manager *dqm); static void deallocate_sdma_queue(struct device_queue_manager *dqm, + struct qcm_process_device *qpd, struct queue *q); static inline void deallocate_hqd(struct device_queue_manager *dqm, struct queue *q); static int allocate_hqd(struct device_queue_manager *dqm, structqueue *q); static int allocate_sdma_queue(struct device_queue_manager *dqm, + struct qcm_process_device *qpd, struct queue *q); static void kfd_process_hw_exception(struct work_struct *work); @@ -349,7 +351,7 @@ static int create_queue_nocpsch(struct
device_queue_manager*dqm,q->pipe, q->queue); } else if (q->properties.type == KFD_QUEUE_TYPE_SDMA || q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) { - retval = allocate_sdma_queue(dqm, q); + retval = allocate_sdma_queue(dqm, qpd, q); if (retval) goto deallocate_vmid; dqm->asic_ops.init_sdma_vm(dqm, q, qpd);drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +1
-1@@ -410,7 +412,7 @@ static int create_queue_nocpsch(structdevice_queue_manager *dqm, deallocate_hqd(dqm, q); else if (q->properties.type == KFD_QUEUE_TYPE_SDMA || q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) - deallocate_sdma_queue(dqm, q); + deallocate_sdma_queue(dqm, qpd, q); deallocate_vmid: if (list_empty(&qpd->queues_list))deallocate_vmid(dqm, qpd, q);drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +2
-2@@ -475,9 +477,9 @@ static int destroy_queue_nocpsch_locked(struct device_queue_manager *dqm, if (q->properties.type == KFD_QUEUE_TYPE_COMPUTE) deallocate_hqd(dqm, q); else if (q->properties.type == KFD_QUEUE_TYPE_SDMA) - deallocate_sdma_queue(dqm, q); + deallocate_sdma_queue(dqm, qpd, q); else if (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) - deallocate_sdma_queue(dqm, q); + deallocate_sdma_queue(dqm, qpd, q); else { pr_debug("q->properties.type %d is invalid\n",q->properties.type);drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c static void pre_reset(struct · line 1044
Quoted 4 lines
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +0
-0@@ -1039,42 +1041,93 @@ static void pre_reset(struct device_queue_manager *dqm) dqm_unlock(dqm); } +static uint64_t sdma_engine_mask(int engine, int num_engines)Looks more like the queue mask for an engine, the name doesn't make it clear.
Quoted 11 lines
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +11
-0+{ + uint64_t mask = 0; + + engine %= num_engines; + while (engine < (sizeof(uint64_t)*8)) { + mask |= 1ULL << engine; + engine += num_engines; + } + return mask; +} +drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c static void pre_reset(struct · line 1048
Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +4
-2static int allocate_sdma_queue(struct device_queue_manager *dqm, + struct qcm_process_device *qpd, struct queue *q) { - int bit; + uint64_t available_queue_bitmap; + unsigned int bit, engine, num_engines; + bool found_sdma = false; if (q->properties.type == KFD_QUEUE_TYPE_SDMA) { - if (dqm->sdma_bitmap == 0) {This is still a valid optimization and no need to loop through if nothing is available anyway. Valid for XGMI loop also.
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c static void pre_reset(struct · line 1066
Quoted 3 lines
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +2
-0+ num_engines = get_num_sdma_engines(dqm); + for_each_set_bit(engine, &(qpd->sdma_engine_bitmap), num_engines) {Probably a naive question -
Quoted 7 lines
Theoretically there are 8 queues per engine as per the mask code. In the below code, there is an assumption that a process will use at best number of queues=max num of sdma engines or xgmi engines simultaneously. Is that true always? For ex: there are 2 sdma engines and 4 queues per engine. Can't multiple threads of a process initiate multiple sdma transactions > number of sdma engines available? This code limits that, but I don't know if that is a possible case.
Quoted 6 lines
When you use multiple SDMA queues on the same engine, the work from the queues gets serialized. You can either let the SDMA engine serialize work from multiple queues, or let ROCr serialize work from multiple threads on the same SDMA queue. There is no obvious benefit to let the SDMA engine do it. But there is a drawback: Fewer processes get to use SDMA.
The fact that there exists multiple queues and there is no such assumption made in existing logic (prior to the patch) tells that there is indeed some advantage of making use of hardware queues. For ex:
switching to different ring buffer may not need context save/resubmission.Quoted 7 lines
Quoted 5 lines
Just like different processes, different threads of same process may take advantage of multiple queues. More interested to see the impact in cases where the focus is on single process ex: impact on running some benchmarks or in HPC where entire hardware is dedicated for a specific compute purpose.
Each engine has multiple user-mode queues because we expect to see multiple processes sharing the device at the same time. The
internal datasheets from the hardware team strongly suggest using one user-mode queue per engine per process.
Quoted 2 lines
With that in mind, the ROCr user-mode runtime attempts to create, at most, one queue per SDMA engine within each process. For
instance, ROCr creates one queue for
H->Dtransfers and one queue forD->Htransfers. We want these queues to be on different engines so that we can get full-duplex PCIe transfers. If both queues are on the same engine, the engine will serialize the transfers (because it can only work on one command at a time), and we will get half-duplex.Quoted 2 lines
The queue allocation algorithm that this patch replaces could accidentally yield two queues on the same engine. If Process A
allocates its first queue, it would receive
sdma0_queue0. Process B could then allocate its first queue, and getsdma1_queue0. Then Process A could allocate its second queue and getsdma0_queue1. This would yield undesirable and unexpected serialization betweenH->DandD->Htransfers in Process A. In addition, I believe the old algorithm would allow an uncooperative process to allocate all SDMA queues to itself. Looking at the git history, I believe the old allocator is basically the same as the initial KFD SDMA allocator built for Kaveri. I'm confident we've learned more about how multiple processes should share our GPUs since then. :)Quoted 2 lines
To your last point: even if we have different threads in the same process submitting transfer requests, the ROCr runtime within the
process will submit all like transfers (e.g. all
H->Dtransfers) into the same queue. Submitting those same commands into different queues on the same engine will not yield any clear performance benefit since the engine will serialize the commands. And it will likely yield some performance loss due to queue-switching overheads in the engine. I think this patch yields a more fair allocation mechanism (e.g., no process can starve out other processes by taking all the queues on all engines), and it also more closely match what user-space expects (e.g., different SDMA queues can run commands in parallel).Quoted 1 line
Thanks Joe for providing more info related to ROCr.
Quoted 10 lines
One other thing is - seems the process will keep waiting for the second queue (assuming first is still in use) to be alloted always on a separate engine. In the example cited above, do you expect a case where sdma1 queues are all full and Process A has to wait till a sdma1 queue is free even though there is a free queue available in sdma0 (by virtue of long/short jobs submitted earlier by other processes). Is that situation managed by ROCr? Or, as worst case allocate on sdma0 itself if nothing is available on other engines? Rather than going in any order, maybe another way is to order engines based on max free queues and give preference to the first engine not actively used by the process.
I think there are a few parts to this answer:
Quoted 8 lines
User-space has always been responsible for handling an allocation failure and falling back in some capacity, even before this patch. If the process receives a NAK and has no SDMA queues, it may need to fall back to a blit copy-kernel submitted to a compute queue. ROCr does this today.
Another example of when this allocator could NAK is (for example) if this process tries to allocate a 2nd PCIe queue, it already has a queue on SDMA0, and SDMA1 is full. User-space could also use a blit kernel to handle this, if it wants transfers to run in parallel (one direction in SDMA, one with a blit). Or it could decide to serialize all transfers commands into the single queue it already has.
A third situation would be if Process A initially requests a queue while SDMA0 is full, so its first queue is allocated on SDMA1. Later, another process ends and one of the SDMA0 queues is freed. If the Process A then requests a second SDMA queue, it will receive it on SDMA0. (Except on Aldebaran, due to the hardware quirk mentioned in the commit message. On Aldebaran, you would end up in the situation from the paragraph above, with one SDMA1 queue).
In none of these cases would we expect user-space to wait until a queue is free and keep trying to allocate. For all one process knows, such an allocation may never succeed. Imagine, for instance, 8 other processes that run perpetually and never relinquish their queue allocations. This SDMA allocations are not time-sliced on today's GPUs.
Thanks again for the details!
Quoted 2 lines
As for the "rather than going in any order" suggestion, the 3rd patch in this series attempts to address this concern without adding a lot of extra code complexity. I worry that ordering engines based on max free queues: a) would requiring more complexity than the current bitmap-based free-queue list, and b) doesn't allow us to easily handle the Aldebaran quirk mentioned above, which specifically deals with SDMA0.
It may not be so complicated once this bitmap is viewed differently. Sent a sample logic separately, please check.
Thanks,
Lijo
Quoted 226 lines
Thanks again,
-JoeQuoted 223 lines
Thanks,
LijoQuoted 219 lines
And thanks for the code suggestions up above, I will make those changes as requested.
Thanks!
-JoeQuoted 214 lines
Thanks,
LijoQuoted 211 lines
Regards,
FelixQuoted 207 lines
Thanks,
LijoQuoted 203 lines
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +73
-22+ /* Do not reuse SDMA0 for any subsequent SDMA queue + * requests on Aldebaran. If SDMA0's queues are all + * full, then this process should never use SDMA0 + * for any further requests + */ + if (dqm->dev->device_info->asic_family == CHIP_ALDEBARAN && + engine == 0) + qpd->sdma_engine_bitmap &= ~(1ULL << engine); + + available_queue_bitmap = sdma_engine_mask(engine, num_engines); + available_queue_bitmap &= dqm->sdma_bitmap; + + if (!available_queue_bitmap) + continue; + /* Take the selected engine off the list so we will not + * allocate two queues onto the same engine + */ + qpd->sdma_engine_bitmap &= ~(1ULL << engine); + found_sdma = true; + + bit = __ffs64(available_queue_bitmap); + dqm->sdma_bitmap &= ~(1ULL << bit); + q->sdma_id = bit; + q->properties.sdma_engine_id = q->sdma_id % num_engines; + q->properties.sdma_queue_id = q->sdma_id / num_engines; + break; + } + if (!found_sdma) { pr_err("No more SDMA queue to allocate\n"); return -ENOMEM; } - - bit = __ffs64(dqm->sdma_bitmap); - dqm->sdma_bitmap &= ~(1ULL << bit); - q->sdma_id = bit; - q->properties.sdma_engine_id = q->sdma_id % - get_num_sdma_engines(dqm); - q->properties.sdma_queue_id = q->sdma_id / - get_num_sdma_engines(dqm); } else if (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) { - if (dqm->xgmi_sdma_bitmap == 0) { + num_engines = get_num_xgmi_sdma_engines(dqm); + for_each_set_bit(engine, &(qpd->xgmi_sdma_engine_bitmap), num_engines) { + available_queue_bitmap = sdma_engine_mask(engine, num_engines); + available_queue_bitmap &= dqm->xgmi_sdma_bitmap; + + if (!available_queue_bitmap) + continue; + /* Take the selected engine off the list so we will not + * allocate two queues onto the same engine + */ + qpd->xgmi_sdma_engine_bitmap &= ~(1ULL << engine); + found_sdma = true; + + bit = __ffs64(available_queue_bitmap); + dqm->xgmi_sdma_bitmap &= ~(1ULL << bit); + q->sdma_id = bit; + /* sdma_engine_id is sdma id including + * both PCIe-optimized SDMAs and XGMI- + * optimized SDMAs. The calculation below + * assumes the first N engines are always + * PCIe-optimized ones + */ + q->properties.sdma_engine_id = get_num_sdma_engines(dqm) + + q->sdma_id % get_num_xgmi_sdma_engines(dqm); + q->properties.sdma_queue_id = q->sdma_id / + get_num_xgmi_sdma_engines(dqm); + break; + } + if (!found_sdma) { pr_err("No more XGMI SDMA queue to allocate\n"); return -ENOMEM; } - bit = __ffs64(dqm->xgmi_sdma_bitmap); - dqm->xgmi_sdma_bitmap &= ~(1ULL << bit); - q->sdma_id = bit; - /* sdma_engine_id is sdma id including - * both PCIe-optimized SDMAs and XGMI- - * optimized SDMAs. The calculation below - * assumes the first N engines are always - * PCIe-optimized ones - */ - q->properties.sdma_engine_id = get_num_sdma_engines(dqm) + - q->sdma_id % get_num_xgmi_sdma_engines(dqm); - q->properties.sdma_queue_id = q->sdma_id / - get_num_xgmi_sdma_engines(dqm); } pr_debug("SDMA engine id: %d\n", q->properties.sdma_engine_id);@@ -1084,16 +1137,32 @@ static int allocate_sdma_queue(structdevice_queue_manager *dqm, } static void deallocate_sdma_queue(struct device_queue_manager *dqm, + struct qcm_process_device *qpd, struct queue *q) { + uint32_t engine = q->properties.sdma_engine_id; + if (q->properties.type == KFD_QUEUE_TYPE_SDMA) { if (q->sdma_id >= get_num_sdma_queues(dqm)) return; dqm->sdma_bitmap |= (1ULL << q->sdma_id); + /* Don't give SDMA0 back to be reallocated on Aldebaran. + * It is only OK to use this engine from the first allocation + * within a process. + */ + if (!(dqm->dev->device_info->asic_family == CHIP_ALDEBARAN && + engine == 0)) + qpd->sdma_engine_bitmap |= (1ULL << engine); + } else if (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) { if (q->sdma_id >= get_num_xgmi_sdma_queues(dqm)) return; dqm->xgmi_sdma_bitmap |= (1ULL << q->sdma_id); + /* engine ID in the queue properties is the global engine ID. + * The XGMI engine bitmap ignores the PCIe-optimized engines. + */ + engine -= get_num_sdma_engines(dqm); + qpd->xgmi_sdma_engine_bitmap |= (1ULL << engine); } } @@ -1303,7 +1372,7 @@ static int create_queue_cpsch(struct
device_queue_manager*dqm, struct queue *q,
if (q->properties.type==KFD_QUEUE_TYPE_SDMA||q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) { dqm_lock(dqm); - retval = allocate_sdma_queue(dqm, q); + retval = allocate_sdma_queue(dqm, qpd, q); dqm_unlock(dqm); if (retval) goto out;drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +1
-1@@ -1365,7 +1434,7 @@ static int create_queue_cpsch(struct device_queue_manager *dqm, struct queue *q, if (q->properties.type == KFD_QUEUE_TYPE_SDMA || q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) { dqm_lock(dqm); - deallocate_sdma_queue(dqm, q); + deallocate_sdma_queue(dqm, qpd, q); dqm_unlock(dqm); }out:
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +1
-1@@ -1536,7 +1605,7 @@ static int destroy_queue_cpsch(struct device_queue_manager *dqm, if ((q->properties.type == KFD_QUEUE_TYPE_SDMA) || (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)) { - deallocate_sdma_queue(dqm, q); + deallocate_sdma_queue(dqm, qpd, q); pdd->sdma_past_activity_counter += sdma_val; } @@ -1751,9 +1820,9 @@ static int process_termination_cpsch(struct
device_queue_manager*dqm,
/* Clear all user mode queues */list_for_each_entry(q, &qpd->queues_list, list) { if (q->properties.type == KFD_QUEUE_TYPE_SDMA) - deallocate_sdma_queue(dqm, q); + deallocate_sdma_queue(dqm, qpd, q); else if (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) - deallocate_sdma_queue(dqm, q); + deallocate_sdma_queue(dqm, qpd, q); if (q->properties.is_active) { decrement_queue_count(dqm, q->properties.type);drivers/gpu/drm/amd/amdkfd/kfd_priv.h +2
-0@@ -576,6 +576,8 @@ struct qcm_process_device { struct list_head priv_queue_list; unsigned int queue_count; + unsigned long sdma_engine_bitmap; + unsigned long xgmi_sdma_engine_bitmap; unsigned int vmid; bool is_debug; unsigned int evicted; /* eviction counter, 0=active */drivers/gpu/drm/amd/amdkfd/kfd_process.c +1
-0@@ -1422,6 +1422,7 @@ struct kfd_process_device *kfd_create_process_device_data(struct kfd_dev *dev, struct kfd_process *p) { struct kfd_process_device *pdd = NULL; + const struct kfd_device_info *dev_info = dev->dqm->dev->device_info; if (WARN_ON_ONCE(p->n_pdds >= MAX_GPU_INSTANCE))return NULL;drivers/gpu/drm/amd/amdkfd/kfd_process.c +2
-0@@ -1446,6 +1447,8 @@ struct kfd_process_device *kfd_create_process_device_data(struct kfd_dev *dev, pdd->qpd.pqm = &p->pqm; pdd->qpd.evicted = 0; pdd->qpd.mapped_gws_queue = false; + pdd->qpd.sdma_engine_bitmap = BIT_ULL(dev_info->num_sdma_engines) - 1; + pdd->qpd.xgmi_sdma_engine_bitmap = BIT_ULL(dev_info->num_xgmi_sdma_engines) - 1;pdd->process = p; pdd->bound = PDD_UNBOUND; pdd->already_dequeued = false;