From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=192.55.52.93; helo=mga11.intel.com; envelope-from=eric.dong@intel.com; receiver=edk2-devel@lists.01.org Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) (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 E7BFA2117B579 for ; Thu, 25 Oct 2018 17:38:37 -0700 (PDT) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga102.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 25 Oct 2018 17:38:37 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.54,426,1534834800"; d="scan'208";a="84555190" Received: from fmsmsx104.amr.corp.intel.com ([10.18.124.202]) by orsmga008.jf.intel.com with ESMTP; 25 Oct 2018 17:38:37 -0700 Received: from fmsmsx125.amr.corp.intel.com (10.18.125.40) by fmsmsx104.amr.corp.intel.com (10.18.124.202) with Microsoft SMTP Server (TLS) id 14.3.319.2; Thu, 25 Oct 2018 17:38:36 -0700 Received: from shsmsx104.ccr.corp.intel.com (10.239.4.70) by FMSMSX125.amr.corp.intel.com (10.18.125.40) with Microsoft SMTP Server (TLS) id 14.3.319.2; Thu, 25 Oct 2018 17:38:36 -0700 Received: from shsmsx102.ccr.corp.intel.com ([169.254.2.84]) by SHSMSX104.ccr.corp.intel.com ([169.254.5.117]) with mapi id 14.03.0415.000; Fri, 26 Oct 2018 08:38:34 +0800 From: "Dong, Eric" To: "Wang, Jian J" , "edk2-devel@lists.01.org" CC: Laszlo Ersek , "Zeng, Star" , "Kinney, Michael D" , "Yao, Jiewen" , "Ni, Ruiyu" Thread-Topic: [PATCH v4 4/6] UefiCpuPkg/CpuDxe: prevent recursive calling of InitializePageTablePool Thread-Index: AQHUbDLoxXdNRcH0gUq0ZIx67vwSEqUwsAZw Date: Fri, 26 Oct 2018 00:38:34 +0000 Message-ID: References: <20181025071805.6692-1-jian.j.wang@intel.com> <20181025071805.6692-5-jian.j.wang@intel.com> In-Reply-To: <20181025071805.6692-5-jian.j.wang@intel.com> Accept-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [10.239.127.40] MIME-Version: 1.0 Subject: Re: [PATCH v4 4/6] UefiCpuPkg/CpuDxe: prevent recursive calling of InitializePageTablePool X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 26 Oct 2018 00:38:38 -0000 Content-Language: en-US Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Reviewed-by: Eric Dong > -----Original Message----- > From: Wang, Jian J > Sent: Thursday, October 25, 2018 3:18 PM > To: edk2-devel@lists.01.org > Cc: Dong, Eric ; Laszlo Ersek ; > Zeng, Star ; Kinney, Michael D > ; Yao, Jiewen ; Ni, > Ruiyu > Subject: [PATCH v4 4/6] UefiCpuPkg/CpuDxe: prevent recursive calling of > InitializePageTablePool >=20 > > v4 changes: none >=20 > The freed-memory guard feature will cause an recursive calling of > InitializePageTablePool(). This is due to a fact that > AllocateAlignedPages() is used to allocate page table pool memory. > This function will most likely call gBS->FreePages to free unaligned page= s and > then cause another round of page attributes change, like below (freed pag= es > will be always marked not-present if freed-memory guard is enabled) >=20 > FreePages() <=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D| > =3D> CpuSetMemoryAttributes() | > =3D> | > =3D> InitializePageTablePool() | > =3D> AllocateAlignedPages() | > =3D> FreePages() =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D| >=20 > The solution is add a global variable as a lock in page table pool alloca= tion > function and fail any other requests if it has not been done. >=20 > Since this issue will only happen if free-memory guard is enabled, it won= 't > affect CpuSetMemoryAttributes() in default build of a BIOS. >=20 > If free-memory guard is enabled, it only affect the pages (failed to mark > them as not-present) freed in AllocateAlignedPages(). >=20 > Since those freed pages haven't been used yet (their addresses not yet > exposed to code outside AllocateAlignedPages), it won't compromise the > freed-memory guard feature. >=20 > This change will just fail the CpuSetMemoryAttributes() called from > FreePages() but it won't fail the FreePages(). So the error status won't = be > propagated upper layer of code. >=20 > Cc: Eric Dong > Cc: Laszlo Ersek > Cc: Star Zeng > Cc: Michael D Kinney > Cc: Jiewen Yao > Cc: Ruiyu Ni > Contributed-under: TianoCore Contribution Agreement 1.1 > Signed-off-by: Jian J Wang > --- > UefiCpuPkg/CpuDxe/CpuPageTable.c | 23 +++++++++++++++++++++-- > 1 file changed, 21 insertions(+), 2 deletions(-) >=20 > diff --git a/UefiCpuPkg/CpuDxe/CpuPageTable.c > b/UefiCpuPkg/CpuDxe/CpuPageTable.c > index 33e8ee2d2c..4bee8c7772 100644 > --- a/UefiCpuPkg/CpuDxe/CpuPageTable.c > +++ b/UefiCpuPkg/CpuDxe/CpuPageTable.c > @@ -100,6 +100,7 @@ PAGE_ATTRIBUTE_TABLE mPageAttributeTable[] =3D { }; >=20 > PAGE_TABLE_POOL *mPageTablePool =3D NULL; > +BOOLEAN mPageTablePoolLock =3D FALSE; > PAGE_TABLE_LIB_PAGING_CONTEXT mPagingContext; > EFI_SMM_BASE2_PROTOCOL *mSmmBase2 =3D NULL; >=20 > @@ -1046,6 +1047,16 @@ InitializePageTablePool ( > VOID *Buffer; > BOOLEAN IsModified; >=20 > + // > + // Do not allow re-entrance. > + // > + if (mPageTablePoolLock) { > + return FALSE; > + } > + > + mPageTablePoolLock =3D TRUE; > + IsModified =3D FALSE; > + > // > // Always reserve at least PAGE_TABLE_POOL_UNIT_PAGES, including one > page for > // header. > @@ -1056,9 +1067,15 @@ InitializePageTablePool ( > Buffer =3D AllocateAlignedPages (PoolPages, > PAGE_TABLE_POOL_ALIGNMENT); > if (Buffer =3D=3D NULL) { > DEBUG ((DEBUG_ERROR, "ERROR: Out of aligned pages\r\n")); > - return FALSE; > + goto Done; > } >=20 > + DEBUG (( > + DEBUG_INFO, > + "Paging: added %lu pages to page table pool\r\n", > + (UINT64)PoolPages > + )); > + > // > // Link all pools into a list for easier track later. > // > @@ -1092,7 +1109,9 @@ InitializePageTablePool ( > ); > ASSERT (IsModified =3D=3D TRUE); >=20 > - return TRUE; > +Done: > + mPageTablePoolLock =3D FALSE; > + return IsModified; > } >=20 > /** > -- > 2.16.2.windows.1