All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] drm/amd/display: Validate max_fclk_mhz in DML2 policy builder
@ 2026-06-06 21:25 Danila Chernetsov
  2026-06-06 21:50 ` sashiko-bot
  0 siblings, 1 reply; 3+ messages in thread
From: Danila Chernetsov @ 2026-06-06 21:25 UTC (permalink / raw)
  To: Austin Zheng
  Cc: Danila Chernetsov, Jun Lei, Harry Wentland, Leo Li, Alex Deucher,
	Christian König, David Airlie, Simona Vetter, amd-gfx,
	dri-devel, linux-kernel, lvc-project

dml2_policy_build_synthetic_soc_states() validates that several
derived maximum clock values are non-zero before constructing
synthetic SOC states.

However, max_fclk_mhz is not included in the validation despite
later logic assuming the presence of at least one valid FCLK level.
If all FCLK entries are zero, num_fclk_dpms remains zero, causing
an integer underflow (0 - 1) and out-of-bounds array access.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes: 7966f319c66d ("drm/amd/display: Introduce DML2")
Signed-off-by: Danila Chernetsov <listdansp@mail.ru>
---
 drivers/gpu/drm/amd/display/dc/dml2_0/dml2_policy.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/gpu/drm/amd/display/dc/dml2_0/dml2_policy.c b/drivers/gpu/drm/amd/display/dc/dml2_0/dml2_policy.c
index ef693f608d59..70f418ee5dd2 100644
--- a/drivers/gpu/drm/amd/display/dc/dml2_0/dml2_policy.c
+++ b/drivers/gpu/drm/amd/display/dc/dml2_0/dml2_policy.c
@@ -157,7 +157,8 @@ int dml2_policy_build_synthetic_soc_states(struct dml2_policy_build_synthetic_so
 			num_uclk_dpms++;
 	}
 
-	if (!max_dcfclk_mhz || !max_dispclk_mhz || !max_dppclk_mhz || !max_phyclk_mhz || !max_dtbclk_mhz)
+	if (!max_dcfclk_mhz || !max_dispclk_mhz || !max_dppclk_mhz || !max_phyclk_mhz ||
+		!max_dtbclk_mhz || !max_fclk_mhz)
 		return -1;
 
 	p->out_states->num_states = 0;
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] drm/amd/display: Validate max_fclk_mhz in DML2 policy builder
  2026-06-06 21:25 [PATCH] drm/amd/display: Validate max_fclk_mhz in DML2 policy builder Danila Chernetsov
@ 2026-06-06 21:50 ` sashiko-bot
  2026-06-06 22:06   ` listdansp
  0 siblings, 1 reply; 3+ messages in thread
From: sashiko-bot @ 2026-06-06 21:50 UTC (permalink / raw)
  To: Danila Chernetsov; +Cc: dri-devel

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] Missing bounds check in `insert_entry_into_table_sorted` allows array out-of-bounds write.
--

commit 803110810943a93ffdbd8a40adba9348d5b58b1b
Author: Danila Chernetsov <listdansp@mail.ru>

drm/amd/display: Validate max_fclk_mhz in DML2 policy builder

This commit adds a validation check for max_fclk_mhz to prevent an integer
underflow and out-of-bounds array access when all FCLK entries are zero.

> diff --git a/drivers/gpu/drm/amd/display/dc/dml2_0/dml2_policy.c b/drivers/gpu/drm/amd/display/dc/dml2_0/dml2_policy.c
> index ab2964811c5bc..938faa6272c2c 100644
> --- a/drivers/gpu/drm/amd/display/dc/dml2_0/dml2_policy.c
> +++ b/drivers/gpu/drm/amd/display/dc/dml2_0/dml2_policy.c
> @@ -157,7 +157,8 @@ int dml2_policy_build_synthetic_soc_states(struct dml2_policy_build_synthetic_so
>  			num_uclk_dpms++;
>  	}
>  
> -	if (!max_dcfclk_mhz || !max_dispclk_mhz || !max_dppclk_mhz || !max_phyclk_mhz || !max_dtbclk_mhz)
> +	if (!max_dcfclk_mhz || !max_dispclk_mhz || !max_dppclk_mhz || !max_phyclk_mhz ||
> +		!max_dtbclk_mhz || !max_fclk_mhz)
>  		return -1;
>  
>  	p->out_states->num_states = 0;

[Severity: High]
This isn't a bug introduced by this patch, but can the subsequent state
insertions overflow the out_states array?

Looking at dml2_policy_build_synthetic_soc_states(), it calls
insert_entry_into_table_sorted() multiple times:

    // Insert all the DCFCLK STAs first
    for (i = 0; i < p->num_dcfclk_stas; i++) {
        ...
        insert_entry_into_table_sorted(p->in_bbox, p->out_states, &s->entry);
    }

    // Insert the UCLK DPMS
    for (i = 0; i < num_uclk_dpms; i++) {
        ...
        insert_entry_into_table_sorted(p->in_bbox, p->out_states, &s->entry);
    }

    // Insert FCLK DPMs (if present)
    if (num_fclk_dpms > 2) {
        for (i = 0; i < num_fclk_dpms; i++) {
            ...
            insert_entry_into_table_sorted(p->in_bbox, p->out_states, &s->entry);
        }
    }

If num_uclk_dpms and num_fclk_dpms can each be derived from the 30-element
input array, the combined insertions could exceed __DML_MAX_STATE_ARRAY_SIZE__.

Inside insert_entry_into_table_sorted(), elements are shifted without a bounds
check:

    for (i = table->num_states; i > index; i--) {
        table->state_array[i] = table->state_array[i - 1];
    }
    ...
    table->num_states++;

Could this write past the end of table->state_array if the total number of
inserted states across these loops exceeds 30?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260606212503.140638-1-listdansp@mail.ru?part=1

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] drm/amd/display: Validate max_fclk_mhz in DML2 policy builder
  2026-06-06 21:50 ` sashiko-bot
@ 2026-06-06 22:06   ` listdansp
  0 siblings, 0 replies; 3+ messages in thread
From: listdansp @ 2026-06-06 22:06 UTC (permalink / raw)
  To: sashiko-reviews; +Cc: dri-devel

On 07/06/2026 00:50, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
> 
> Pre-existing issues:
> - [High] Missing bounds check in `insert_entry_into_table_sorted` allows array out-of-bounds write.
> --
> 
> commit 803110810943a93ffdbd8a40adba9348d5b58b1b
> Author: Danila Chernetsov <listdansp@mail.ru>
> 
> drm/amd/display: Validate max_fclk_mhz in DML2 policy builder
> 
> This commit adds a validation check for max_fclk_mhz to prevent an integer
> underflow and out-of-bounds array access when all FCLK entries are zero.
> 
>> diff --git a/drivers/gpu/drm/amd/display/dc/dml2_0/dml2_policy.c b/drivers/gpu/drm/amd/display/dc/dml2_0/dml2_policy.c
>> index ab2964811c5bc..938faa6272c2c 100644
>> --- a/drivers/gpu/drm/amd/display/dc/dml2_0/dml2_policy.c
>> +++ b/drivers/gpu/drm/amd/display/dc/dml2_0/dml2_policy.c
>> @@ -157,7 +157,8 @@ int dml2_policy_build_synthetic_soc_states(struct dml2_policy_build_synthetic_so
>>   			num_uclk_dpms++;
>>   	}
>>   
>> -	if (!max_dcfclk_mhz || !max_dispclk_mhz || !max_dppclk_mhz || !max_phyclk_mhz || !max_dtbclk_mhz)
>> +	if (!max_dcfclk_mhz || !max_dispclk_mhz || !max_dppclk_mhz || !max_phyclk_mhz ||
>> +		!max_dtbclk_mhz || !max_fclk_mhz)
>>   		return -1;
>>   
>>   	p->out_states->num_states = 0;
> 
> [Severity: High]
> This isn't a bug introduced by this patch, but can the subsequent state
> insertions overflow the out_states array?
> 
> Looking at dml2_policy_build_synthetic_soc_states(), it calls
> insert_entry_into_table_sorted() multiple times:
> 
>      // Insert all the DCFCLK STAs first
>      for (i = 0; i < p->num_dcfclk_stas; i++) {
>          ...
>          insert_entry_into_table_sorted(p->in_bbox, p->out_states, &s->entry);
>      }
> 
>      // Insert the UCLK DPMS
>      for (i = 0; i < num_uclk_dpms; i++) {
>          ...
>          insert_entry_into_table_sorted(p->in_bbox, p->out_states, &s->entry);
>      }
> 
>      // Insert FCLK DPMs (if present)
>      if (num_fclk_dpms > 2) {
>          for (i = 0; i < num_fclk_dpms; i++) {
>              ...
>              insert_entry_into_table_sorted(p->in_bbox, p->out_states, &s->entry);
>          }
>      }
> 
> If num_uclk_dpms and num_fclk_dpms can each be derived from the 30-element
> input array, the combined insertions could exceed __DML_MAX_STATE_ARRAY_SIZE__.
> 
> Inside insert_entry_into_table_sorted(), elements are shifted without a bounds
> check:
> 
>      for (i = table->num_states; i > index; i--) {
>          table->state_array[i] = table->state_array[i - 1];
>      }
>      ...
>      table->num_states++;
> 
> Could this write past the end of table->state_array if the total number of
> inserted states across these loops exceeds 30?
> 

Thanks for the review. Both reported issues are pre-existing in the codebase and are completely unrelated to my changes. 
This patch does not introduce, touch, or modify this logic. I request the maintainers to review my patch as is.

Best regards,
Danila Chernetsov

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-06-07 15:54 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-06-06 21:25 [PATCH] drm/amd/display: Validate max_fclk_mhz in DML2 policy builder Danila Chernetsov
2026-06-06 21:50 ` sashiko-bot
2026-06-06 22:06   ` listdansp

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.