]> git.proxmox.com Git - mirror_edk2.git/commitdiff
OvmfPkg/IoMmuDxe: track all mappings
authorLaszlo Ersek <lersek@redhat.com>
Thu, 7 Sep 2017 13:30:19 +0000 (15:30 +0200)
committerLaszlo Ersek <lersek@redhat.com>
Fri, 8 Sep 2017 18:24:01 +0000 (20:24 +0200)
The "mRecycledMapInfos" list implements an internal pool of unused
MAP_INFO structures between the IoMmuUnmap() and IoMmuMap() functions. The
original goal was to allow IoMmuUnmap() to tear down CommonBuffer mappings
without releasing any memory: IoMmuUnmap() would recycle the MAP_INFO
structure to the list, and IoMmuMap() would always check the list first,
before allocating a brand new MAP_INFO structure.

In one of the following patches, we'll change OvmfPkg/IoMmuDxe so that it
unmaps all existent bus master operations (CommonBuffer, Read, Write) at
ExitBootServices(), strictly after the individual device drivers abort
pending DMA on the devices they manage, in their own ExitBootServices()
notification functions.

For this, rename and repurpose the list to track all live mappings.

This means that IoMmuUnmap() will always release a MAP_INFO structure
(even when cleaning up a CommonBuffer operation). That's fine (for now),
because device drivers are no longer expected to call Unmap() in their
ExitBootServices() notification functions.

In theory, we could also move the allocation and freeing of the stash
buffer from IoMmuAllocateBuffer() and IoMmuFreeBuffer(), respectively, to
IoMmuMap() and IoMmuUnmap(). However, this would require allocating and
freeing a stash buffer in *both* IoMmuMap() and IoMmuUnmap(), as
IoMmuMap() performs in-place decryption for CommonBuffer operations, and
IoMmuUnmap() performs in-place encryption for the same.

By keeping the stash buffer allocation as-is, not only do we keep the code
almost fully undisturbed, but

- we also continue to guarantee that IoMmuUnmap() succeeds: allocating a
  stash buffer in IoMmuUnmap(), for in-place encryption after a
  CommonBuffer operation, could fail;

- we also keep IoMmuUnmap() largely reusable for ExitBootServices()
  callback context: allocating a stash buffer in IoMmuUnmap() would simply
  be forbidden in that context.

Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Cc: Brijesh Singh <brijesh.singh@amd.com>
Cc: Jiewen Yao <jiewen.yao@intel.com>
Cc: Jordan Justen <jordan.l.justen@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
Reviewed-by: Jiewen Yao <jiewen.yao@intel.com>
Reviewed-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Reviewed-by: Brijesh Singh <brijesh.singh@amd.com>
Tested-by: Brijesh Singh <brijesh.singh@amd.com>
OvmfPkg/IoMmuDxe/AmdSevIoMmu.c

index bc57de5b572b6048b6be7a4954ca1ca149a078df..c86e734985552d2f9e5657109cfcd8e4750528da 100644 (file)
@@ -33,14 +33,11 @@ typedef struct {
 } MAP_INFO;\r
 \r
 //\r
 } MAP_INFO;\r
 \r
 //\r
-// List of MAP_INFO structures recycled by Unmap().\r
+// List of the MAP_INFO structures that have been set up by IoMmuMap() and not\r
+// yet torn down by IoMmuUnmap(). The list represents the full set of mappings\r
+// currently in effect.\r
 //\r
 //\r
-// Recycled MAP_INFO structures are equally good for future recycling and\r
-// freeing.\r
-//\r
-STATIC LIST_ENTRY mRecycledMapInfos = INITIALIZE_LIST_HEAD_VARIABLE (\r
-                                        mRecycledMapInfos\r
-                                        );\r
+STATIC LIST_ENTRY mMapInfos = INITIALIZE_LIST_HEAD_VARIABLE (mMapInfos);\r
 \r
 #define COMMON_BUFFER_SIG SIGNATURE_64 ('C', 'M', 'N', 'B', 'U', 'F', 'F', 'R')\r
 \r
 \r
 #define COMMON_BUFFER_SIG SIGNATURE_64 ('C', 'M', 'N', 'B', 'U', 'F', 'F', 'R')\r
 \r
