[edk2] [PATCH] UefiCpuPkg/MtrrLib: Avoid running unnecessary code

Ruiyu Ni posted 1 patch 7 years, 6 months ago
Failed in applying to current master (apply log)
UefiCpuPkg/Library/MtrrLib/MtrrLib.c | 35 +++++++++++++++++++----------------
1 file changed, 19 insertions(+), 16 deletions(-)
[edk2] [PATCH] UefiCpuPkg/MtrrLib: Avoid running unnecessary code
Posted by Ruiyu Ni 7 years, 6 months ago
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Ruiyu Ni <ruiyu.ni@intel.com>
Cc: Hao A Wu <hao.a.wu@intel.com>
---
 UefiCpuPkg/Library/MtrrLib/MtrrLib.c | 35 +++++++++++++++++++----------------
 1 file changed, 19 insertions(+), 16 deletions(-)

diff --git a/UefiCpuPkg/Library/MtrrLib/MtrrLib.c b/UefiCpuPkg/Library/MtrrLib/MtrrLib.c
index c38191a..9d19272 100644
--- a/UefiCpuPkg/Library/MtrrLib/MtrrLib.c
+++ b/UefiCpuPkg/Library/MtrrLib/MtrrLib.c
@@ -1536,6 +1536,7 @@ MtrrLibAddVariableMtrr (
 
   MTRR_LIB_ASSERT_ALIGNED (BaseAddress, Length);
   if (Type == CacheInvalid) {
+    ASSERT (Ranges != NULL);
     for (Index = 0; Index < RangeCount; Index++) {
       if (Ranges[Index].BaseAddress <= BaseAddress && BaseAddress < Ranges[Index].BaseAddress + Ranges[Index].Length) {
 
@@ -2027,25 +2028,27 @@ MtrrSetMemoryAttributeWorker (
   ASSERT (OriginalVariableMtrrCount - FreeVariableMtrrCount <= FirmwareVariableMtrrCount);
 
   //
-  // Move MTRRs after the FirmwraeVariableMtrrCount position to beginning
+  // Move MTRRs after the FirmwareVariableMtrrCount position to beginning
   //
-  WorkingIndex = FirmwareVariableMtrrCount;
-  for (Index = 0; Index < FirmwareVariableMtrrCount; Index++) {
-    if (!OriginalVariableMtrr[Index].Valid) {
-      //
-      // Found an empty MTRR in WorkingIndex position
-      //
-      for (; WorkingIndex < OriginalVariableMtrrCount; WorkingIndex++) {
-        if (OriginalVariableMtrr[WorkingIndex].Valid) {
-          break;
+  if (FirmwareVariableMtrrCount < OriginalVariableMtrrCount) {
+    WorkingIndex = FirmwareVariableMtrrCount;
+    for (Index = 0; Index < FirmwareVariableMtrrCount; Index++) {
+      if (!OriginalVariableMtrr[Index].Valid) {
+        //
+        // Found an empty MTRR in WorkingIndex position
+        //
+        for (; WorkingIndex < OriginalVariableMtrrCount; WorkingIndex++) {
+          if (OriginalVariableMtrr[WorkingIndex].Valid) {
+            break;
+          }
         }
-      }
 
-      if (WorkingIndex != OriginalVariableMtrrCount) {
-        CopyMem (&OriginalVariableMtrr[Index], &OriginalVariableMtrr[WorkingIndex], sizeof (VARIABLE_MTRR));
-        VariableSettingModified[Index] = TRUE;
-        VariableSettingModified[WorkingIndex] = TRUE;
-        OriginalVariableMtrr[WorkingIndex].Valid = FALSE;
+        if (WorkingIndex != OriginalVariableMtrrCount) {
+          CopyMem (&OriginalVariableMtrr[Index], &OriginalVariableMtrr[WorkingIndex], sizeof (VARIABLE_MTRR));
+          VariableSettingModified[Index] = TRUE;
+          VariableSettingModified[WorkingIndex] = TRUE;
+          OriginalVariableMtrr[WorkingIndex].Valid = FALSE;
+        }
       }
     }
   }
-- 
2.9.0.windows.1

_______________________________________________
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel
Re: [edk2] [PATCH] UefiCpuPkg/MtrrLib: Avoid running unnecessary code
Posted by Wu, Hao A 7 years, 6 months ago
Reviewed-by: Hao Wu <hao.a.wu@intel.com>


Best Regards,
Hao Wu


> -----Original Message-----
> From: edk2-devel [mailto:edk2-devel-bounces@lists.01.org] On Behalf Of Ruiyu
> Ni
> Sent: Monday, April 17, 2017 12:57 PM
> To: edk2-devel@lists.01.org
> Cc: Wu, Hao A
> Subject: [edk2] [PATCH] UefiCpuPkg/MtrrLib: Avoid running unnecessary code
> 
> Contributed-under: TianoCore Contribution Agreement 1.0
> Signed-off-by: Ruiyu Ni <ruiyu.ni@intel.com>
> Cc: Hao A Wu <hao.a.wu@intel.com>
> ---
>  UefiCpuPkg/Library/MtrrLib/MtrrLib.c | 35 +++++++++++++++++++---------------
> -
>  1 file changed, 19 insertions(+), 16 deletions(-)
> 
> diff --git a/UefiCpuPkg/Library/MtrrLib/MtrrLib.c
> b/UefiCpuPkg/Library/MtrrLib/MtrrLib.c
> index c38191a..9d19272 100644
> --- a/UefiCpuPkg/Library/MtrrLib/MtrrLib.c
> +++ b/UefiCpuPkg/Library/MtrrLib/MtrrLib.c
> @@ -1536,6 +1536,7 @@ MtrrLibAddVariableMtrr (
> 
>    MTRR_LIB_ASSERT_ALIGNED (BaseAddress, Length);
>    if (Type == CacheInvalid) {
> +    ASSERT (Ranges != NULL);
>      for (Index = 0; Index < RangeCount; Index++) {
>        if (Ranges[Index].BaseAddress <= BaseAddress && BaseAddress <
> Ranges[Index].BaseAddress + Ranges[Index].Length) {
> 
> @@ -2027,25 +2028,27 @@ MtrrSetMemoryAttributeWorker (
>    ASSERT (OriginalVariableMtrrCount - FreeVariableMtrrCount <=
> FirmwareVariableMtrrCount);
> 
>    //
> -  // Move MTRRs after the FirmwraeVariableMtrrCount position to beginning
> +  // Move MTRRs after the FirmwareVariableMtrrCount position to beginning
>    //
> -  WorkingIndex = FirmwareVariableMtrrCount;
> -  for (Index = 0; Index < FirmwareVariableMtrrCount; Index++) {
> -    if (!OriginalVariableMtrr[Index].Valid) {
> -      //
> -      // Found an empty MTRR in WorkingIndex position
> -      //
> -      for (; WorkingIndex < OriginalVariableMtrrCount; WorkingIndex++) {
> -        if (OriginalVariableMtrr[WorkingIndex].Valid) {
> -          break;
> +  if (FirmwareVariableMtrrCount < OriginalVariableMtrrCount) {
> +    WorkingIndex = FirmwareVariableMtrrCount;
> +    for (Index = 0; Index < FirmwareVariableMtrrCount; Index++) {
> +      if (!OriginalVariableMtrr[Index].Valid) {
> +        //
> +        // Found an empty MTRR in WorkingIndex position
> +        //
> +        for (; WorkingIndex < OriginalVariableMtrrCount; WorkingIndex++) {
> +          if (OriginalVariableMtrr[WorkingIndex].Valid) {
> +            break;
> +          }
>          }
> -      }
> 
> -      if (WorkingIndex != OriginalVariableMtrrCount) {
> -        CopyMem (&OriginalVariableMtrr[Index],
> &OriginalVariableMtrr[WorkingIndex], sizeof (VARIABLE_MTRR));
> -        VariableSettingModified[Index] = TRUE;
> -        VariableSettingModified[WorkingIndex] = TRUE;
> -        OriginalVariableMtrr[WorkingIndex].Valid = FALSE;
> +        if (WorkingIndex != OriginalVariableMtrrCount) {
> +          CopyMem (&OriginalVariableMtrr[Index],
> &OriginalVariableMtrr[WorkingIndex], sizeof (VARIABLE_MTRR));
> +          VariableSettingModified[Index] = TRUE;
> +          VariableSettingModified[WorkingIndex] = TRUE;
> +          OriginalVariableMtrr[WorkingIndex].Valid = FALSE;
> +        }
>        }
>      }
>    }
> --
> 2.9.0.windows.1
> 
> _______________________________________________
> edk2-devel mailing list
> edk2-devel@lists.01.org
> https://lists.01.org/mailman/listinfo/edk2-devel
_______________________________________________
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel