From patchwork Tue Jun 27 07:41:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 697533 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id ACF3FEB64D9 for ; Tue, 27 Jun 2023 07:41:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229861AbjF0Hlq (ORCPT ); Tue, 27 Jun 2023 03:41:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39670 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229884AbjF0Hlp (ORCPT ); Tue, 27 Jun 2023 03:41:45 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1BC061B2 for ; Tue, 27 Jun 2023 00:41:40 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id A46F161048 for ; Tue, 27 Jun 2023 07:41:39 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CFA37C433C8; Tue, 27 Jun 2023 07:41:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1687851699; bh=m0hbLmMXErw2pjxKdsddIrOX4/v0RSmwGX1Zj/XNtOY=; h=From:To:Cc:Subject:Date:From; b=lmhQztFIcfIox/nnkgy/KxCiwSbO+i3nqegk2q4ZpkoUrT+0pCP7OoxeXovaXeyxa enToignRtCeJj0fJnOtzM/YIrRpQvbEm4s3UHj/WjGiStV5xisiX/wMSPgKiXNKuk3 Nuzui+wOXh8Px5h6YnmVZf/Px9XYN4GXCnM07geudzPN3Mtca15iyTH9ycVsJ9Njdv OBpeSdBULuO64aTzejVpPZC9t+Iuo74hEC02sYpGKF7A0k+X/lId8YdttDlIj/DLFc TkmnwFMemDnokFexPX6viUwsIJ/EKAPeNUy8KDnbL215boQYpJiBbG6+w7WzYzs8ti bkj5dFBUVaa4g== From: Ard Biesheuvel To: linux-efi@vger.kernel.org Cc: Ard Biesheuvel , Matthew Garrett , Daniel Kiper , Glenn Washburn Subject: [PATCH] efi/libstub: Disable PCI DMA before grabbing the EFI memory map Date: Tue, 27 Jun 2023 09:41:32 +0200 Message-Id: <20230627074132.1016795-1-ardb@kernel.org> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2135; i=ardb@kernel.org; h=from:subject; bh=m0hbLmMXErw2pjxKdsddIrOX4/v0RSmwGX1Zj/XNtOY=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIWXWpDWuNdaa9y77CN+z03tS+vL0HGehc3bTPrVYxZ6Nn fIs5n1YRykLgxgHg6yYIovA7L/vdp6eKFXrPEsWZg4rE8gQBi5OAZjITDmGf8rb9EIF5FYdLdmm aRnzRFqpuKy/uOFTlPmB1Se9uLasKmFkuB4WYvavrK3xSXfDjpMqBzbolFVP5tIUWMa0cKlDYUc FKwA= X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 Precedence: bulk List-ID: X-Mailing-List: linux-efi@vger.kernel.org Currently, the EFI stub will disable PCI DMA as the very last thing it does before calling ExitBootServices(), to avoid interfering with the firmware's normal operation as much as possible. However, the stub will invoke DisconnectController() on all endpoints downstream of the PCI bridges it disables, and this may affect the layout of the EFI memory map, making it likely that ExitBootServices() will fail the first time around, and that the EFI memory map needs to be reloaded. This, in turn, increases the likelihood that the slack space we allocated is insufficient (and we can no longer allocate memory via boot services after having called ExitBootServices() once), causing the second call to GetMemoryMap (and therefore the boot) to fail. This makes the PCI DMA disable feature a bit more fragile than it already is, so let's make it more robust, by allocating the space for the EFI memory map after disabling PCI DMA. Cc: Matthew Garrett Cc: Daniel Kiper Reported-by: Glenn Washburn Signed-off-by: Ard Biesheuvel Acked-by: Matthew Garrett --- drivers/firmware/efi/libstub/efi-stub-helper.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/drivers/firmware/efi/libstub/efi-stub-helper.c b/drivers/firmware/efi/libstub/efi-stub-helper.c index 51779279fbff21b5..bfa30625f5d03167 100644 --- a/drivers/firmware/efi/libstub/efi-stub-helper.c +++ b/drivers/firmware/efi/libstub/efi-stub-helper.c @@ -380,6 +380,9 @@ efi_status_t efi_exit_boot_services(void *handle, void *priv, struct efi_boot_memmap *map; efi_status_t status; + if (efi_disable_pci_dma) + efi_pci_disable_bridge_busmaster(); + status = efi_get_memory_map(&map, true); if (status != EFI_SUCCESS) return status; @@ -390,9 +393,6 @@ efi_status_t efi_exit_boot_services(void *handle, void *priv, return status; } - if (efi_disable_pci_dma) - efi_pci_disable_bridge_busmaster(); - status = efi_bs_call(exit_boot_services, handle, map->map_key); if (status == EFI_INVALID_PARAMETER) {