public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* [PATCH] ArmPkg/ArmScmiDxe: Fix the calculation of RequiredArraySize in ClockDescribeRates()
@ 2023-02-02 14:30 Sudeep Holla
  2023-02-03 11:10 ` PierreGondois
  0 siblings, 1 reply; 4+ messages in thread
From: Sudeep Holla @ 2023-02-02 14:30 UTC (permalink / raw)
  To: devel
  Cc: Sudeep Holla, Leif Lindholm, Ard Biesheuvel, Pierre Gondois,
	Girish Pathak, Jeff Brasen, Sami Mujawar

As per the SCMI specification, section CLOCK_DESCRIBE_RATES mentions that
the value of num_rates_flags[11:0] in the response must be 3 if the return
format is the triplet. Due to the buggy firmware, this was not noticed for
long time. The firmware is now fixed resulting in ClockDescribeRates() to
fail with "Buffer Too Small" error as the RequiredArraySize gets miscalculated
as 72 instead of 24.

Fix the issue by reusing the logic for both the return format which must
work if num_rates_flags has correct value as expected from the specification.

Cc: Pierre Gondois <Pierre.Gondois@arm.com>
Cc: Girish Pathak <girish.pathak@arm.com>
Cc: Jeff Brasen <jbrasen@nvidia.com>
Reported-by: Sami Mujawar <sami.mujawar@arm.com>
Signed-off-by: Sudeep Holla <sudeep.holla@arm.com>
---
 ArmPkg/Drivers/ArmScmiDxe/ScmiClockProtocol.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)

Hi All,

Sorry for the resend, I hadn't subscribed before so resending after the
subscription.

Regards,
Sudeep

diff --git a/ArmPkg/Drivers/ArmScmiDxe/ScmiClockProtocol.c b/ArmPkg/Drivers/ArmScmiDxe/ScmiClockProtocol.c
index 12a7e6df5d43..1412717bc227 100644
--- a/ArmPkg/Drivers/ArmScmiDxe/ScmiClockProtocol.c
+++ b/ArmPkg/Drivers/ArmScmiDxe/ScmiClockProtocol.c
@@ -236,12 +236,7 @@ ClockDescribeRates (
       *TotalRates = NUM_RATES (DescribeRates->NumRatesFlags)
                     + NUM_REMAIN_RATES (DescribeRates->NumRatesFlags);
 
-      if (*Format == ScmiClockRateFormatDiscrete) {
-        RequiredArraySize = (*TotalRates) * sizeof (UINT64);
-      } else {
-        // We need to return triplet of 64 bit value for each rate
-        RequiredArraySize = (*TotalRates) * 3 * sizeof (UINT64);
-      }
+      RequiredArraySize = (*TotalRates) * sizeof (UINT64);
 
       if (RequiredArraySize > (*RateArraySize)) {
         *RateArraySize = RequiredArraySize;
-- 
2.39.1


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

* Re: [PATCH] ArmPkg/ArmScmiDxe: Fix the calculation of RequiredArraySize in ClockDescribeRates()
  2023-02-02 14:30 [PATCH] ArmPkg/ArmScmiDxe: Fix the calculation of RequiredArraySize in ClockDescribeRates() Sudeep Holla
@ 2023-02-03 11:10 ` PierreGondois
  2023-02-03 11:20   ` Sudeep Holla
  0 siblings, 1 reply; 4+ messages in thread
From: PierreGondois @ 2023-02-03 11:10 UTC (permalink / raw)
  To: Sudeep Holla, devel
  Cc: Leif Lindholm, Ard Biesheuvel, Girish Pathak, Jeff Brasen,
	Sami Mujawar

Hello Sudeep,

On 2/2/23 15:30, Sudeep Holla wrote:
> As per the SCMI specification, section CLOCK_DESCRIBE_RATES mentions that
> the value of num_rates_flags[11:0] in the response must be 3 if the return
> format is the triplet. Due to the buggy firmware, this was not noticed for
> long time. The firmware is now fixed resulting in ClockDescribeRates() to
> fail with "Buffer Too Small" error as the RequiredArraySize gets miscalculated
> as 72 instead of 24.
> 
> Fix the issue by reusing the logic for both the return format which must
> work if num_rates_flags has correct value as expected from the specification.
> 
> Cc: Pierre Gondois <Pierre.Gondois@arm.com>
> Cc: Girish Pathak <girish.pathak@arm.com>
> Cc: Jeff Brasen <jbrasen@nvidia.com>
> Reported-by: Sami Mujawar <sami.mujawar@arm.com>
> Signed-off-by: Sudeep Holla <sudeep.holla@arm.com>
> ---
>   ArmPkg/Drivers/ArmScmiDxe/ScmiClockProtocol.c | 7 +------
>   1 file changed, 1 insertion(+), 6 deletions(-)
> 
> Hi All,
> 
> Sorry for the resend, I hadn't subscribed before so resending after the
> subscription.
> 
> Regards,
> Sudeep
> 
> diff --git a/ArmPkg/Drivers/ArmScmiDxe/ScmiClockProtocol.c b/ArmPkg/Drivers/ArmScmiDxe/ScmiClockProtocol.c
> index 12a7e6df5d43..1412717bc227 100644
> --- a/ArmPkg/Drivers/ArmScmiDxe/ScmiClockProtocol.c
> +++ b/ArmPkg/Drivers/ArmScmiDxe/ScmiClockProtocol.c
> @@ -236,12 +236,7 @@ ClockDescribeRates (
>         *TotalRates = NUM_RATES (DescribeRates->NumRatesFlags)
>                       + NUM_REMAIN_RATES (DescribeRates->NumRatesFlags);
>   
> -      if (*Format == ScmiClockRateFormatDiscrete) {
> -        RequiredArraySize = (*TotalRates) * sizeof (UINT64);
> -      } else {
> -        // We need to return triplet of 64 bit value for each rate
> -        RequiredArraySize = (*TotalRates) * 3 * sizeof (UINT64);
> -      }
> +      RequiredArraySize = (*TotalRates) * sizeof (UINT64);
>   
>         if (RequiredArraySize > (*RateArraySize)) {
>           *RateArraySize = RequiredArraySize;

I think the following also needs to be modified:
"""
for (RateNo = 0; RateNo < NUM_RATES (DescribeRates->NumRatesFlags); RateNo++) {
   // Linear clock rates from minimum to maximum in steps
   // Minimum clock rate.
   Rate                                    = &DescribeRates->Rates[RateOffset++];
   RateArray[RateIndex].ContinuousRate.Min =
	ConvertTo64Bit (Rate->Low, Rate->High);

   Rate = &DescribeRates->Rates[RateOffset++];
   // Maximum clock rate.
   RateArray[RateIndex].ContinuousRate.Max =
	ConvertTo64Bit (Rate->Low, Rate->High);

   Rate = &DescribeRates->Rates[RateOffset++];
   // Step.
   RateArray[RateIndex++].ContinuousRate.Step =
	ConvertTo64Bit (Rate->Low, Rate->High);
}
"""

If the clock format is continuous, then per spec:
   NUM_RATES (DescribeRates->NumRatesFlags) == 3
However 3 rates are read per loop here. So I think the
loop needs to be removed aswell.

Without this additional change, I get:

"""
Synchronous Exception at 0xA2D0E6EBD20F8604

Synchronous Exception at 0xA2D0E6EBD20F8604
PC 0xA2D0E6EBD20F8604

Recursive exception occurred while dumping the CPU state
"""

With the above loop removed:
Reviewed-by: Pierre Gondois <pierre.gondois@arm.com>
Tested-by: Pierre Gondois <pierre.gondois@arm.com>

Regards,
Pierre

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

* Re: [PATCH] ArmPkg/ArmScmiDxe: Fix the calculation of RequiredArraySize in ClockDescribeRates()
  2023-02-03 11:10 ` PierreGondois
