Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp17045214ybl; Wed, 1 Jan 2020 20:32:18 -0800 (PST) X-Google-Smtp-Source: APXvYqy2GsIUlaVabVOlYd1OwQ6BA58BMjyktdXpPC8i/1nbcAwauz8nG3jCZ1kvDbxVNxz8RSyh X-Received: by 2002:a05:6830:194:: with SMTP id q20mr93473032ota.92.1577939538350; Wed, 01 Jan 2020 20:32:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1577939538; cv=none; d=google.com; s=arc-20160816; b=AvdV6MmIAC6XKzNbmjyxc/dvBgT0GCjDipiuXwdEI1mbuY7sc1sCRhE1QGWYEzhH06 jQ68USo8S5k+lMT9C9GUivizLS1qpafcGT/W/ihikV4XmVVcB1hDcTou/XGG8/zg3Vcs M+93B85/cdVJ11emgssT1DzCLSu+aPg8rsSwkTMphd9hOO/h3WFJGFJi0Fsjg4ShWceW c0WVj6SJxlzU42qOkLqPC80J/l9eHpPqNoi6pPeLSyvXfj1KcSpL8JDIeJZHZwTBumVE /gugmj8bLjbAkul/zEHYl8GXspyAdvPyZiwRXk7yAsohC3rg1BHzKu/f2KYnF57DToqL DmhQ== 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=Dt9ip3X4drPIQsjIoz2xoyQAT2kFq2cbhcn7f00DrUw=; b=cF2QnyaxRyegKCrosqAZ0y7wRClHe9ujwHLwk07K4sVxmkdDRAqMjMiTeWTv4gis7J Yun5B3nSXcvlw7ZzeFLEYPOj1e03KcFjvtN0tkdaXH2MkJqDAfDxnBbjMnqSHLEK2hbO TlIc7ejbsw2Xc+es3Y+U2vwC+0swZGCRVYhrg/ZdG6QlyTAfASAoHAdzvrcDIP009Wwf 4YGa1WZ8ZgHPtomgIUS9NLsRoi5+3ol/wEJnl2SFT8jecTfT47RT3LFHPcS+3oyJmcOW 06HZP0rxp4Gnkngr48gu/F/bmu7IC9ev/oJfXQa5BOk1rtOReeCrOQks4zNcCIIog/A9 ID+g== 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 f203si25044649oib.56.2020.01.01.20.32.06; Wed, 01 Jan 2020 20:32:18 -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 S1727680AbgABE3h (ORCPT + 99 others); Wed, 1 Jan 2020 23:29:37 -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 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 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;