Skip to content

Commit 02f7f99

Browse files
committed
librocdxg: Increase AQL frame size calculation
to prevent PM4 command buffer overflow Signed-off-by: Flora Cui <flora.cui@amd.com> Reviewed-by: Longlong Yao <Longlong.Yao@amd.com> Part-of: <http://10.67.69.192/wsl/rocr-runtime/-/merge_requests/113>
1 parent d035d57 commit 02f7f99

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/wddm/device.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,14 +409,20 @@ void WDDMDevice::InitCmdbufInfo(void) {
409409
cmdbuf_aql_frame_size_ = 2 * sizeof(gfx10::AcquireMemTemplate);
410410
}
411411

412-
if (device_info_.major >= 11)
412+
if (device_info_.major >= 11) {
413413
cmdbuf_aql_frame_size_ += sizeof(SetScratchTemplate);
414+
cmdbuf_aql_frame_size_ += sizeof(DispatchProgramResourceRegs); // BuildComputeShaderParams
415+
}
414416

415417
cmdbuf_aql_frame_size_ +=
416418
sizeof(PM4MEC_COPY_DATA) * 2 +
417419
sizeof(BarrierTemplate) * 2 +
418420
sizeof(DispatchTemplate) +
419421
sizeof(AtomicTemplate) * 2;
422+
423+
// Add safety margin to account for alignment and future additions
424+
cmdbuf_aql_frame_size_ += 128;
425+
420426
cmdbuf_aql_frame_size_ = AlignUp(cmdbuf_aql_frame_size_, 0x10);
421427

422428
cmdbuf_size_ = AlignUp(cmdbuf_aql_frame_num_ * cmdbuf_aql_frame_size_, 0x1000);

src/wddm/queue.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,12 @@ ComputeQueue::KernelDispatchAqlToPm4(char *cpu, hsa_kernel_dispatch_packet_t *pa
723723
else
724724
i += cmd_util.BuildWriteData64Command(cpu + i, (uint64_t *)ring_rptr, cmdbuf_aql_frame_write_index + 1);
725725

726+
// Check if we exceeded the frame size
727+
if ((i - ib_size) > cmdbuf_aql_frame_size) {
728+
pr_err("PM4 command buffer overflow in KernelDispatch: used %d bytes, limit %d bytes\n", i - ib_size, cmdbuf_aql_frame_size);
729+
return HSA_STATUS_ERROR_OUT_OF_RESOURCES;
730+
}
731+
726732
ib_size = i;
727733
cmdbuf_aql_frame_write_index++;
728734
packet->header = HSA_PACKET_TYPE_INVALID;
@@ -811,6 +817,12 @@ ComputeQueue::BarrierGenericAqlToPm4(char *cpu, hsa_barrier_and_packet_t *packet
811817
else
812818
i += cmd_util.BuildWriteData64Command(cpu + i, (uint64_t *)ring_rptr, cmdbuf_aql_frame_write_index + 1);
813819

820+
// Check if we exceeded the frame size
821+
if ((i - ib_size) > cmdbuf_aql_frame_size) {
822+
pr_err("PM4 command buffer overflow in BarrierGeneric: used %d bytes, limit %d bytes\n", i - ib_size, cmdbuf_aql_frame_size);
823+
return HSA_STATUS_ERROR_OUT_OF_RESOURCES;
824+
}
825+
814826
ib_size = i;
815827
cmdbuf_aql_frame_write_index++;
816828
packet->header = HSA_PACKET_TYPE_INVALID;
@@ -881,6 +893,12 @@ hsa_status_t ComputeQueue::VendorSpecificAqlToPm4(char *cpu, amd_aql_pm4_ib *pac
881893
else
882894
i += cmd_util.BuildWriteData64Command(cpu + i, (uint64_t *)ring_rptr, cmdbuf_aql_frame_write_index + 1);
883895

896+
// Check if we exceeded the frame size
897+
if ((i - ib_size) > cmdbuf_aql_frame_size) {
898+
pr_err("PM4 command buffer overflow in VendorSpecific: used %d bytes, limit %d bytes\n", i - ib_size, cmdbuf_aql_frame_size);
899+
return HSA_STATUS_ERROR_OUT_OF_RESOURCES;
900+
}
901+
884902
ib_size = i;
885903
cmdbuf_aql_frame_write_index++;
886904
packet->header = HSA_PACKET_TYPE_INVALID;

0 commit comments

Comments
 (0)