From: Jian J Wang <jian.j.wang@intel.com>
To: edk2-devel@lists.01.org
Cc: Star Zeng <star.zeng@intel.com>,
Michael D Kinney <michael.d.kinney@intel.com>,
Jiewen Yao <jiewen.yao@intel.com>, Ruiyu Ni <ruiyu.ni@intel.com>,
Laszlo Ersek <lersek@redhat.com>
Subject: [PATCH v2 3/5] MdeModulePkg/Core: fix a lock issue in GCD memory map dump
Date: Tue, 23 Oct 2018 22:53:29 +0800 [thread overview]
Message-ID: <20181023145331.5768-4-jian.j.wang@intel.com> (raw)
In-Reply-To: <20181023145331.5768-1-jian.j.wang@intel.com>
> v2 changes:
> a. Update implementation of CoreGetMemorySpaceMap() and
> CoreGetIoSpaceMap() to avoid lock failure. Drop the code to
> detect debug print level used to achieve the same effect.
This issue is hidden in current code but exposed by introduction
of freed-memory guard feature due to the fact that the feature
will turn all pool allocation to page allocation.
The solution is move the memory allocation in CoreGetMemorySpaceMap()
and CoreGetIoSpaceMap() to be out of the GCD memory map lock.
CoreDumpGcdMemorySpaceMap()
=> CoreGetMemorySpaceMap()
=> CoreAcquireGcdMemoryLock () *
AllocatePool()
=> InternalAllocatePool()
=> CoreAllocatePool()
=> CoreAllocatePoolI()
=> CoreAllocatePoolPagesI()
=> CoreAllocatePoolPages()
=> FindFreePages()
=> PromoteMemoryResource()
=> CoreAcquireGcdMemoryLock() **
Cc: Star Zeng <star.zeng@intel.com>
Cc: Michael D Kinney <michael.d.kinney@intel.com>
Cc: Jiewen Yao <jiewen.yao@intel.com>
Cc: Ruiyu Ni <ruiyu.ni@intel.com>
Cc: Laszlo Ersek <lersek@redhat.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Jian J Wang <jian.j.wang@intel.com>
---
MdeModulePkg/Core/Dxe/Gcd/Gcd.c | 140 ++++++++++++++++++++++++----------------
1 file changed, 86 insertions(+), 54 deletions(-)
diff --git a/MdeModulePkg/Core/Dxe/Gcd/Gcd.c b/MdeModulePkg/Core/Dxe/Gcd/Gcd.c
index d9c65a8045..133c3dcd87 100644
--- a/MdeModulePkg/Core/Dxe/Gcd/Gcd.c
+++ b/MdeModulePkg/Core/Dxe/Gcd/Gcd.c
@@ -1691,10 +1691,10 @@ CoreGetMemorySpaceMap (
OUT EFI_GCD_MEMORY_SPACE_DESCRIPTOR **MemorySpaceMap
)
{
- EFI_STATUS Status;
LIST_ENTRY *Link;
EFI_GCD_MAP_ENTRY *Entry;
EFI_GCD_MEMORY_SPACE_DESCRIPTOR *Descriptor;
+ UINTN Number;
//
// Make sure parameters are valid
@@ -1706,38 +1706,54 @@ CoreGetMemorySpaceMap (
return EFI_INVALID_PARAMETER;
}
- CoreAcquireGcdMemoryLock ();
-
//
// Count the number of descriptors
//
- *NumberOfDescriptors = CoreCountGcdMapEntry (&mGcdMemorySpaceMap);
+ Number = 0;
+ *NumberOfDescriptors = 0;
+ *MemorySpaceMap = NULL;
+ while (TRUE) {
+ //
+ // Allocate the MemorySpaceMap
+ //
+ if (Number != 0) {
+ if (*MemorySpaceMap != NULL) {
+ FreePool (*MemorySpaceMap);
+ }
- //
- // Allocate the MemorySpaceMap
- //
- *MemorySpaceMap = AllocatePool (*NumberOfDescriptors * sizeof (EFI_GCD_MEMORY_SPACE_DESCRIPTOR));
- if (*MemorySpaceMap == NULL) {
- Status = EFI_OUT_OF_RESOURCES;
- goto Done;
- }
+ *MemorySpaceMap = AllocatePool (Number * sizeof(EFI_GCD_MEMORY_SPACE_DESCRIPTOR));
+ if (*MemorySpaceMap == NULL) {
+ return EFI_OUT_OF_RESOURCES;
+ }
- //
- // Fill in the MemorySpaceMap
- //
- Descriptor = *MemorySpaceMap;
- Link = mGcdMemorySpaceMap.ForwardLink;
- while (Link != &mGcdMemorySpaceMap) {
- Entry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);
- BuildMemoryDescriptor (Descriptor, Entry);
- Descriptor++;
- Link = Link->ForwardLink;
+ *NumberOfDescriptors = Number;
+ }
+
+ CoreAcquireGcdMemoryLock ();
+
+ Number = CoreCountGcdMapEntry (&mGcdMemorySpaceMap);
+ if (Number == *NumberOfDescriptors) {
+ //
+ // Fill in the MemorySpaceMap
+ //
+ Descriptor = *MemorySpaceMap;
+ Link = mGcdMemorySpaceMap.ForwardLink;
+ while (Link != &mGcdMemorySpaceMap && Number != 0) {
+ Entry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);
+ BuildMemoryDescriptor (Descriptor, Entry);
+ Descriptor++;
+ Number--;
+ Link = Link->ForwardLink;
+ }
+
+ CoreReleaseGcdMemoryLock ();
+ break;
+ }
+
+ CoreReleaseGcdMemoryLock ();
}
- Status = EFI_SUCCESS;
-Done:
- CoreReleaseGcdMemoryLock ();
- return Status;
+ return EFI_SUCCESS;
}
@@ -1964,10 +1980,10 @@ CoreGetIoSpaceMap (
OUT EFI_GCD_IO_SPACE_DESCRIPTOR **IoSpaceMap
)
{
- EFI_STATUS Status;
LIST_ENTRY *Link;
EFI_GCD_MAP_ENTRY *Entry;
EFI_GCD_IO_SPACE_DESCRIPTOR *Descriptor;
+ UINTN Number;
//
// Make sure parameters are valid
@@ -1979,38 +1995,54 @@ CoreGetIoSpaceMap (
return EFI_INVALID_PARAMETER;
}
- CoreAcquireGcdIoLock ();
+ Number = 0;
+ *NumberOfDescriptors = 0;
+ *IoSpaceMap = NULL;
+ while (TRUE) {
+ //
+ // Allocate the IoSpaceMap
+ //
+ if (Number != 0) {
+ if (*IoSpaceMap != NULL) {
+ FreePool (*IoSpaceMap);
+ }
- //
- // Count the number of descriptors
- //
- *NumberOfDescriptors = CoreCountGcdMapEntry (&mGcdIoSpaceMap);
+ *IoSpaceMap = AllocatePool (Number * sizeof(EFI_GCD_IO_SPACE_DESCRIPTOR));
+ if (*IoSpaceMap == NULL) {
+ return EFI_OUT_OF_RESOURCES;
+ }
- //
- // Allocate the IoSpaceMap
- //
- *IoSpaceMap = AllocatePool (*NumberOfDescriptors * sizeof (EFI_GCD_IO_SPACE_DESCRIPTOR));
- if (*IoSpaceMap == NULL) {
- Status = EFI_OUT_OF_RESOURCES;
- goto Done;
- }
+ *NumberOfDescriptors = Number;
+ }
- //
- // Fill in the IoSpaceMap
- //
- Descriptor = *IoSpaceMap;
- Link = mGcdIoSpaceMap.ForwardLink;
- while (Link != &mGcdIoSpaceMap) {
- Entry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);
- BuildIoDescriptor (Descriptor, Entry);
- Descriptor++;
- Link = Link->ForwardLink;
+ CoreAcquireGcdIoLock ();
+
+ //
+ // Count the number of descriptors
+ //
+ Number = CoreCountGcdMapEntry (&mGcdIoSpaceMap);
+ if (Number == *NumberOfDescriptors) {
+ //
+ // Fill in the IoSpaceMap
+ //
+ Descriptor = *IoSpaceMap;
+ Link = mGcdIoSpaceMap.ForwardLink;
+ while (Link != &mGcdIoSpaceMap && Number != 0) {
+ Entry = CR (Link, EFI_GCD_MAP_ENTRY, Link, EFI_GCD_MAP_SIGNATURE);
+ BuildIoDescriptor (Descriptor, Entry);
+ Descriptor++;
+ Number--;
+ Link = Link->ForwardLink;
+ }
+
+ CoreReleaseGcdIoLock ();
+ break;
+ }
+
+ CoreReleaseGcdIoLock ();
}
- Status = EFI_SUCCESS;
-Done:
- CoreReleaseGcdIoLock ();
- return Status;
+ return EFI_SUCCESS;
}
--
2.16.2.windows.1
next prev parent reply other threads:[~2018-10-23 14:53 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-10-23 14:53 [PATCH v2 0/5] Add freed-memory guard feature Jian J Wang
2018-10-23 14:53 ` [PATCH v2 1/5] MdeModulePkg/MdeModulePkg.dec: update PCD description for new feature Jian J Wang
2018-10-23 16:09 ` Laszlo Ersek
2018-10-24 0:45 ` Wang, Jian J
2018-10-23 14:53 ` [PATCH v2 2/5] UefiCpuPkg/CpuDxe: fix an infinite loop issue Jian J Wang
2018-10-23 16:41 ` Laszlo Ersek
2018-10-23 14:53 ` Jian J Wang [this message]
2018-10-23 18:26 ` [PATCH v2 3/5] MdeModulePkg/Core: fix a lock issue in GCD memory map dump Laszlo Ersek
2018-10-23 14:53 ` [PATCH v2 4/5] MdeModulePkg/Core: add freed-memory guard feature Jian J Wang
2018-10-23 18:29 ` Laszlo Ersek
2018-10-23 14:53 ` [PATCH v2 5/5] MdeModulePkg/Core: fix-up for changes introduced by freed-memory guard Jian J Wang
2018-10-23 17:16 ` Laszlo Ersek
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-list from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20181023145331.5768-4-jian.j.wang@intel.com \
--to=devel@edk2.groups.io \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox