Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp503440ybl; Tue, 7 Jan 2020 09:51:32 -0800 (PST) X-Google-Smtp-Source: APXvYqzvJwo9oGZEXRi7+Ddf5CYKjNp7QhV7rE+bWkxo2kMehMTE+g6CUNU63gDqihnsOAMmKuJe X-Received: by 2002:a9d:7f11:: with SMTP id j17mr988330otq.281.1578419492262; Tue, 07 Jan 2020 09:51:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1578419492; cv=none; d=google.com; s=arc-20160816; b=xnuGKgokjf5jc85Rr24nF7LZO2lQi0t35eGXDmLQF92WxG14lvUrc/wC/QGclYYlgF NhBGpFK9sh3B7ah1hU8BuZueQh/dsekyHCFNZUbxjvHmMxXEgDGE5QvxHOVIACDV1zy+ fLKhZKbyS74sw9Zw2kydL4qGoXvww/GuSUEEFttJrCvQQ8rWgkGAs6OEFkMpHcxau6CV g4y/n0xreuxBHbF2YeVKAIVLQLU0OVT1NRd2nEqyUJps8nM5JuJuPRQ9bt7r/As90lco GFTu0kaPsM3oqxzqojPFKeyQc9XdKAw0B3gBGCybTMNq3zuTQcDUl+HGmpN+MBvJY7AB gd9A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=uyvuS2G9MB9Tvq9pni2gKNsuJduIchYAcY94iemGDHk=; b=BGPWvWwjXtYrX13eSIKTKl8O3Pg4uycHdhter7mDP4JaMRFV9BfWFFFe86JOELou+z vq8sreushbAnzdwiRwl58z0BvjnBOsUphQH2Vb0O+xGBCJYsT3OHAX5ps6p+zOR+iH5P ncKqFip1SuI69xo8xLAiEYIQvHZtz+qt+8wM8kt6j+vHzIEjCfcQ3svBSbIlbTdymu/f 9zMtYkPaAUWewxIT6j9PA6RQevpjf4pHQ9gfOB+uJcaPD+S8Ccn/lhBea/Y27Xd/jc8Q 96trvNPZhLIw0tx3pK1Raqo34xPLDoDvVwW7r75g8BlI92yAvRmbkD/yWf48vz0a13i9 AtgQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=C3P65o55; 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=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l16si262621otj.59.2020.01.07.09.51.19; Tue, 07 Jan 2020 09:51:32 -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=@linaro.org header.s=google header.b=C3P65o55; 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=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728366AbgAGRti (ORCPT + 99 others); Tue, 7 Jan 2020 12:49:38 -0500 Received: from mail-wr1-f67.google.com ([209.85.221.67]:39949 "EHLO mail-wr1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728348AbgAGRth (ORCPT ); Tue, 7 Jan 2020 12:49:37 -0500 Received: by mail-wr1-f67.google.com with SMTP id c14so388824wrn.7 for ; Tue, 07 Jan 2020 09:49:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=uyvuS2G9MB9Tvq9pni2gKNsuJduIchYAcY94iemGDHk=; b=C3P65o55D+SQxxN6mGKZo2HFJ4tjidCXpgNJDyVr+teyCUgHLA/oplEcHIvlFBb7Se GJYJOEPFDLCZpCThR12FScKuecIuQ5bHwfgVDwdIZsF2pB/TLPpqFCH3gm1hJ+Dt3u4Z rNOH1aRZvB+2wrLM/4TuQzAS8xfGkkRDqWOQ305+FYFU8ckBrQt5jRV8s+var7Bh6e+n 0LCwOZYmzj7QNEJw7wyGJSeyXrz7sh0qABAGVmyFDUjkuPkJOocS78vu+NVHD32dRRbO kiVYDdRYm68fFOAehcFimn9Ia23jgmYcV51F7qCCdVuW99cjr3OQd0hEyc5WdRpmw1/r /L8Q== 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=uyvuS2G9MB9Tvq9pni2gKNsuJduIchYAcY94iemGDHk=; b=dld5zPoUZETQUocTu+IqtkUS4jEANhqpDVQqE7UL8U21PoAPLcfnc+RLeuXqyGVODC HwZzNREXvtr8HmUgZLlkZe2c+CP/WfJ0MrlwSFPllh1FihLkWba4ak8f4FBs4uWnssXp jPYzX9XqrO23ZMuyoT6muDKJcX1TqCxrTQBVdZDKo0aAFlcljznVuvL/xzAkiaxqfU+k ZZf9rByis6SzgYRb9XWnz4DJ2A9ScXRrS2U9z0LmCnPXHbx1zSHTm90dQJoJfghN0jOi vYiRRX/MlegXYSDplW2i6t2eAnEvGzR4wUVKa7tUseqqr2/F/kAqvDBuZ5XWNbWmo3iO bpuw== X-Gm-Message-State: APjAAAWPUiaMHlikB5HXrgMZIGtaAldvZTeyBhzR6LFC0NmhXwtpnI0a tWWaVtf/H5mO8gwGsXoXhltlxjm6RFfZb5QcOjw+bA== X-Received: by 2002:a5d:6652:: with SMTP id f18mr275382wrw.246.1578419375184; Tue, 07 Jan 2020 09:49:35 -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> <20200107051822.GB19080@dhcp-128-65.nay.redhat.com> In-Reply-To: <20200107051822.GB19080@dhcp-128-65.nay.redhat.com> From: Ard Biesheuvel Date: Tue, 7 Jan 2020 18:49:24 +0100 Message-ID: Subject: Re: [PATCH v4 3/4] efi: Fix efi_memmap_alloc() leaks To: Dave Young Cc: Dan Williams , Ingo Molnar , Taku Izumi , linux-efi , X86 ML , Linux Kernel Mailing List , Kexec Mailing List Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, 7 Jan 2020 at 06:18, Dave Young wrote: > > 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. > A one line comment to remind our future selves of this discussion would probably be helpful, but beyond that, I don't think we need to do much here.