@@ -123,7 +120,6 @@ IoMmuMap (
   )\r
 {\r
   EFI_STATUS                                        Status;\r
   )\r
 {\r
   EFI_STATUS                                        Status;\r
-  LIST_ENTRY                                        *RecycledMapInfo;\r
   MAP_INFO                                          *MapInfo;\r
   EFI_ALLOCATE_TYPE                                 AllocateType;\r
   COMMON_BUFFER_HEADER                              *CommonBufferHeader;\r
   MAP_INFO                                          *MapInfo;\r
   EFI_ALLOCATE_TYPE                                 AllocateType;\r
   COMMON_BUFFER_HEADER                              *CommonBufferHeader;\r
@@ -150,19 +146,10 @@ IoMmuMap (
   // Allocate a MAP_INFO structure to remember the mapping when Unmap() is\r
   // called later.\r
   //\r
   // Allocate a MAP_INFO structure to remember the mapping when Unmap() is\r
   // called later.\r
   //\r
-  RecycledMapInfo = GetFirstNode (&mRecycledMapInfos);\r
-  if (RecycledMapInfo == &mRecycledMapInfos) {\r
-    //\r
-    // No recycled MAP_INFO structure, allocate a new one.\r
-    //\r
-    MapInfo = AllocatePool (sizeof (MAP_INFO));\r
-    if (MapInfo == NULL) {\r
-      Status = EFI_OUT_OF_RESOURCES;\r
-      goto Failed;\r
-    }\r
-  } else {\r
-    MapInfo = CR (RecycledMapInfo, MAP_INFO, Link, MAP_INFO_SIG);\r
-    RemoveEntryList (RecycledMapInfo);\r
+  MapInfo = AllocatePool (sizeof (MAP_INFO));\r
+  if (MapInfo == NULL) {\r
+    Status = EFI_OUT_OF_RESOURCES;\r
+    goto Failed;\r
   }\r
 \r
   //\r
   }\r
 \r
   //\r
@@ -298,6 +285,10 @@ IoMmuMap (
       );\r
   }\r
 \r
       );\r
   }\r
 \r
+  //\r
+  // Track all MAP_INFO structures.\r
+  //\r
+  InsertHeadList (&mMapInfos, &MapInfo->Link);\r
   //\r
   // Populate output parameters.\r
   //\r
   //\r
   // Populate output parameters.\r
   //\r
@@ -430,24 +421,20 @@ IoMmuUnmap (
       CommonBufferHeader->StashBuffer,\r
       MapInfo->NumberOfBytes\r
       );\r
       CommonBufferHeader->StashBuffer,\r
       MapInfo->NumberOfBytes\r
       );\r
-\r
-    //\r
-    // Recycle the MAP_INFO structure.\r
-    //\r
-    InsertTailList (&mRecycledMapInfos, &MapInfo->Link);\r
   } else {\r
     ZeroMem (\r
       (VOID *)(UINTN)MapInfo->PlainTextAddress,\r
       EFI_PAGES_TO_SIZE (MapInfo->NumberOfPages)\r
       );\r
     gBS->FreePages (MapInfo->PlainTextAddress, MapInfo->NumberOfPages);\r
   } else {\r
     ZeroMem (\r
       (VOID *)(UINTN)MapInfo->PlainTextAddress,\r
       EFI_PAGES_TO_SIZE (MapInfo->NumberOfPages)\r
       );\r
     gBS->FreePages (MapInfo->PlainTextAddress, MapInfo->NumberOfPages);\r
-\r
-    //\r
-    // Free the MAP_INFO structure.\r
-    //\r
-    FreePool (MapInfo);\r
   }\r
 \r
   }\r
 \r
+  //\r
+  // Forget and free the MAP_INFO structure.\r
+  //\r
+  RemoveEntryList (&MapInfo->Link);\r
+  FreePool (MapInfo);\r
+\r
   return EFI_SUCCESS;\r
 }\r
 \r
   return EFI_SUCCESS;\r
 }\r
 \r