From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-vk0-x232.google.com (mail-vk0-x232.google.com [IPv6:2607:f8b0:400c:c05::232]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 54D2E21DFA901 for ; Thu, 30 Mar 2017 03:42:08 -0700 (PDT) Received: by mail-vk0-x232.google.com with SMTP id d188so48825934vka.0 for ; Thu, 30 Mar 2017 03:42:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=philjordan-eu.20150623.gappssmtp.com; s=20150623; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=lQXfRYS7iDANPuZBroWArfDVrrohoNN23PpnVy5Hklk=; b=hvcQBsZZHIFkgIATsJXvnr14Zk5Sqkq90REY/QfouZTBfHKew9iKqf/9EPwT1KsmQQ TbT4d342bwT0N58LpjYwpoBZgHaDYW4yQ+V53WhmrQv3aAi64QrcYVDZUwH8wHTuAif4 9QtCMQkiAUDY1szJfFVwgUpAs45J3Zbqrfi9CoBnA4rYTt/eYresNRPVs79tvEAGBtyc xTypWf7R3O809TZXGWrm8S6zYlL2ZX08LX+Sm00/U2+1vNYNfuQMoRozMWLUqYUMvrhr TPeWKEN/zeYEo8hjO9EqmiHx29v4nSuGDJBl8YG1HQqZQWVx2/H6V29RzBSggPx89hsv hx0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=lQXfRYS7iDANPuZBroWArfDVrrohoNN23PpnVy5Hklk=; b=pRV0FK6HONBn+ICBB0Vo+VU7vgkz4I63tfDq1oLJTI3EvBd9dvc+fSLB6cbJTgEqwh Js7BZC+2+K+9avqNBb4DC5ZH6vf0bZ1CtDn21HDwvjosh9YoQ6QgbBxNUx/ZBE9DyOSS J0WIiIfVKbYHLV4nVcoGX2AJAZNv+ySixl1VdjXPk1sMoi0Fqg8K11WlJY55H6Ws1K4Z dM9yoroZenfo/UZuHyeB7RCwFDw951OtJUE/z7zHb4GO/YLLYU5DCwhUEjU5ni0l0Ipj M7N3EW4qywaNORo3zQrRQ6oxq0mtcU7DhTsAzRdQkHOB5D14LxmugO8Xz6IeL8lhxjF+ SSiQ== X-Gm-Message-State: AFeK/H2h/dj7xIHBtlJ97r8fLlNaScKEFYTQ/SBcMfAXHptLuhoyT99sDrtagQoMkCSB9Pw/F2sBwp44ZzrRPw== X-Received: by 10.176.71.24 with SMTP id h24mr2612008uac.167.1490870526717; Thu, 30 Mar 2017 03:42:06 -0700 (PDT) MIME-Version: 1.0 Received: by 10.31.58.83 with HTTP; Thu, 30 Mar 2017 03:41:46 -0700 (PDT) In-Reply-To: References: <1490773812-23839-1-git-send-email-lists@philjordan.eu> <1490773812-23839-2-git-send-email-lists@philjordan.eu> From: Phil Dennis-Jordan Date: Thu, 30 Mar 2017 23:41:46 +1300 Message-ID: To: Laszlo Ersek Cc: edk2-devel-01 , Phil Dennis-Jordan Subject: Re: [PATCH v1 1/1] OvmfPkg: Allow multiple add-pointer linker commands to same ACPI table 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: Thu, 30 Mar 2017 10:42:08 -0000 Content-Type: text/plain; charset=UTF-8 On Thu, Mar 30, 2017 at 2:06 PM, Laszlo Ersek wrote: > This is a very good first patch! > > I have a few requests. I'll generally rehash the points from > . > > On 03/29/17 09:50, Phil Dennis-Jordan wrote: >> From: Phil Dennis-Jordan >> >> ACPI tables may contain multiple pointer fields to the same destination >> table. For example, in some revisions, the FADT contains both DSDT and >> X_DSDT fields, and they may both point to the DSDT. Indeed, some operating >> systems demand this to be the case. >> >> Previously, if Qemu created "add pointer" linker commands for both fields, >> the linking process would fail, as AcpiProtocol->InstallAcpiTable() may >> only be called once for each destination table and otherwise returns an >> error. > > (1) Please be a bit more precise here: passing the exact same table > twice to InstallAcpiTable() is wrong, but for two different reasons. > > Reason #1 is what you named -- some, but not all, ACPI table types are > not allowed to have multiple instances, and that aborts the linking > process. Reason #2 is that successfully installing the exact same table, > of a type that does allow multiple instances (such as an SSDT, there > could be others) is just as wrong, although it doesn't abort the linking. Thanks - I know you've explained this distinction a bunch of times now, but it's finally clicked with me. >> >> This change adds a memoisation data structure which tracks the table >> pointers that have already been installed; even if the same pointer is >> encountered multiple times, it is only installed once. > > (2) Please s/installed/processed/g above -- see for more below. > >> >> Contributed-under: TianoCore Contribution Agreement 1.0 >> Signed-off-by: Phil Dennis-Jordan >> --- >> OvmfPkg/AcpiPlatformDxe/QemuFwCfgAcpi.c | 109 ++++++++++++++++---- >> 1 file changed, 89 insertions(+), 20 deletions(-) >> >> diff --git a/OvmfPkg/AcpiPlatformDxe/QemuFwCfgAcpi.c b/OvmfPkg/AcpiPlatformDxe/QemuFwCfgAcpi.c >> index 7bb2e3f21821..cffa838623cc 100644 >> --- a/OvmfPkg/AcpiPlatformDxe/QemuFwCfgAcpi.c >> +++ b/OvmfPkg/AcpiPlatformDxe/QemuFwCfgAcpi.c >> @@ -100,6 +100,39 @@ BlobCompare ( >> >> >> /** >> + Comparator function for two opaque pointers, ordering on pointer value >> + itself. >> + Can be used as both Key and UserStruct comparator. >> + >> + @param[in] Pointer1 First pointer. >> + >> + @param[in] Pointer2 Second pointer. >> + >> + @retval <0 If Pointer1 compares less than Pointer2. >> + >> + @retval 0 If Pointer1 compares equal to Pointer2. >> + >> + @retval >0 If Pointer1 compares greater than Pointer2. >> +**/ >> +STATIC >> +INTN >> +EFIAPI >> +PointerCompare ( >> + IN CONST VOID *Pointer1, >> + IN CONST VOID *Pointer2 >> + ) >> +{ >> + if (Pointer1 == Pointer2) { >> + return 0; >> + } else if ((INTN)Pointer1 < (INTN)Pointer2) { > > (3) Please use UINTN here, not INTN. > > Also, I slightly dislike an "else" after a "return", but I guess it's up > to you. :) > >> + return -1; >> + } else { >> + return 1; >> + } >> +} >> + >> + >> +/** >> Process a QEMU_LOADER_ALLOCATE command. >> >> @param[in] Allocate The QEMU_LOADER_ALLOCATE command to process. >> @@ -535,27 +568,32 @@ UndoCmdWritePointer ( >> This function assumes that the entire QEMU linker/loader command file has >> been processed successfully in a prior first pass. >> >> - @param[in] AddPointer The QEMU_LOADER_ADD_POINTER command to process. >> + @param[in] AddPointer The QEMU_LOADER_ADD_POINTER command to process. >> >> - @param[in] Tracker The ORDERED_COLLECTION tracking the BLOB user >> - structures. >> + @param[in] Tracker The ORDERED_COLLECTION tracking the BLOB user >> + structures. >> >> - @param[in] AcpiProtocol The ACPI table protocol used to install tables. >> + @param[in] AcpiProtocol The ACPI table protocol used to install tables. >> >> - @param[in,out] InstalledKey On input, an array of INSTALLED_TABLES_MAX UINTN >> - elements, allocated by the caller. On output, >> - the function will have stored (appended) the >> - AcpiProtocol-internal key of the ACPI table that >> - the function has installed, if the AddPointer >> - command identified an ACPI table that is >> - different from RSDT and XSDT. >> + @param[in,out] InstalledKey On input, an array of INSTALLED_TABLES_MAX UINTN >> + elements, allocated by the caller. On output, >> + the function will have stored (appended) the >> + AcpiProtocol-internal key of the ACPI table that >> + the function has installed, if the AddPointer >> + command identified an ACPI table that is >> + different from RSDT and XSDT. >> >> - @param[in,out] NumInstalled On input, the number of entries already used in >> - InstalledKey; it must be in [0, >> - INSTALLED_TABLES_MAX] inclusive. On output, the >> - parameter is incremented if the AddPointer >> - command identified an ACPI table that is >> - different from RSDT and XSDT. >> + @param[in,out] NumInstalled On input, the number of entries already used in >> + InstalledKey; it must be in [0, >> + INSTALLED_TABLES_MAX] inclusive. On output, the >> + parameter is incremented if the AddPointer >> + command identified an ACPI table that is >> + different from RSDT and XSDT. >> + >> + @param[in,out] InstalledTables The ORDERED_COLLECTION tracking the ACPI tables >> + which have already been installed. If a new >> + table is encountered by the function, it is >> + added; existing ones will not be installed again. > > (4) Please rename "InstalledTables" to "SeenPointers", and update its > comment block accordingly: > > "The ORDERED_COLLECTION tracking the absolute target addresses that have > been pointed-to by QEMU_LOADER_ADD_POINTER commands thus far. If a > target address is encountered for the first time, and it identifies an > ACPI table that is different from RDST and DSDT, the table is installed. > If a target address is seen for the second or later times, it is skipped > without taking any action." > > (5) An added benefit of using the name "SeenPointers" is that it is > exactly as long as "NumInstalled", and you won't have to reindent the > other comment blocks. > > (6) Please also update the @retval EFI_SUCCESS comment: > > "AddPointer has been processed. Either its absolute target address has > been encountered before, or an ACPI table different from RSDT and XSDT > has been installed (reflected by InstalledKey and NumInstalled), or RSDT > or XSDT has been identified but not installed, or the fw_cfg blob > pointed-into by AddPointer has been marked as hosting something else > than just direct ACPI table contents." > >> >> @retval EFI_INVALID_PARAMETER NumInstalled was outside the allowed range on >> input. >> @@ -584,7 +622,8 @@ Process2ndPassCmdAddPointer ( >> IN CONST ORDERED_COLLECTION *Tracker, >> IN EFI_ACPI_TABLE_PROTOCOL *AcpiProtocol, >> IN OUT UINTN InstalledKey[INSTALLED_TABLES_MAX], >> - IN OUT INT32 *NumInstalled >> + IN OUT INT32 *NumInstalled, >> + IN OUT ORDERED_COLLECTION *InstalledTables >> ) >> { >> CONST ORDERED_COLLECTION_ENTRY *TrackerEntry; >> @@ -679,6 +718,21 @@ Process2ndPassCmdAddPointer ( >> return EFI_SUCCESS; >> } >> >> + Status = OrderedCollectionInsert ( >> + InstalledTables, NULL, (VOID *)(UINTN)PointerValue); > > (7) For function calls (and function-like macro invocations) that don't > fit on a single line, we should now follow the style described in > . > > (8) The main point of renaming InstalledTables to SeenPointers: > > please move this logic higher up in the function, > > ASSERT (PointerValue < Blob2Remaining); > <------- right here > Blob2Remaining -= (UINTN) PointerValue; > > The reason why is explained in > , in the part > that starts with "This is justified because all actions after that point > would be..." I've now moved it as requested for patch v2, but my reasoning for the original ordering of the logic was precisely to avoid the complication of having to roll back in the error case. It seemed a valid code transformation as the other early-out paths also return EFI_SUCCESS, the RSDT/XSDT one has no side effects, and the opaque blob one's side effect is surely idempotent. > (9) Near the two late error exits in the function (when we run out of > INSTALLED_TABLES_MAX, or InstallAcpiTable() fails), please replace the > direct returns with a goto to an error handling label. This error > handling label should remove the just added PointerValue from SeenPointers. > > For this, you can use the second parameter of OrderedCollectionInsert(). > On success, that parameter is set to the node of the newly inserted > item, and then you can pass that to OrderedCollectionDelete() directly > on the error path. > >> + if (EFI_ERROR (Status)) { >> + if (Status == RETURN_ALREADY_STARTED) { >> + // >> + // Already installed this table, don't try to do so again. >> + // >> + DEBUG ((DEBUG_VERBOSE, "%a: AcpiProtocol->InstallAcpiTable reports table " >> + "already installed, skipping. PointerValue=0x%Lx\n", >> + __FUNCTION__, PointerValue)); >> + Status = EFI_SUCCESS; >> + } >> + return Status; >> + } >> + >> if (*NumInstalled == INSTALLED_TABLES_MAX) { >> DEBUG ((EFI_D_ERROR, "%a: can't install more than %d tables\n", >> __FUNCTION__, INSTALLED_TABLES_MAX)); >> @@ -739,6 +793,8 @@ InstallQemuFwCfgTables ( >> UINTN *InstalledKey; >> INT32 Installed; >> ORDERED_COLLECTION_ENTRY *TrackerEntry, *TrackerEntry2; >> + ORDERED_COLLECTION *InstalledTables; >> + ORDERED_COLLECTION_ENTRY *InstalledTableEntry; > > (10) please rename InstalledTables here too. > >> >> Status = QemuFwCfgFindFile ("etc/table-loader", &FwCfgItem, &FwCfgSize); >> if (EFI_ERROR (Status)) { >> @@ -827,14 +883,21 @@ InstallQemuFwCfgTables ( >> goto RollbackWritePointersAndFreeTracker; >> } >> >> + InstalledTables = OrderedCollectionInit (PointerCompare, PointerCompare); >> + if (InstalledTables == NULL) { >> + Status = EFI_OUT_OF_RESOURCES; >> + goto FreeKeys; >> + } >> + > > Looks valid. > >> // >> // second pass: identify and install ACPI tables >> // >> Installed = 0; >> for (LoaderEntry = LoaderStart; LoaderEntry < LoaderEnd; ++LoaderEntry) { >> if (LoaderEntry->Type == QemuLoaderCmdAddPointer) { >> - Status = Process2ndPassCmdAddPointer (&LoaderEntry->Command.AddPointer, >> - Tracker, AcpiProtocol, InstalledKey, &Installed); >> + Status = Process2ndPassCmdAddPointer ( >> + &LoaderEntry->Command.AddPointer, Tracker, AcpiProtocol, >> + InstalledKey, &Installed, InstalledTables); >> if (EFI_ERROR (Status)) { >> goto UninstallAcpiTables; >> } >> @@ -870,6 +933,12 @@ UninstallAcpiTables: >> DEBUG ((EFI_D_INFO, "%a: installed %d tables\n", __FUNCTION__, Installed)); >> } >> >> + while (((InstalledTableEntry = OrderedCollectionMax(InstalledTables))) != NULL) { >> + OrderedCollectionDelete (InstalledTables, InstalledTableEntry, NULL); >> + } >> + OrderedCollectionUninit (InstalledTables); >> + > > (11) Please implement this loop instead like the one that tears down > "Tracker". Using an assignment in the controlling expression of the > "while" is powerful and I like it too, but it is never used in edk2 > code, to my knowledge. There's a third option, which is to also use a for loop, but avoids the second running entry variable by calling OrderedCollectionMin() (or Max) on each iteration. This might even be slightly more efficient than OrderedCollectionNext(), but only by a constant factor, so I've gone with the same teardown as for Tracker for the sake of consistency. >> +FreeKeys: >> FreePool (InstalledKey); > > Yes, this looks correct too. > > (I apologize if I made any confusing requests in my review, I'm very tired.) > > Thank you, > Laszlo > >> >> RollbackWritePointersAndFreeTracker: >> > Thanks for the quick review and detailed explanations, Laszlo! I've submitted v2 of the patch with the changes.