From mboxrd@z Thu Jan 1 00:00:00 1970 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=DKIM_SIGNED,DKIM_VALID, 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 38C18C33C99 for ; Tue, 7 Jan 2020 04:24:45 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 02829206F0 for ; Tue, 7 Jan 2020 04:24:44 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=intel-com.20150623.gappssmtp.com header.i=@intel-com.20150623.gappssmtp.com header.b="hMwCZTFo" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727563AbgAGEYo (ORCPT ); Mon, 6 Jan 2020 23:24:44 -0500 Received: from mail-ot1-f68.google.com ([209.85.210.68]:33415 "EHLO mail-ot1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727464AbgAGEYo (ORCPT ); Mon, 6 Jan 2020 23:24:44 -0500 Received: by mail-ot1-f68.google.com with SMTP id b18so52852858otp.0 for ; Mon, 06 Jan 2020 20:24:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel-com.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=rpRVgkQrP+H8glvNMQeoSS2IHkaJmEZmOMYgHcv4/Sk=; b=hMwCZTFotL92VhgJnQOx+5zKXeoHFoSDwxyQeDkGA7LL1xJ/5I1qifpHD05tGEJ/k1 D81lbVKvMM5wqPFXLh61rQyjbKeBSH5Fvnn0a/5n9Yj/TpAk3Pj0GP1I+sfNM0VmwXVH T9SVgNr3TPjwmBBxcbMTev/tuMSNk+LWI0YsAS4v1EPSUGxuxQo/3h+UYuZlAUBRmir+ cilRI/wWpV+b0C/e78M1pw8nlm8AZ/iKLvy3lHo3u1ZPcZLcPhj36PonE0fXHE5FebIw 1H04+KlDDlAkwaMukRX/ufRxoF1bmBJoRoWyjdZAxuB28KEJGfld5brUc0sgJ20yz7fV NLlg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=rpRVgkQrP+H8glvNMQeoSS2IHkaJmEZmOMYgHcv4/Sk=; b=iMgDna71500W3vrkES/Bk8XSPzoExmY+GHxbqsCeo8Kg1LHilnwKQzQCTV3e8Q0yDM IIRu8vQDSpYUoSQtpB8AzkStNZX2camBIKI0SQwE4m15eZPgpiwv6HY+EhcoNYmdEcFL m0TKupiCIwE8wXSfMDgBemabye16vPvYRkNA5AoJBUoGML8NyiMDZjZP4rZhEWm9C836 pZVJvJ01R1lTxocmQFBmwVcEyInxVi4vHB2LPchix//1xj3piTgwEJIx7Rpz6SQFq4s9 MKIk/BtQjxl9+p/37qOhj4UBwqidL3ZG1iyfupiAorwdWkgWqrY8dhxsu0wrVl3/Jm1Y ojQw== X-Gm-Message-State: APjAAAXz1KkfEaFNNAUG+mC7AVxleXItFVLdRns0uSrYhM5iLF9G3mxS BFd7cDLApeohE8DG/HjuvcYlaDd77gjiNpARwxGNTg== X-Google-Smtp-Source: APXvYqzf1d2T/uMauIo2ZeG7AJCoT0+lduA5vctbRQQqcUAxoautUmiDkuGdp3E5eiwrTJn7bTCIku50x+CJ+lwCShE= X-Received: by 2002:a9d:4e99:: with SMTP id v25mr123180121otk.363.1578371083582; Mon, 06 Jan 2020 20:24:43 -0800 (PST) MIME-Version: 1.0 References: <157835762222.1456824.290100196815539830.stgit@dwillia2-desk3.amr.corp.intel.com> <157835763783.1456824.4013634516855823659.stgit@dwillia2-desk3.amr.corp.intel.com> <20200107035824.GA19080@dhcp-128-65.nay.redhat.com> In-Reply-To: <20200107035824.GA19080@dhcp-128-65.nay.redhat.com> From: Dan Williams Date: Mon, 6 Jan 2020 20:24:32 -0800 Message-ID: Subject: Re: [PATCH v4 3/4] efi: Fix efi_memmap_alloc() leaks To: Dave Young Cc: Ingo Molnar , Taku Izumi , Ard Biesheuvel , linux-efi , X86 ML , Linux Kernel Mailing List , Kexec Mailing List Content-Type: text/plain; charset="UTF-8" Sender: linux-efi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-efi@vger.kernel.org On Mon, Jan 6, 2020 at 7:58 PM Dave Young wrote: > > On 01/06/20 at 04:40pm, Dan Williams wrote: > > 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 04dfa56b994b..bffa320d2f9a 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,8 @@ static int __init __efi_memmap_init(struct efi_memory_map_data *data) > > return -ENOMEM; > > } > > > > + efi_memmap_free(); > > + > > This seems still not safe, see below function: > arch/x86/platform/efi/efi.c: > static void __init efi_clean_memmap(void) > It use same memmap for both old and new, and filter out those invalid > ranges in place, if the memory is freed then .. In the efi_clean_memmap() case flags are 0, so efi_memmap_free() is a nop. Would you feel better with an explicit? WARN_ON(efi.memmap.phys_map == data->phys_map && (data->flags & (EFI_MEMMAP_SLAB | EFI_MEMMAP_MEMBLOCK)) ...not sure it's worth it. > > > map.phys_map = data->phys_map; > > map.nr_map = data->size / data->desc_size; > > map.map_end = map.map + data->size; > >