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=-8.4 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 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 B65E5C33C99 for ; Tue, 7 Jan 2020 05:18:36 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 872FF2080A for ; Tue, 7 Jan 2020 05:18:36 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="EoOGQWVC" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725801AbgAGFSg (ORCPT ); Tue, 7 Jan 2020 00:18:36 -0500 Received: from us-smtp-2.mimecast.com ([205.139.110.61]:24120 "EHLO us-smtp-delivery-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1725781AbgAGFSf (ORCPT ); Tue, 7 Jan 2020 00:18:35 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1578374314; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=e+pMdW3YlNEjXyRDuZ2IYPHP1swSW2oY5Q3WONSeYK4=; b=EoOGQWVC8ehdBW6pr9d9vbdetnikhy3qtuf5XsDkjk+HStpOKSDym4XzOLCo7DvUesiJsC uZnmpKuGvWqNzrosSkkStqs+CCDbKJHHfrf/H9NWNdB5IpTwgSXsYJOxhNTWGSsw+SqUhv GqmMT6LlOBi+xIMPANZh9qOXttavfTg= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-98-FJfdGUOEPeCfwn242bSx9A-1; Tue, 07 Jan 2020 00:18:31 -0500 X-MC-Unique: FJfdGUOEPeCfwn242bSx9A-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 3AF4A1800D4E; Tue, 7 Jan 2020 05:18:30 +0000 (UTC) Received: from dhcp-128-65.nay.redhat.com (ovpn-12-32.pek2.redhat.com [10.72.12.32]) by smtp.corp.redhat.com (Postfix) with ESMTPS id E7F541036D1B; Tue, 7 Jan 2020 05:18:26 +0000 (UTC) Date: Tue, 7 Jan 2020 13:18:22 +0800 From: Dave Young To: Dan Williams Cc: Ingo Molnar , Taku Izumi , Ard Biesheuvel , linux-efi , X86 ML , Linux Kernel Mailing List , Kexec Mailing List Subject: Re: [PATCH v4 3/4] efi: Fix efi_memmap_alloc() leaks Message-ID: <20200107051822.GB19080@dhcp-128-65.nay.redhat.com> 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> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.12.1 (2019-06-15) X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 Sender: linux-efi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-efi@vger.kernel.org On 01/06/20 at 08:24pm, Dan Williams wrote: > 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. Ah, yes, sorry I did not see the flags, although it is not very obvious. Maybe add some code comment for efi_mem_alloc and efi_mem_init. Let's defer the suggestion to Ard. Thanks Dave