From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 0F43B20D7A3A4 for ; Sun, 16 Apr 2017 21:56:42 -0700 (PDT) Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga101.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 16 Apr 2017 21:56:41 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.37,213,1488873600"; d="scan'208";a="249958445" Received: from ray-dev.ccr.corp.intel.com ([10.239.9.25]) by fmsmga004.fm.intel.com with ESMTP; 16 Apr 2017 21:56:39 -0700 From: Ruiyu Ni To: edk2-devel@lists.01.org Cc: Hao A Wu Date: Mon, 17 Apr 2017 12:56:36 +0800 Message-Id: <20170417045636.102048-1-ruiyu.ni@intel.com> X-Mailer: git-send-email 2.9.0.windows.1 Subject: [PATCH] UefiCpuPkg/MtrrLib: Avoid running unnecessary code X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 17 Apr 2017 04:56:42 -0000 Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Ruiyu Ni Cc: Hao A Wu --- 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