@ 2023-02-03 11:20   ` Sudeep Holla
  2023-02-03 12:27     ` [edk2-devel] " Ard Biesheuvel
  0 siblings, 1 reply; 4+ messages in thread
From: Sudeep Holla @ 2023-02-03 11:20 UTC (permalink / raw)
  To: Pierre Gondois
  Cc: devel, Leif Lindholm, Ard Biesheuvel, Girish Pathak, Jeff Brasen,
	Sami Mujawar

On Fri, Feb 03, 2023 at 12:10:38PM +0100, Pierre Gondois wrote:
> Hello Sudeep,
> 
> On 2/2/23 15:30, Sudeep Holla wrote:
> > As per the SCMI specification, section CLOCK_DESCRIBE_RATES mentions that
> > the value of num_rates_flags[11:0] in the response must be 3 if the return
> > format is the triplet. Due to the buggy firmware, this was not noticed for
> > long time. The firmware is now fixed resulting in ClockDescribeRates() to
> > fail with "Buffer Too Small" error as the RequiredArraySize gets miscalculated
> > as 72 instead of 24.
> > 
> > Fix the issue by reusing the logic for both the return format which must
> > work if num_rates_flags has correct value as expected from the specification.
> > 
> > Cc: Pierre Gondois <Pierre.Gondois@arm.com>
> > Cc: Girish Pathak <girish.pathak@arm.com>
> > Cc: Jeff Brasen <jbrasen@nvidia.com>
> > Reported-by: Sami Mujawar <sami.mujawar@arm.com>
> > Signed-off-by: Sudeep Holla <sudeep.holla@arm.com>
> > ---
> >   ArmPkg/Drivers/ArmScmiDxe/ScmiClockProtocol.c | 7 +------
> >   1 file changed, 1 insertion(+), 6 deletions(-)
> > 
> > Hi All,
> > 
> > Sorry for the resend, I hadn't subscribed before so resending after the
> > subscription.
> > 
> > Regards,
> > Sudeep
> > 
> > diff --git a/ArmPkg/Drivers/ArmScmiDxe/ScmiClockProtocol.c b/ArmPkg/Drivers/ArmScmiDxe/ScmiClockProtocol.c
> > index 12a7e6df5d43..1412717bc227 100644
> > --- a/ArmPkg/Drivers/ArmScmiDxe/ScmiClockProtocol.c
> > +++ b/ArmPkg/Drivers/ArmScmiDxe/ScmiClockProtocol.c
> > @@ -236,12 +236,7 @@ ClockDescribeRates (
> >         *TotalRates = NUM_RATES (DescribeRates->NumRatesFlags)
> >                       + NUM_REMAIN_RATES (DescribeRates->NumRatesFlags);
> > -      if (*Format == ScmiClockRateFormatDiscrete) {
> > -        RequiredArraySize = (*TotalRates) * sizeof (UINT64);
> > -      } else {
> > -        // We need to return triplet of 64 bit value for each rate
> > -        RequiredArraySize = (*TotalRates) * 3 * sizeof (UINT64);
> > -      }
> > +      RequiredArraySize = (*TotalRates) * sizeof (UINT64);
> >         if (RequiredArraySize > (*RateArraySize)) {
> >           *RateArraySize = RequiredArraySize;
> 
> I think the following also needs to be modified:
> """
> for (RateNo = 0; RateNo < NUM_RATES (DescribeRates->NumRatesFlags); RateNo++) {
>   // Linear clock rates from minimum to maximum in steps
>   // Minimum clock rate.
>   Rate                                    = &DescribeRates->Rates[RateOffset++];
>   RateArray[RateIndex].ContinuousRate.Min =
> 	ConvertTo64Bit (Rate->Low, Rate->High);
> 
>   Rate = &DescribeRates->Rates[RateOffset++];
>   // Maximum clock rate.
>   RateArray[RateIndex].ContinuousRate.Max =
> 	ConvertTo64Bit (Rate->Low, Rate->High);
> 
>   Rate = &DescribeRates->Rates[RateOffset++];
>   // Step.
>   RateArray[RateIndex++].ContinuousRate.Step =
> 	ConvertTo64Bit (Rate->Low, Rate->High);
> }
> """
>

Right, good catch. For some reason, I don't see any exceptions in release
build :(. I will try the debug build and check. Thanks for the review.
> 
> With the above loop removed:
> Reviewed-by: Pierre Gondois <pierre.gondois@arm.com>
> Tested-by: Pierre Gondois <pierre.gondois@arm.com>
>

Thanks!

-- 
Regards,
Sudeep

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

* Re: [edk2-devel] [PATCH] ArmPkg/ArmScmiDxe: Fix the calculation of RequiredArraySize in ClockDescribeRates()
  2023-02-03 11:20   ` Sudeep Holla
@ 2023-02-03 12:27     ` Ard Biesheuvel
  0 siblings, 0 replies; 4+ messages in thread
From: Ard Biesheuvel @ 2023-02-03 12:27 UTC (permalink / raw)
  To: devel, sudeep.holla
  Cc: Pierre Gondois, Leif Lindholm, Ard Biesheuvel, Girish Pathak,
	Jeff Brasen, Sami Mujawar

On Fri, 3 Feb 2023 at 12:20, Sudeep Holla <sudeep.holla@arm.com> wrote:
>
> On Fri, Feb 03, 2023 at 12:10:38PM +0100, Pierre Gondois wrote:
> > Hello Sudeep,
> >
> > On 2/2/23 15:30, Sudeep Holla wrote:
> > > As per the SCMI specification, section CLOCK_DESCRIBE_RATES mentions that
> > > the value of num_rates_flags[11:0] in the response must be 3 if the return
> > > format is the triplet. Due to the buggy firmware, this was not noticed for
> > > long time. The firmware is now fixed resulting in ClockDescribeRates() to
> > > fail with "Buffer Too Small" error as the RequiredArraySize gets miscalculated
> > > as 72 instead of 24.
> > >
> > > Fix the issue by reusing the logic for both the return format which must
> > > work if num_rates_flags has correct value as expected from the specification.
> > >
> > > Cc: Pierre Gondois <Pierre.Gondois@arm.com>
> > > Cc: Girish Pathak <girish.pathak@arm.com>
> > > Cc: Jeff Brasen <jbrasen@nvidia.com>
> > > Reported-by: Sami Mujawar <sami.mujawar@arm.com>
> > > Signed-off-by: Sudeep Holla <sudeep.holla@arm.com>
> > > ---
> > >   ArmPkg/Drivers/ArmScmiDxe/ScmiClockProtocol.c | 7 +------
> > >   1 file changed, 1 insertion(+), 6 deletions(-)
> > >
> > > Hi All,
> > >
> > > Sorry for the resend, I hadn't subscribed before so resending after the
> > > subscription.
> > >
> > > Regards,
> > > Sudeep
> > >
> > > diff --git a/ArmPkg/Drivers/ArmScmiDxe/ScmiClockProtocol.c b/ArmPkg/Drivers/ArmScmiDxe/ScmiClockProtocol.c
> > > index 12a7e6df5d43..1412717bc227 100644
> > > --- a/ArmPkg/Drivers/ArmScmiDxe/ScmiClockProtocol.c
> > > +++ b/ArmPkg/Drivers/ArmScmiDxe/ScmiClockProtocol.c
> > > @@ -236,12 +236,7 @@ ClockDescribeRates (
> > >         *TotalRates = NUM_RATES (DescribeRates->NumRatesFlags)
> > >                       + NUM_REMAIN_RATES (DescribeRates->NumRatesFlags);
> > > -      if (*Format == ScmiClockRateFormatDiscrete) {
> > > -        RequiredArraySize = (*TotalRates) * sizeof (UINT64);
> > > -      } else {
> > > -        // We need to return triplet of 64 bit value for each rate
> > > -        RequiredArraySize = (*TotalRates) * 3 * sizeof (UINT64);
> > > -      }
> > > +      RequiredArraySize = (*TotalRates) * sizeof (UINT64);
> > >         if (RequiredArraySize > (*RateArraySize)) {
> > >           *RateArraySize = RequiredArraySize;
> >
> > I think the following also needs to be modified:
> > """
> > for (RateNo = 0; RateNo < NUM_RATES (DescribeRates->NumRatesFlags); RateNo++) {
> >   // Linear clock rates from minimum to maximum in steps
> >   // Minimum clock rate.
> >   Rate                                    = &DescribeRates->Rates[RateOffset++];
> >   RateArray[RateIndex].ContinuousRate.Min =
> >       ConvertTo64Bit (Rate->Low, Rate->High);
> >
> >   Rate = &DescribeRates->Rates[RateOffset++];
> >   // Maximum clock rate.
> >   RateArray[RateIndex].ContinuousRate.Max =
> >       ConvertTo64Bit (Rate->Low, Rate->High);
> >
> >   Rate = &DescribeRates->Rates[RateOffset++];
> >   // Step.
> >   RateArray[RateIndex++].ContinuousRate.Step =
> >       ConvertTo64Bit (Rate->Low, Rate->High);
> > }
> > """
> >
>
> Right, good catch. For some reason, I don't see any exceptions in release
> build :(. I will try the debug build and check. Thanks for the review.
> >
> > With the above loop removed:
> > Reviewed-by: Pierre Gondois <pierre.gondois@arm.com>
> > Tested-by: Pierre Gondois <pierre.gondois@arm.com>
> >
>
> Thanks!
>

Excellent. Could someone post the final version please?

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

end of thread, other threads:[~2023-02-03 12:27 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-02-02 14:30 [PATCH] ArmPkg/ArmScmiDxe: Fix the calculation of RequiredArraySize in ClockDescribeRates() Sudeep Holla
2023-02-03 11:10 ` PierreGondois
2023-02-03 11:20   ` Sudeep Holla
2023-02-03 12:27     ` [edk2-devel] " Ard Biesheuvel

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox