drm/amdkfd: Fix UBSAN shift-out-of-bounds warning
authorFelix Kuehling <Felix.Kuehling@amd.com>
Wed, 21 Sep 2022 21:45:59 +0000 (17:45 -0400)
committerAlex Deucher <alexander.deucher@amd.com>
Fri, 30 Sep 2022 15:21:25 +0000 (11:21 -0400)
This was fixed in initialize_cpsch before, but not in initialize_nocpsch.
Factor sdma bitmap initialization into a helper function to apply the
correct implementation in both cases without duplicating it.

v2: Added a range check

Reported-by: Ellis Michael <ellis@ellismichael.com>
Signed-off-by: Felix Kuehling <Felix.Kuehling@amd.com>
Reviewed-by: Graham Sider <Graham.Sider@amd.com>
Signed-off-by: Alex Deucher <alexander.deucher@amd.com>
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c

index 007a3db..ecb4c3a 100644 (file)
@@ -1242,6 +1242,24 @@ static void init_interrupts(struct device_queue_manager *dqm)
                        dqm->dev->kfd2kgd->init_interrupts(dqm->dev->adev, i);
 }
 
+static void init_sdma_bitmaps(struct device_queue_manager *dqm)
+{
+       unsigned int num_sdma_queues =
+               min_t(unsigned int, sizeof(dqm->sdma_bitmap)*8,
+                     get_num_sdma_queues(dqm));
+       unsigned int num_xgmi_sdma_queues =
+               min_t(unsigned int, sizeof(dqm->xgmi_sdma_bitmap)*8,
+                     get_num_xgmi_sdma_queues(dqm));
+
+       if (num_sdma_queues)
+               dqm->sdma_bitmap = GENMASK_ULL(num_sdma_queues-1, 0);
+       if (num_xgmi_sdma_queues)
+               dqm->xgmi_sdma_bitmap = GENMASK_ULL(num_xgmi_sdma_queues-1, 0);
+
+       dqm->sdma_bitmap &= ~get_reserved_sdma_queues_bitmap(dqm);
+       pr_info("sdma_bitmap: %llx\n", dqm->sdma_bitmap);
+}
+
 static int initialize_nocpsch(struct device_queue_manager *dqm)
 {
        int pipe, queue;
@@ -1270,11 +1288,7 @@ static int initialize_nocpsch(struct device_queue_manager *dqm)
 
        memset(dqm->vmid_pasid, 0, sizeof(dqm->vmid_pasid));
 
-       dqm->sdma_bitmap = ~0ULL >> (64 - get_num_sdma_queues(dqm));
-       dqm->sdma_bitmap &= ~(get_reserved_sdma_queues_bitmap(dqm));
-       pr_info("sdma_bitmap: %llx\n", dqm->sdma_bitmap);
-
-       dqm->xgmi_sdma_bitmap = ~0ULL >> (64 - get_num_xgmi_sdma_queues(dqm));
+       init_sdma_bitmaps(dqm);
 
        return 0;
 }
@@ -1452,9 +1466,6 @@ static int set_sched_resources(struct device_queue_manager *dqm)
 
 static int initialize_cpsch(struct device_queue_manager *dqm)
 {
-       uint64_t num_sdma_queues;
-       uint64_t num_xgmi_sdma_queues;
-
        pr_debug("num of pipes: %d\n", get_pipes_per_mec(dqm));
 
        mutex_init(&dqm->lock_hidden);
@@ -1463,24 +1474,10 @@ static int initialize_cpsch(struct device_queue_manager *dqm)
        dqm->active_cp_queue_count = 0;
        dqm->gws_queue_count = 0;
        dqm->active_runlist = false;
-
-       num_sdma_queues = get_num_sdma_queues(dqm);
-       if (num_sdma_queues >= BITS_PER_TYPE(dqm->sdma_bitmap))
-               dqm->sdma_bitmap = ULLONG_MAX;
-       else
-               dqm->sdma_bitmap = (BIT_ULL(num_sdma_queues) - 1);
-
-       dqm->sdma_bitmap &= ~(get_reserved_sdma_queues_bitmap(dqm));
-       pr_info("sdma_bitmap: %llx\n", dqm->sdma_bitmap);
-
-       num_xgmi_sdma_queues = get_num_xgmi_sdma_queues(dqm);
-       if (num_xgmi_sdma_queues >= BITS_PER_TYPE(dqm->xgmi_sdma_bitmap))
-               dqm->xgmi_sdma_bitmap = ULLONG_MAX;
-       else
-               dqm->xgmi_sdma_bitmap = (BIT_ULL(num_xgmi_sdma_queues) - 1);
-
        INIT_WORK(&dqm->hw_exception_work, kfd_process_hw_exception);
 
+       init_sdma_bitmaps(dqm);
+
        return 0;
 }