[PATCH v2] drm/amdkfd: Fix UBSAN shift-out-of-bounds warning

Sider, Graham Graham.Sider at amd.com
Wed Sep 28 14:06:46 UTC 2022


[Public]

Reviewed-by: Graham Sider <Graham.Sider at amd.com>

> -----Original Message-----
> From: Kuehling, Felix <Felix.Kuehling at amd.com>
> Sent: Monday, September 26, 2022 6:36 PM
> To: amd-gfx at lists.freedesktop.org
> Cc: Sider, Graham <Graham.Sider at amd.com>; Ellis Michael
> <ellis at ellismichael.com>
> Subject: [PATCH v2] drm/amdkfd: Fix UBSAN shift-out-of-bounds warning
> 
> 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 at ellismichael.com>
> Signed-off-by: Felix Kuehling <Felix.Kuehling at amd.com>
> ---
>  .../drm/amd/amdkfd/kfd_device_queue_manager.c | 45 +++++++++--------
> --
>  1 file changed, 21 insertions(+), 24 deletions(-)
> 
> diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c
> b/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c
> index 007a3db69df1..ecb4c3abc629 100644
> --- a/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c
> +++ b/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c
> @@ -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;
>  }
> 
> --
> 2.32.0


More information about the amd-gfx mailing list