Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp15747569ybl; Tue, 31 Dec 2019 14:22:22 -0800 (PST) X-Google-Smtp-Source: APXvYqzwqYfu9bkDf27jjuNsUcrWJKR+8D7vzD2TSf6V7NC/jLqp9oQNOOgGBml7pdTy+mL7hPpn X-Received: by 2002:aa7:c2d3:: with SMTP id m19mr77192591edp.136.1577830942263; Tue, 31 Dec 2019 14:22:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1577830942; cv=none; d=google.com; s=arc-20160816; b=ZRCkWhpdWAqkLDXvWuGEiXgdbph+KMg8Jy0FCYdwCSw7MYUdMMDZUuAeEWm1YPyb09 h5YomT0u2lNF0qdrXdoU/SjwMDdxgdOUZL57EQg7pe9Ym4IK7VW2nlEaTOYejcvy/b/e wNkiDU3iYCk5zVNjhY+/pljkULFSI/WHvh/vHTX7ND4+YGb0+Wv08gHV+Mj6XLwxqB6m XNll/K06Uh1v2kW+pZ1xSuby5aJ2jgeFZWOqqEg6KKiiM8gU9KmBL8K146XWDRG3sDis taQZjF7m8xHtwIRxvKMrs5qX1r8sRQJT2I4gF5vpRG0gZzW6SJ8luS/Ytv6GKvjZs065 f05w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:cc:to:from :subject; bh=Cul83B4gm1bJT2a9tJcM9KLYlAlsGI55Iw/BOGTMULc=; b=q4Sn7QxtxC+FdjM5+fx9d2yfJHgf4dvgj1dpc7pxnoQXMoweIti2IX737Eh1AVqHKg rILavEA/tthVgGrnnOjLnbirnOqmGVyQb7/47JgcBSoFFV7vmeH35cqqk6nEQAN403BX 1pQSqBDVO+sYXDCGjjokNsdSXHGK44PJjp+AxW6UtiOpeBxp+9mKtaJ2sAokz4tVtSMS 7gmb/xKO5jrOo6KLg6SljnJt2mWC/odc+zQbVH2z4Ns5TZ/WLVrAwmxrivXdbfiYG3ip qejf9AUWsUNBiHKXHWmwJuLq4bT5Y8995iV4r9giwg3BfBIAhU7FS4JPza2M3mFlsWfS IS7w== 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 j20si31101340eds.410.2019.12.31.14.21.58; Tue, 31 Dec 2019 14:22:22 -0800 (PST) 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 S1727146AbfLaWUb (ORCPT + 99 others); Tue, 31 Dec 2019 17:20:31 -0500 Received: from mga18.intel.com ([134.134.136.126]:19283 "EHLO mga18.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727054AbfLaWUa (ORCPT ); Tue, 31 Dec 2019 17:20:30 -0500 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga003.jf.intel.com ([10.7.209.27]) by orsmga106.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 31 Dec 2019 14:20:30 -0800 X-IronPort-AV: E=Sophos;i="5.69,380,1571727600"; d="scan'208";a="221518225" Received: from dwillia2-desk3.jf.intel.com (HELO dwillia2-desk3.amr.corp.intel.com) ([10.54.39.16]) by orsmga003-auth.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 31 Dec 2019 14:20:30 -0800 Subject: [PATCH v2 2/4] efi: Add tracking for dynamically allocated memmaps 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: Tue, 31 Dec 2019 14:04:28 -0800 Message-ID: <157782986828.367056.12411146199853107797.stgit@dwillia2-desk3.amr.corp.intel.com> In-Reply-To: <157782985777.367056.14741265874314204783.stgit@dwillia2-desk3.amr.corp.intel.com> References: <157782985777.367056.14741265874314204783.stgit@dwillia2-desk3.amr.corp.intel.com> User-Agent: StGit/0.18-3-g996c MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In preparation for fixing efi_memmap_alloc() leaks, add support for recording whether the memmap was dynamically allocated from slab, memblock, or is the original physical memmap provided by the platform. Cc: Taku Izumi Cc: Ard Biesheuvel Signed-off-by: Dan Williams --- arch/x86/platform/efi/efi.c | 2 +- arch/x86/platform/efi/quirks.c | 11 ++++++----- drivers/firmware/efi/fake_mem.c | 5 +++-- drivers/firmware/efi/memmap.c | 16 ++++++++++------ include/linux/efi.h | 8 ++++++-- 5 files changed, 26 insertions(+), 16 deletions(-) diff --git a/arch/x86/platform/efi/efi.c b/arch/x86/platform/efi/efi.c index 38d44f36d5ed..7086afbb84fd 100644 --- a/arch/x86/platform/efi/efi.c +++ b/arch/x86/platform/efi/efi.c @@ -333,7 +333,7 @@ static void __init efi_clean_memmap(void) u64 size = efi.memmap.nr_map - n_removal; pr_warn("Removing %d invalid memory map entries.\n", n_removal); - efi_memmap_install(efi.memmap.phys_map, size); + efi_memmap_install(efi.memmap.phys_map, size, 0); } } diff --git a/arch/x86/platform/efi/quirks.c b/arch/x86/platform/efi/quirks.c index f8f0220b6a66..4a71c790f9c3 100644 --- a/arch/x86/platform/efi/quirks.c +++ b/arch/x86/platform/efi/quirks.c @@ -244,6 +244,7 @@ EXPORT_SYMBOL_GPL(efi_query_variable_store); void __init efi_arch_mem_reserve(phys_addr_t addr, u64 size) { phys_addr_t new_phys, new_size; + unsigned long flags = 0; struct efi_mem_range mr; efi_memory_desc_t md; int num_entries; @@ -272,8 +273,7 @@ void __init efi_arch_mem_reserve(phys_addr_t addr, u64 size) num_entries += efi.memmap.nr_map; new_size = efi.memmap.desc_size * num_entries; - - new_phys = efi_memmap_alloc(num_entries); + new_phys = efi_memmap_alloc(num_entries, &flags); if (!new_phys) { pr_err("Could not allocate boot services memmap\n"); return; @@ -288,7 +288,7 @@ void __init efi_arch_mem_reserve(phys_addr_t addr, u64 size) efi_memmap_insert(&efi.memmap, new, &mr); early_memunmap(new, new_size); - efi_memmap_install(new_phys, num_entries); + efi_memmap_install(new_phys, num_entries, flags); e820__range_update(addr, size, E820_TYPE_RAM, E820_TYPE_RESERVED); e820__update_table(e820_table); } @@ -408,6 +408,7 @@ static void __init efi_unmap_pages(efi_memory_desc_t *md) void __init efi_free_boot_services(void) { phys_addr_t new_phys, new_size; + unsigned long flags = 0; efi_memory_desc_t *md; int num_entries = 0; void *new, *new_md; @@ -463,7 +464,7 @@ void __init efi_free_boot_services(void) return; new_size = efi.memmap.desc_size * num_entries; - new_phys = efi_memmap_alloc(num_entries); + new_phys = efi_memmap_alloc(num_entries, &flags); if (!new_phys) { pr_err("Failed to allocate new EFI memmap\n"); return; @@ -493,7 +494,7 @@ void __init efi_free_boot_services(void) memunmap(new); - if (efi_memmap_install(new_phys, num_entries)) { + if (efi_memmap_install(new_phys, num_entries, flags)) { 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 bb9fc70d0cfa..7e53e5520548 100644 --- a/drivers/firmware/efi/fake_mem.c +++ b/drivers/firmware/efi/fake_mem.c @@ -39,6 +39,7 @@ void __init efi_fake_memmap(void) int new_nr_map = efi.memmap.nr_map; efi_memory_desc_t *md; phys_addr_t new_memmap_phy; + unsigned long flags = 0; void *new_memmap; int i; @@ -55,7 +56,7 @@ void __init efi_fake_memmap(void) } /* allocate memory for new EFI memmap */ - new_memmap_phy = efi_memmap_alloc(new_nr_map); + new_memmap_phy = efi_memmap_alloc(new_nr_map, &flags); if (!new_memmap_phy) return; @@ -73,7 +74,7 @@ void __init efi_fake_memmap(void) /* swap into new EFI memmap */ early_memunmap(new_memmap, efi.memmap.desc_size * new_nr_map); - efi_memmap_install(new_memmap_phy, new_nr_map); + efi_memmap_install(new_memmap_phy, new_nr_map, flags); /* print new EFI memmap */ efi_print_memmap(); diff --git a/drivers/firmware/efi/memmap.c b/drivers/firmware/efi/memmap.c index 813674ef9000..2b81ee6858a9 100644 --- a/drivers/firmware/efi/memmap.c +++ b/drivers/firmware/efi/memmap.c @@ -32,6 +32,7 @@ static phys_addr_t __init __efi_memmap_alloc_late(unsigned long size) /** * efi_memmap_alloc - Allocate memory for the EFI memory map * @num_entries: Number of entries in the allocated map. + * @flags: Late map, memblock alloc, slab alloc flags * * Depending on whether mm_init() has already been invoked or not, * either memblock or "normal" page allocation is used. @@ -39,20 +40,23 @@ static phys_addr_t __init __efi_memmap_alloc_late(unsigned long size) * Returns the physical address of the allocated memory map on * success, zero on failure. */ -phys_addr_t __init efi_memmap_alloc(unsigned int num_entries) +phys_addr_t __init efi_memmap_alloc(unsigned int num_entries, unsigned long *flags) { unsigned long size = num_entries * efi.memmap.desc_size; - if (slab_is_available()) + if (slab_is_available()) { + *flags |= EFI_MEMMAP_SLAB; return __efi_memmap_alloc_late(size); + } + *flags |= EFI_MEMMAP_MEMBLOCK; return __efi_memmap_alloc_early(size); } /** * __efi_memmap_init - Common code for mapping the EFI memory map * @data: EFI memory map data - * @flags: Use early or late mapping function? + * @flags: Use early or late mapping function, and allocator * * 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 @@ -192,10 +196,10 @@ int __init efi_memmap_init_late(phys_addr_t addr, unsigned long size) * * Returns zero on success, a negative error code on failure. */ -int __init efi_memmap_install(phys_addr_t addr, unsigned int nr_map) +int __init efi_memmap_install(phys_addr_t addr, unsigned int nr_map, + unsigned long flags) { struct efi_memory_map_data data; - unsigned long flags; efi_memmap_unmap(); @@ -203,7 +207,7 @@ 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; + flags |= efi.memmap.flags & EFI_MEMMAP_LATE; return __efi_memmap_init(&data, flags); } diff --git a/include/linux/efi.h b/include/linux/efi.h index b8e930f5ff77..fa2668a992ae 100644 --- a/include/linux/efi.h +++ b/include/linux/efi.h @@ -796,6 +796,8 @@ struct efi_memory_map { unsigned long desc_version; unsigned long desc_size; #define EFI_MEMMAP_LATE (1UL << 0) +#define EFI_MEMMAP_MEMBLOCK (1UL << 1) +#define EFI_MEMMAP_SLAB (1UL << 2) unsigned long flags; }; @@ -1057,11 +1059,13 @@ static inline efi_status_t efi_query_variable_store(u32 attributes, #endif extern void __iomem *efi_lookup_mapped_addr(u64 phys_addr); -extern phys_addr_t __init efi_memmap_alloc(unsigned int num_entries); +extern phys_addr_t __init efi_memmap_alloc(unsigned int num_entries, + unsigned long *flags); extern int __init efi_memmap_init_early(struct efi_memory_map_data *data); extern int __init efi_memmap_init_late(phys_addr_t addr, unsigned long size); extern void __init efi_memmap_unmap(void); -extern int __init efi_memmap_install(phys_addr_t addr, unsigned int nr_map); +extern int __init efi_memmap_install(phys_addr_t addr, unsigned int nr_map, + unsigned long flags); extern int __init efi_memmap_split_count(efi_memory_desc_t *md, struct range *range); extern void __init efi_memmap_insert(struct efi_memory_map *old_memmap,