]> xenbits.xensource.com Git - people/aperard/ovmf.git/commitdiff
OvmfPkg/XenBusDxe: Construct paths without allocation
authorAnthony PERARD <anthony.perard@citrix.com>
Mon, 26 Aug 2019 09:53:00 +0000 (10:53 +0100)
committerAnthony PERARD <anthony.perard@citrix.com>
Fri, 13 Sep 2019 14:30:13 +0000 (15:30 +0100)
When doing an action with a path and subpath in the xenstore,
XenStoreJoin is called to generate "$path/$subpath". But this function
do an allocation of memory which isn't necessary. Instead we will
construct the path with WRITE_REQUEST and data used to generate the
path will be copied directly to the xenstore shared ring.

Also change WRITE_REQUEST.Len type, it only contain sizes and doesn't
need to be exactly 32bits.

Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=2190
Signed-off-by: Anthony PERARD <anthony.perard@citrix.com>
OvmfPkg/XenBusDxe/XenStore.c

index 7b71dc156d5cff12a4cb85895933b486b5877e26..ca7be12d68b10382ea4b8a3299b685f2ca44cf3f 100644 (file)
@@ -53,7 +53,7 @@
 \r
 typedef struct {\r
   CONST VOID  *Data;\r
-  UINT32      Len;\r
+  UINT      Len;\r
 } WRITE_REQUEST;\r
 \r
 /* Register callback to watch subtree (node) in the XenStore. */\r
@@ -260,6 +260,35 @@ XenStoreFindWatch (
   return NULL;\r
 }\r
 \r
+/**\r
+  Fill the first three slots of a WRITE_REQUEST array.\r
+\r
+  When those three slots are concatenated to generate a string, the resulting\r
+  string will be "$Path\0" or "$Path/$SubPath\0" if SubPath is provided.\r
+**/\r
+STATIC\r
+VOID\r
+XenStorePrepareWriteRequest (\r
+  IN OUT WRITE_REQUEST *WriteRequest,\r
+  IN     CONST CHAR8   *Path,\r
+  IN     CONST CHAR8   *SubPath OPTIONAL\r
+  )\r
+{\r
+  SetMem(WriteRequest, 3 * sizeof (WRITE_REQUEST), 0);\r
+  WriteRequest[0].Data = Path;\r
+  WriteRequest[0].Len = AsciiStrSize (Path);\r
+  if (SubPath != NULL && SubPath[0] != '\0') {\r
+    //\r
+    // Remove the \0 from the first part of the request.\r
+    //\r
+    WriteRequest[0].Len--;\r
+    WriteRequest[1].Data = "/";\r
+    WriteRequest[1].Len = 1;\r
+    WriteRequest[2].Data = SubPath;\r
+    WriteRequest[2].Len = AsciiStrSize (SubPath);\r
+  }\r
+}\r
+\r
 //\r
 // Public Utility Functions\r
 // API comments for these methods can be found in XenStore.h\r
@@ -842,6 +871,7 @@ Error:
   @param Transaction    The transaction to use for this request.\r
   @param RequestType    The type of message to send.\r
   @param Body           The body of the request.\r
+  @param SubPath        If !NULL and not "", "/$SubPath" is append to Body.\r
   @param LenPtr         The returned length of the reply.\r
   @param Result         The returned body of the reply.\r
 \r
