Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp4194836ybl; Mon, 13 Jan 2020 09:24:58 -0800 (PST) X-Google-Smtp-Source: APXvYqzP+GlHKD2gKzkW50VPtn+0lrJt4eHc3wgxWzl8fq02W34233u0Sj4oMkpQqhNXjAjW4sVe X-Received: by 2002:a9d:588c:: with SMTP id x12mr13274404otg.2.1578936298749; Mon, 13 Jan 2020 09:24:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1578936298; cv=none; d=google.com; s=arc-20160816; b=Sg4w9ZN/1ylW/fKorfEVGdozrYWBwtyA3kT1JauycWGkHe+mlAW2PjsAXmu0w8HnhB XxC7PNrRO8XxrLfBTfoOLn/WLFnAGHW/8SEHWLSAGQNV1Bt8LLIllzB5165HFqwtAmbX HrYMGNmSU7ZPNgU+ds6d1zivEcZfrSqTYgoD8s47NC6IWD6ynKDdzBM5pg93aUfTE8Vr aW7FAfwXUqr7Klbfv9vihhbN/H2pByMQwnrpn4gb8zEhqtnL0PT9BK7SpU+Kef6saIIm 5d+2wG+f0eVj/KbLq00yKlC2iEvSC2nKJiPKq/lq0/7AhL80YQLqpmB0JCY7K3FWtmJj vUtg== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=vyGa03oHs7vjPax7Bup7bxddEATfFiyGGp7YomybSZM=; b=EsIWzZLnSqnSvlVdvXhWQ2smvYNMPbkT+6LKT8nz8TinV5cQKNjHMsrma/RrAyZdIY KotjCXjD5ZJ32OnAsRIubyVyMiIdrR7Qwi4PthA6RdrquA97bb1+u2r2XmNdcmlXxSBC RcT0WcTU+o2661c5mghJhaZhgoq2vVkwCAmmgsg2sZCkwpPHRjhzMb/+izX9Z95nnmGA 32cL2paXZZw3eadA+pHYKzxzpvzziURZsdK32GYQQzGZrCdug8W6YjReiJxCuMiAqimc v5+Mf5PBd8mcWfcbFqve7XMaES/cMf7NNDsSlq/Bglj9yo0eNnV81wH6mCVkk1eAxExF ff1A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=ZF5bInE9; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id s129si5985094oig.177.2020.01.13.09.24.47; Mon, 13 Jan 2020 09:24:58 -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; dkim=pass header.i=@kernel.org header.s=default header.b=ZF5bInE9; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728957AbgAMRXj (ORCPT + 99 others); Mon, 13 Jan 2020 12:23:39 -0500 Received: from mail.kernel.org ([198.145.29.99]:42752 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728669AbgAMRXe (ORCPT ); Mon, 13 Jan 2020 12:23:34 -0500 Received: from dogfood.home (amontpellier-657-1-18-247.w109-210.abo.wanadoo.fr [109.210.65.247]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 4B9752081E; Mon, 13 Jan 2020 17:23:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578936214; bh=DA2b6/3T3fG/fQwNZi6iqo4jIIatk8iTU5FAbNaWkwE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ZF5bInE9tGhqX8cZA4RaJALH4SbULs0vREQE8OrKu2RwVEivp6mFSGuTVhTtQIs4Z HHMX5tIHpUi86qLkW1CL39vp4DwkYEpa6U1OpeCj04twnVtIIeIe+ypm3122tWB9jO k0YMVJ6ke1lGEJTg4jfZlmR78mJY+2mMuaUaZwT8= From: Ard Biesheuvel To: linux-efi@vger.kernel.org, Ingo Molnar , Thomas Gleixner Cc: Ard Biesheuvel , linux-kernel@vger.kernel.org, Anshuman Khandual , Arnd Bergmann , Dan Williams , Dave Young , Saravana Kannan Subject: [PATCH 12/13] efi: Fix efi_memmap_alloc() leaks Date: Mon, 13 Jan 2020 18:22:44 +0100 Message-Id: <20200113172245.27925-13-ardb@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200113172245.27925-1-ardb@kernel.org> References: <20200113172245.27925-1-ardb@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Dan Williams 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=. [ ardb: added a comment to clarify that efi_memmap_free() does nothing when called from efi_clean_memmap(), i.e., with data->flags == 0x0 ] Signed-off-by: Dan Williams Signed-off-by: Ard Biesheuvel --- drivers/firmware/efi/memmap.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/drivers/firmware/efi/memmap.c b/drivers/firmware/efi/memmap.c index 04dfa56b994b..501672166502 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. @@ -100,6 +122,9 @@ static int __init __efi_memmap_init(struct efi_memory_map_data *data) return -ENOMEM; } + /* NOP if data->flags & (EFI_MEMMAP_MEMBLOCK | EFI_MEMMAP_SLAB) == 0 */ + efi_memmap_free(); + map.phys_map = data->phys_map; map.nr_map = data->size / data->desc_size; map.map_end = map.map + data->size; -- 2.20.1