Received: by 2002:ac0:a581:0:0:0:0:0 with SMTP id m1-v6csp619692imm; Mon, 2 Jul 2018 18:54:05 -0700 (PDT) X-Google-Smtp-Source: ADUXVKIRGirsUodh/TjDnO3eMEVDuTMfNSfEPJ+2iSLTVLLirZYBMOr3iLBfQD5UgC9AdxkjRoID X-Received: by 2002:a63:107:: with SMTP id 7-v6mr23268098pgb.289.1530582845297; Mon, 02 Jul 2018 18:54:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1530582845; cv=none; d=google.com; s=arc-20160816; b=NyZxwc1hY+62GRpXRY4gdwjt1wBTjXge+d9RilmrKdF3OIx0fJz/Rx4MFRCDNGI70g exSThNuC4AHSMxrr4mXrGx18AzbmYjDnL3Hm5d35oCEd7TPje6sY8VTUhZcC9Hywe0mG 1fOfGX9it8mvonKKZ5eDKwFnT5u1WBSf0kzn954rmwPF21rYnBkBSNRJ+lsiW0FOuY5H jJxQ68IoQTNmv6IqliVxS/7SNg3iA61gb2OPYDn+stC6iGDMHf2xLOeZOfR0zIpJnjFm l0DO2TGPTPBE6ZV/t90+ehbxYLWJvd9lZuRGhLtMSbMeGkPdCiC/Bl8MlIMTBGmQlWLv xGjw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:arc-authentication-results; bh=PSe6+ao/G/vmKHghCvGMZiZK6i4vPxDmwrdplZJNW4c=; b=IE+K61zjVJUwKP23AZAynJ712w6zVEke0R1+0oCZ1Pz58TItJXQfW903i2ZZCLB5Pj 3Wqba2FXxY19BIe4bQWuhf9pmLv+dSpMRph4EkNDnpbJGrtwYL9wexVL+GZyQO8bDR4a EVowVEpchubEZffzQLuFBWzyPAwr6g9A8b7cTNLVK8vXrvf/auyfAkdZ9Is5U6vsrkCa d+/rfjiK5Qft8OslY2/jsEx2Odc40qsoF5SbKai5jxii7P+vW8PxQxm44l9/MpqqVKt7 SWny/esrujPD9iIpLqeVQ7dJxkQAzbgkNkvA2cOwqK4lAZOh2F66d+Pdvw8uz8Nmz8v2 nEsg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j4-v6si16647042plk.406.2018.07.02.18.53.50; Mon, 02 Jul 2018 18:54:05 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932517AbeGCBwj (ORCPT + 99 others); Mon, 2 Jul 2018 21:52:39 -0400 Received: from mga07.intel.com ([134.134.136.100]:5207 "EHLO mga07.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753463AbeGCBvW (ORCPT ); Mon, 2 Jul 2018 21:51:22 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga105.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 02 Jul 2018 18:51:21 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.51,301,1526367600"; d="scan'208";a="212956411" Received: from sai-dev-mach.sc.intel.com ([143.183.140.52]) by orsmga004.jf.intel.com with ESMTP; 02 Jul 2018 18:51:15 -0700 From: Sai Praneeth Prakhya To: linux-efi@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Sai Praneeth , Lee Chun-Yi , Dave Young , Borislav Petkov , Laszlo Ersek , Jan Kiszka , Dave Hansen , Bhupesh Sharma , Nicolai Stange , Naresh Bhat , Ricardo Neri , Peter Zijlstra , Taku Izumi , Ravi Shankar , Matt Fleming , Dan Williams , Ard Biesheuvel Subject: [PATCH 4/6] x86/efi: Free existing memory map before installing new memory map Date: Mon, 2 Jul 2018 18:50:52 -0700 Message-Id: <1530582654-25662-5-git-send-email-sai.praneeth.prakhya@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1530582654-25662-1-git-send-email-sai.praneeth.prakhya@intel.com> References: <1530582654-25662-1-git-send-email-sai.praneeth.prakhya@intel.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Sai Praneeth efi_memmap_install(), unmaps the existing memory map and installs a new memory map but doesn't free the memory allocated to the existing memory map. Fortunately, the details about the existing memory map (like the physical address, number of entries and type of memory) are stored in efi.memmap. Hence, use them to free the memory. In __efi_enter_virtual_mode(), we don't use efi_memmap_install() to install a new memory map, instead we use efi_memmap_init_late(). Hence, free existing memory map there too before installing a new memory map. Generally, memory for new memory map is allocated using efi_memmap_alloc() but in __efi_enter_virtual_mode() it's done using realloc_pages() [please see efi_map_regions()]. So, it's OK to free this memory using efi_memmap_free() in efi_free_boot_services(). Also, note that the first time efi_free_memmap() is called either from efi_fake_memmap() or efi_arch_mem_reserve() [depending on the boot sequence], we are actually freeing memblock_reserved memory which isn't allocated by efi_memmap_alloc(). So, there are two outliers where we use efi_free_memmap() to free memory allocated through other sources rather than efi_memmap_alloc(). Signed-off-by: Sai Praneeth Prakhya Suggested-by: Ard Biesheuvel Cc: Lee Chun-Yi Cc: Dave Young Cc: Borislav Petkov Cc: Laszlo Ersek Cc: Jan Kiszka Cc: Dave Hansen Cc: Bhupesh Sharma Cc: Nicolai Stange Cc: Naresh Bhat Cc: Ricardo Neri Cc: Peter Zijlstra Cc: Taku Izumi Cc: Ravi Shankar Cc: Matt Fleming Cc: Dan Williams Cc: Ard Biesheuvel --- arch/x86/platform/efi/efi.c | 3 +++ arch/x86/platform/efi/quirks.c | 6 ++++++ drivers/firmware/efi/fake_mem.c | 3 +++ 3 files changed, 12 insertions(+) diff --git a/arch/x86/platform/efi/efi.c b/arch/x86/platform/efi/efi.c index cda54abf25a6..7756426e93b5 100644 --- a/arch/x86/platform/efi/efi.c +++ b/arch/x86/platform/efi/efi.c @@ -952,6 +952,9 @@ static void __init __efi_enter_virtual_mode(void) * firmware via SetVirtualAddressMap(). */ efi_memmap_unmap(); + /* Free existing memory map before installing new memory map */ + efi_memmap_free(efi.memmap.phys_map, efi.memmap.nr_map, + efi.memmap.alloc_type); if (efi_memmap_init_late(pa, efi.memmap.desc_size * count)) { pr_err("Failed to remap late EFI memory map\n"); diff --git a/arch/x86/platform/efi/quirks.c b/arch/x86/platform/efi/quirks.c index 11fa6ac9f0c2..11800f3cbb93 100644 --- a/arch/x86/platform/efi/quirks.c +++ b/arch/x86/platform/efi/quirks.c @@ -292,6 +292,9 @@ void __init efi_arch_mem_reserve(phys_addr_t addr, u64 size) efi_memmap_insert(&efi.memmap, new, &mr); early_memunmap(new, new_size); + /* Free existing memory map before installing new memory map */ + efi_memmap_free(efi.memmap.phys_map, efi.memmap.nr_map, + efi.memmap.alloc_type); efi_memmap_install(new_phys, num_entries, alloc_type); } @@ -452,6 +455,9 @@ void __init efi_free_boot_services(void) memunmap(new); + /* Free existing memory map before installing new memory map */ + efi_memmap_free(efi.memmap.phys_map, efi.memmap.nr_map, + efi.memmap.alloc_type); if (efi_memmap_install(new_phys, num_entries, alloc_type)) { pr_err("Could not install new EFI memmap\n"); return; diff --git a/drivers/firmware/efi/fake_mem.c b/drivers/firmware/efi/fake_mem.c index 82dcfa1c340b..a47754efb796 100644 --- a/drivers/firmware/efi/fake_mem.c +++ b/drivers/firmware/efi/fake_mem.c @@ -90,6 +90,9 @@ void __init efi_fake_memmap(void) /* swap into new EFI memmap */ early_memunmap(new_memmap, efi.memmap.desc_size * new_nr_map); + /* Free existing memory map before installing new memory map */ + efi_memmap_free(efi.memmap.phys_map, efi.memmap.nr_map, + efi.memmap.alloc_type); efi_memmap_install(new_memmap_phy, new_nr_map, alloc_type); /* print new EFI memmap */ -- 2.7.4