@@ -854,16 +884,16 @@ XenStoreSingle (
   IN  CONST XENSTORE_TRANSACTION *Transaction,\r
   IN  enum xsd_sockmsg_type   RequestType,\r
   IN  CONST CHAR8             *Body,\r
+  IN  CONST CHAR8             *SubPath OPTIONAL,\r
   OUT UINT32                  *LenPtr OPTIONAL,\r
   OUT VOID                    **Result OPTIONAL\r
   )\r
 {\r
-  WRITE_REQUEST WriteRequest;\r
+  WRITE_REQUEST   WriteRequest[3];\r
 \r
-  WriteRequest.Data = (VOID *) Body;\r
-  WriteRequest.Len = (UINT32)AsciiStrSize (Body);\r
+  XenStorePrepareWriteRequest (WriteRequest, Body, SubPath);\r
 \r
-  return XenStoreTalkv (Transaction, RequestType, &WriteRequest, 1,\r
+  return XenStoreTalkv (Transaction, RequestType, WriteRequest, 3,\r
                         LenPtr, Result);\r
 }\r
 \r
@@ -1113,15 +1143,12 @@ XenStoreListDirectory (
   OUT CONST CHAR8           ***DirectoryListPtr\r
   )\r
 {\r
-  CHAR8 *Path;\r
   CHAR8 *TempStr;\r
   UINT32 Len = 0;\r
   XENSTORE_STATUS Status;\r
 \r
-  Path = XenStoreJoin (DirectoryPath, Node);\r
-  Status = XenStoreSingle (Transaction, XS_DIRECTORY, Path, &Len,\r
+  Status = XenStoreSingle (Transaction, XS_DIRECTORY, DirectoryPath, Node, &Len,\r
                            (VOID **) &TempStr);\r
-  FreePool (Path);\r
   if (Status != XENSTORE_STATUS_SUCCESS) {\r
     return Status;\r
   }\r
@@ -1160,13 +1187,11 @@ XenStoreRead (
   OUT VOID                    **Result\r
   )\r
 {\r
-  CHAR8 *Path;\r
   VOID *Value;\r
   XENSTORE_STATUS Status;\r
 \r
-  Path = XenStoreJoin (DirectoryPath, Node);\r
-  Status = XenStoreSingle (Transaction, XS_READ, Path, LenPtr, &Value);\r
-  FreePool (Path);\r
+  Status = XenStoreSingle (Transaction, XS_READ, DirectoryPath, Node,\r
+    LenPtr, &Value);\r
   if (Status != XENSTORE_STATUS_SUCCESS) {\r
     return Status;\r
   }\r
@@ -1183,21 +1208,13 @@ XenStoreWrite (
   IN CONST CHAR8           *Str\r
   )\r
 {\r
-  CHAR8 *Path;\r
-  WRITE_REQUEST WriteRequest[2];\r
-  XENSTORE_STATUS Status;\r
+  WRITE_REQUEST   WriteRequest[4];\r
 \r
-  Path = XenStoreJoin (DirectoryPath, Node);\r
+  XenStorePrepareWriteRequest (WriteRequest, DirectoryPath, Node);\r
+  WriteRequest[3].Data = Str;\r
+  WriteRequest[3].Len = AsciiStrLen (Str);\r
 \r
-  WriteRequest[0].Data = (VOID *) Path;\r
-  WriteRequest[0].Len = (UINT32)AsciiStrSize (Path);\r
-  WriteRequest[1].Data = (VOID *) Str;\r
-  WriteRequest[1].Len = (UINT32)AsciiStrLen (Str);\r
-\r
-  Status = XenStoreTalkv (Transaction, XS_WRITE, WriteRequest, 2, NULL, NULL);\r
-  FreePool (Path);\r
-\r
-  return Status;\r
+  return XenStoreTalkv (Transaction, XS_WRITE, WriteRequest, 4, NULL, NULL);\r
 }\r
 \r
 XENSTORE_STATUS\r
@@ -1207,12 +1224,9 @@ XenStoreRemove (
   IN CONST CHAR8            *Node\r
   )\r
 {\r
-  CHAR8 *Path;\r
   XENSTORE_STATUS Status;\r
 \r
-  Path = XenStoreJoin (DirectoryPath, Node);\r
-  Status = XenStoreSingle (Transaction, XS_RM, Path, NULL, NULL);\r
-  FreePool (Path);\r
+  Status = XenStoreSingle (Transaction, XS_RM, DirectoryPath, Node, NULL, NULL);\r
 \r
   return Status;\r
 }\r
@@ -1226,7 +1240,7 @@ XenStoreTransactionStart (
   XENSTORE_STATUS Status;\r
 \r
   Status = XenStoreSingle (XST_NIL, XS_TRANSACTION_START, "", NULL,\r
-                           (VOID **) &IdStr);\r
+    NULL, (VOID **) &IdStr);\r
   if (Status == XENSTORE_STATUS_SUCCESS) {\r
     Transaction->Id = (UINT32)AsciiStrDecimalToUintn (IdStr);\r
     FreePool (IdStr);\r
@@ -1246,7 +1260,7 @@ XenStoreTransactionEnd (
   AbortStr[0] = Abort ? 'F' : 'T';\r
   AbortStr[1] = '\0';\r
 \r
-  return XenStoreSingle (Transaction, XS_TRANSACTION_END, AbortStr, NULL, NULL);\r
+  return XenStoreSingle (Transaction, XS_TRANSACTION_END, AbortStr, NULL, NULL, NULL);\r
 }\r
 \r
 XENSTORE_STATUS\r