aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYong Zhao <yong.zhao@amd.com>2017-09-20 18:10:21 -0400
committerOded Gabbay <oded.gabbay@gmail.com>2017-09-20 18:10:21 -0400
commitcb1d9967461cdf3b6aac6317c8d954a14f842571 (patch)
tree50bfb10a76331b899b285ba803f7c03c8090545c
parentb22666febf6fc67776d49782057fe4dd06f41552 (diff)
drm/amdkfd: Fix kernel-queue wrapping bugs
Avoid intermediate negative numbers when doing calculations with a mix of signed and unsigned variables where implicit conversions can lead to unexpected results. When kernel queue buffer wraps around to 0, we need to check that rptr won't be overwritten by the new packet. Signed-off-by: Yong Zhao <yong.zhao@amd.com> Signed-off-by: Felix Kuehling <Felix.Kuehling@amd.com> Signed-off-by: Oded Gabbay <oded.gabbay@gmail.com>
-rw-r--r--drivers/gpu/drm/amd/amdkfd/kfd_kernel_queue.c18
1 files changed, 15 insertions, 3 deletions
diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_kernel_queue.c b/drivers/gpu/drm/amd/amdkfd/kfd_kernel_queue.c
index a4ca1133482c..ed71ad40e8f7 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_kernel_queue.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_kernel_queue.c
@@ -210,6 +210,11 @@ static int acquire_packet_buffer(struct kernel_queue *kq,
210 uint32_t wptr, rptr; 210 uint32_t wptr, rptr;
211 unsigned int *queue_address; 211 unsigned int *queue_address;
212 212
213 /* When rptr == wptr, the buffer is empty.
214 * When rptr == wptr + 1, the buffer is full.
215 * It is always rptr that advances to the position of wptr, rather than
216 * the opposite. So we can only use up to queue_size_dwords - 1 dwords.
217 */
213 rptr = *kq->rptr_kernel; 218 rptr = *kq->rptr_kernel;
214 wptr = *kq->wptr_kernel; 219 wptr = *kq->wptr_kernel;
215 queue_address = (unsigned int *)kq->pq_kernel_addr; 220 queue_address = (unsigned int *)kq->pq_kernel_addr;
@@ -219,11 +224,10 @@ static int acquire_packet_buffer(struct kernel_queue *kq,
219 pr_debug("wptr: %d\n", wptr); 224 pr_debug("wptr: %d\n", wptr);
220 pr_debug("queue_address 0x%p\n", queue_address); 225 pr_debug("queue_address 0x%p\n", queue_address);
221 226
222 available_size = (rptr - 1 - wptr + queue_size_dwords) % 227 available_size = (rptr + queue_size_dwords - 1 - wptr) %
223 queue_size_dwords; 228 queue_size_dwords;
224 229
225 if (packet_size_in_dwords >= queue_size_dwords || 230 if (packet_size_in_dwords > available_size) {
226 packet_size_in_dwords >= available_size) {
227 /* 231 /*
228 * make sure calling functions know 232 * make sure calling functions know
229 * acquire_packet_buffer() failed 233 * acquire_packet_buffer() failed
@@ -233,6 +237,14 @@ static int acquire_packet_buffer(struct kernel_queue *kq,
233 } 237 }
234 238
235 if (wptr + packet_size_in_dwords >= queue_size_dwords) { 239 if (wptr + packet_size_in_dwords >= queue_size_dwords) {
240 /* make sure after rolling back to position 0, there is
241 * still enough space.
242 */
243 if (packet_size_in_dwords >= rptr) {
244 *buffer_ptr = NULL;
245 return -ENOMEM;
246 }
247 /* fill nops, roll back and start at position 0 */
236 while (wptr > 0) { 248 while (wptr > 0) {
237 queue_address[wptr] = kq->nop_packet; 249 queue_address[wptr] = kq->nop_packet;
238 wptr = (wptr + 1) % queue_size_dwords; 250 wptr = (wptr + 1) % queue_size_dwords;