From patchwork Thu Jan 2 04:13:23 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dan Williams X-Patchwork-Id: 206666 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9704FC2D0DD for ; Thu, 2 Jan 2020 04:29:27 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 75523215A4 for ; Thu, 2 Jan 2020 04:29:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727630AbgABE30 (ORCPT ); Wed, 1 Jan 2020 23:29:26 -0500 Received: from mga12.intel.com ([192.55.52.136]:10262 "EHLO mga12.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726234AbgABE30 (ORCPT ); Wed, 1 Jan 2020 23:29:26 -0500 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga006.jf.intel.com ([10.7.209.51]) by fmsmga106.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 01 Jan 2020 20:29:25 -0800 X-IronPort-AV: E=Sophos;i="5.69,385,1571727600"; d="scan'208";a="221274665" Received: from dwillia2-desk3.jf.intel.com (HELO dwillia2-desk3.amr.corp.intel.com) ([10.54.39.16]) by orsmga006-auth.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 01 Jan 2020 20:29:25 -0800 Subject: [PATCH v3 1/4] efi: Add a flags parameter to efi_memory_map From: Dan Williams To: mingo@redhat.com Cc: Taku Izumi , Ard Biesheuvel , linux-kernel@vger.kernel.org, linux-efi@vger.kernel.org, kexec@lists.infradead.org, x86@kernel.org Date: Wed, 01 Jan 2020 20:13:23 -0800 Message-ID: <157793840352.977550.5176906606902330845.stgit@dwillia2-desk3.amr.corp.intel.com> In-Reply-To: <157793839827.977550.7845382457971215205.stgit@dwillia2-desk3.amr.corp.intel.com> References: <157793839827.977550.7845382457971215205.stgit@dwillia2-desk3.amr.corp.intel.com> User-Agent: StGit/0.18-3-g996c MIME-Version: 1.0 Sender: linux-efi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-efi@vger.kernel.org In preparation for garbage collecting dynamically allocated efi memory maps, where the allocation method of memblock vs slab needs to be recalled, convert the existing 'late' flag into a 'flags' bitmask. Cc: Taku Izumi Cc: Ard Biesheuvel Signed-off-by: Dan Williams --- drivers/firmware/efi/memmap.c | 24 +++++++++++++----------- include/linux/efi.h | 3 ++- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/drivers/firmware/efi/memmap.c b/drivers/firmware/efi/memmap.c index 38b686c67b17..813674ef9000 100644 --- a/drivers/firmware/efi/memmap.c +++ b/drivers/firmware/efi/memmap.c @@ -52,7 +52,7 @@ phys_addr_t __init efi_memmap_alloc(unsigned int num_entries) /** * __efi_memmap_init - Common code for mapping the EFI memory map * @data: EFI memory map data - * @late: Use early or late mapping function? + * @flags: Use early or late mapping function? * * This function takes care of figuring out which function to use to * map the EFI memory map in efi.memmap based on how far into the boot @@ -66,9 +66,9 @@ phys_addr_t __init efi_memmap_alloc(unsigned int num_entries) * Returns zero on success, a negative error code on failure. */ static int __init -__efi_memmap_init(struct efi_memory_map_data *data, bool late) +__efi_memmap_init(struct efi_memory_map_data *data, unsigned long flags) { - struct efi_memory_map map; + struct efi_memory_map map = { 0 }; phys_addr_t phys_map; if (efi_enabled(EFI_PARAVIRT)) @@ -76,7 +76,7 @@ __efi_memmap_init(struct efi_memory_map_data *data, bool late) phys_map = data->phys_map; - if (late) + if (flags & EFI_MEMMAP_LATE) map.map = memremap(phys_map, data->size, MEMREMAP_WB); else map.map = early_memremap(phys_map, data->size); @@ -92,7 +92,7 @@ __efi_memmap_init(struct efi_memory_map_data *data, bool late) map.desc_version = data->desc_version; map.desc_size = data->desc_size; - map.late = late; + map.flags |= flags; set_bit(EFI_MEMMAP, &efi.flags); @@ -111,9 +111,9 @@ __efi_memmap_init(struct efi_memory_map_data *data, bool late) int __init efi_memmap_init_early(struct efi_memory_map_data *data) { /* Cannot go backwards */ - WARN_ON(efi.memmap.late); + WARN_ON(efi.memmap.flags & EFI_MEMMAP_LATE); - return __efi_memmap_init(data, false); + return __efi_memmap_init(data, 0); } void __init efi_memmap_unmap(void) @@ -121,7 +121,7 @@ void __init efi_memmap_unmap(void) if (!efi_enabled(EFI_MEMMAP)) return; - if (!efi.memmap.late) { + if (!(efi.memmap.flags & EFI_MEMMAP_LATE)) { unsigned long size; size = efi.memmap.desc_size * efi.memmap.nr_map; @@ -168,7 +168,7 @@ int __init efi_memmap_init_late(phys_addr_t addr, unsigned long size) WARN_ON(efi.memmap.map); /* Were we already called? */ - WARN_ON(efi.memmap.late); + WARN_ON(efi.memmap.flags & EFI_MEMMAP_LATE); /* * It makes no sense to allow callers to register different @@ -178,7 +178,7 @@ int __init efi_memmap_init_late(phys_addr_t addr, unsigned long size) data.desc_version = efi.memmap.desc_version; data.desc_size = efi.memmap.desc_size; - return __efi_memmap_init(&data, true); + return __efi_memmap_init(&data, EFI_MEMMAP_LATE); } /** @@ -195,6 +195,7 @@ int __init efi_memmap_init_late(phys_addr_t addr, unsigned long size) int __init efi_memmap_install(phys_addr_t addr, unsigned int nr_map) { struct efi_memory_map_data data; + unsigned long flags; efi_memmap_unmap(); @@ -202,8 +203,9 @@ int __init efi_memmap_install(phys_addr_t addr, unsigned int nr_map) data.size = efi.memmap.desc_size * nr_map; data.desc_version = efi.memmap.desc_version; data.desc_size = efi.memmap.desc_size; + flags = efi.memmap.flags & EFI_MEMMAP_LATE; - return __efi_memmap_init(&data, efi.memmap.late); + return __efi_memmap_init(&data, flags); } /** diff --git a/include/linux/efi.h b/include/linux/efi.h index aa54586db7a5..b8e930f5ff77 100644 --- a/include/linux/efi.h +++ b/include/linux/efi.h @@ -795,7 +795,8 @@ struct efi_memory_map { int nr_map; unsigned long desc_version; unsigned long desc_size; - bool late; +#define EFI_MEMMAP_LATE (1UL << 0) + unsigned long flags; }; struct efi_mem_range { From patchwork Thu Jan 2 04:13:33 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dan Williams X-Patchwork-Id: 206665 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 82BA9C3276D for ; Thu, 2 Jan 2020 04:29:37 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 6163B222C3 for ; Thu, 2 Jan 2020 04:29:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727670AbgABE3g (ORCPT ); Wed, 1 Jan 2020 23:29:36 -0500 Received: from mga09.intel.com ([134.134.136.24]:12925 "EHLO mga09.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726234AbgABE3g (ORCPT ); Wed, 1 Jan 2020 23:29:36 -0500 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga102.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 01 Jan 2020 20:29:35 -0800 X-IronPort-AV: E=Sophos;i="5.69,385,1571727600"; d="scan'208";a="369228461" Received: from dwillia2-desk3.jf.intel.com (HELO dwillia2-desk3.amr.corp.intel.com) ([10.54.39.16]) by orsmga004-auth.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 01 Jan 2020 20:29:35 -0800 Subject: [PATCH v3 3/4] efi: Fix efi_memmap_alloc() leaks From: Dan Williams To: mingo@redhat.com Cc: Taku Izumi , Ard Biesheuvel , linux-kernel@vger.kernel.org, linux-efi@vger.kernel.org, kexec@lists.infradead.org, x86@kernel.org Date: Wed, 01 Jan 2020 20:13:33 -0800 Message-ID: <157793841381.977550.3798473750871197840.stgit@dwillia2-desk3.amr.corp.intel.com> In-Reply-To: <157793839827.977550.7845382457971215205.stgit@dwillia2-desk3.amr.corp.intel.com> References: <157793839827.977550.7845382457971215205.stgit@dwillia2-desk3.amr.corp.intel.com> User-Agent: StGit/0.18-3-g996c MIME-Version: 1.0 Sender: linux-efi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-efi@vger.kernel.org With efi_fake_memmap() and efi_arch_mem_reserve() the efi table may be updated and replaced multiple times. When that happens a previous dynamically allocated efi memory map can be garbage collected. Use the new EFI_MEMMAP_{SLAB,MEMBLOCK} flags to detect when a dynamically allocated memory map is being replaced. Debug statements in efi_memmap_free() reveal: efi: __efi_memmap_free:37: phys: 0x23ffdd580 size: 2688 flags: 0x2 efi: __efi_memmap_free:37: phys: 0x9db00 size: 2640 flags: 0x2 efi: __efi_memmap_free:37: phys: 0x9e580 size: 2640 flags: 0x2 ...a savings of 7968 bytes on a qemu boot with 2 entries specified to efi_fake_mem=. Cc: Taku Izumi Cc: Ard Biesheuvel Signed-off-by: Dan Williams --- drivers/firmware/efi/memmap.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/drivers/firmware/efi/memmap.c b/drivers/firmware/efi/memmap.c index 2b81ee6858a9..46c8b4056cc1 100644 --- a/drivers/firmware/efi/memmap.c +++ b/drivers/firmware/efi/memmap.c @@ -29,6 +29,28 @@ static phys_addr_t __init __efi_memmap_alloc_late(unsigned long size) return PFN_PHYS(page_to_pfn(p)); } +static void __init __efi_memmap_free(u64 phys, unsigned long size, unsigned long flags) +{ + if (flags & EFI_MEMMAP_MEMBLOCK) { + if (slab_is_available()) + memblock_free_late(phys, size); + else + memblock_free(phys, size); + } else if (flags & EFI_MEMMAP_SLAB) { + struct page *p = pfn_to_page(PHYS_PFN(phys)); + unsigned int order = get_order(size); + + free_pages((unsigned long) page_address(p), order); + } +} + +static void __init efi_memmap_free(void) +{ + __efi_memmap_free(efi.memmap.phys_map, + efi.memmap.desc_size * efi.memmap.nr_map, + efi.memmap.flags); +} + /** * efi_memmap_alloc - Allocate memory for the EFI memory map * @num_entries: Number of entries in the allocated map. @@ -90,6 +112,8 @@ __efi_memmap_init(struct efi_memory_map_data *data, unsigned long flags) return -ENOMEM; } + efi_memmap_free(); + map.phys_map = data->phys_map; map.nr_map = data->size / data->desc_size; map.map_end = map.map + data->size;