Received: by 2002:ac0:a581:0:0:0:0:0 with SMTP id m1-v6csp6003979imm; Wed, 27 Jun 2018 00:02:50 -0700 (PDT) X-Google-Smtp-Source: ADUXVKI0ksZqb9UcKirOwahgmH94otHJ+dpmH5tUOlT3chUmS7+PxmK+M+q628lcczB+wl/KCF8X X-Received: by 2002:a65:6004:: with SMTP id m4-v6mr4126587pgu.124.1530082970293; Wed, 27 Jun 2018 00:02:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1530082970; cv=none; d=google.com; s=arc-20160816; b=q769VT1xIlSVP1qjphRZX6NrfwTzUwHJcFXle3PwzVinAm0huzRmDxQH3D/Y9BzptT jqLae/u+XCfQPeGMBAJxJpAs/RNkIzN1xSG8uOY8g2Ti6Q+RWdvDX6gV2OJwU8fu6zcI TaYKx0WgLVilclmx3JkhS31U1ldZj8K2XiWviMzVv1lZkBwX+imJkCNO/ciomppMFvbL IssBEwornLi7SJ97dnAWXhgEzyNRC1xzzxcLwgSxB7Q1+vXguPiezRSK0suFz4uPpi3f dfTafrftW5Qwhxh8WKAfvuzmaujU07jov9uAjxE77j6IudZIN7l194cTs7AvenI8NWjL CqkA== 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 :references:in-reply-to:mime-version:dkim-signature :arc-authentication-results; bh=ytBViXbuLurIjrZHtEojwVnAu8MIq3NsR3KncRuinP8=; b=TcONavnuV5ErNNFOxrE8UsLzARPdhYlZJnLBYirNoorDn7TmACtleiQOo4678Dqj8e jDr1+nMJE5S/KKsE2CkMSof/AeymhrL9XscV2j/FI7mfG+0qhIlt3XxGZ4s+woKk5kUR HZMVS7Ayk6ro8o/5hCepfBBl8EueCShxQSOoayyPlK1QGAi/zf0bwaTIPNhMa0zKn2w/ Y+herWvbNBgpeJYvHqy8aOasagFwESN4LY48jYyqWTK+/5kyOO6z9aWFOQq9hKa2kS0H 6VKuui1eKY2++Wvyozz8vR1EbBwMU6hJGlWoEX4Zi2s6HZRrF10P0NFdMyq0fYvAB6ug +l5A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=gV3RRtIv; 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 f13-v6si2831906pgv.374.2018.06.27.00.02.36; Wed, 27 Jun 2018 00:02:50 -0700 (PDT) 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=gV3RRtIv; 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 S1752423AbeF0HBy (ORCPT + 99 others); Wed, 27 Jun 2018 03:01:54 -0400 Received: from mail-it0-f68.google.com ([209.85.214.68]:50641 "EHLO mail-it0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752039AbeF0HBw (ORCPT ); Wed, 27 Jun 2018 03:01:52 -0400 Received: by mail-it0-f68.google.com with SMTP id u4-v6so5968022itg.0 for ; Wed, 27 Jun 2018 00:01:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=ytBViXbuLurIjrZHtEojwVnAu8MIq3NsR3KncRuinP8=; b=gV3RRtIvHWhHvGINuqdaltSaURneaJdU3b7k3mbE1BHZo4grKBnZOE0eNrGpQIM8O7 Tbr9+pPzprgJDCCqD9runFCCBxXrpFywn3jV9hjPAiEBFhPT5vkMcudAM4OpdgYvCznc P3mYTh4qM50BCrbqTXmNu7pZY5two9Z8CFFVk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=ytBViXbuLurIjrZHtEojwVnAu8MIq3NsR3KncRuinP8=; b=lxUVyLxydc8Fx6Q8SMKecm/mSvFxPViSsuoNnARfp3oSCMVw0QNi/jawvEJqQm1bT6 J3RJZsKw/vq4VyCbw1rSnuAWfHBAeiNbYkX5FiqVCPPlMnZpGoOKPRceEtRKXtZnBLM6 BJp488LSj6jBBXtXJqmGfImiv8sy6R54BZv6NsOD5Iy2GZUgCqd+HLcKtt7OrkxMIQip tlZIMKViXKVehWzdAPdRytJRYVpQUMs4M/50i2TgNmq/7EJsEGTAMx2Y4Dk7FRIjDn4w MG+DXN00OHhTBQ+7TTjIIxVRf+2KT3Ec2yff9yu3Ew5NIzg8DOa5a4R+tEqUq2LFwhUn hFdQ== X-Gm-Message-State: APt69E0Z8BGG3q4PLR1yKgdpyp5z/V3YA5crHtABEe2maPjBfBWe8ZhP dojIwqZG3uWlomhXYPFdBGaGRm4nXmKKd2GtZOEgIA== X-Received: by 2002:a02:2422:: with SMTP id f34-v6mr3986500jaa.2.1530082912108; Wed, 27 Jun 2018 00:01:52 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:a6b:bbc7:0:0:0:0:0 with HTTP; Wed, 27 Jun 2018 00:01:51 -0700 (PDT) In-Reply-To: References: <1529980892-11833-1-git-send-email-sai.praneeth.prakhya@intel.com> From: Ard Biesheuvel Date: Wed, 27 Jun 2018 09:01:51 +0200 Message-ID: Subject: Re: [PATCH] efi: Free existing memory map before installing new memory map To: "Prakhya, Sai Praneeth" Cc: linux-efi , Linux Kernel Mailing List , Lee Chun-Yi , Borislav Petkov , Dave Young , Laszlo Ersek , Bhupesh Sharma , "Neri, Ricardo" , "Shankar, Ravi V" , Matt Fleming 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 27 June 2018 at 06:51, Prakhya, Sai Praneeth wrote: >> > + /* Free the memory allocated to the existing memory map */ >> > + efi_memmap_free(efi.memmap.phys_map, efi.memmap.nr_map, >> > + efi.memmap.late); >> > + >> > data.phys_map = addr; >> > data.size = efi.memmap.desc_size * nr_map; >> > data.desc_version = efi.memmap.desc_version; >> > -- >> > 2.7.4 >> > >> >> If only it were so simple :-) >> >> At this point, efi.memmap.phys_map could point to memory that was allocated >> early, allocated late or simply passed to the OS at boot time by the stub (in >> which case it was memblock_reserve()d but not memblock_alloc()d, and it >> should not be freed) >> > > Yes, completely agree that there could be three types of allocations for memmap. > I thought, > efi_memmap_free(efi.memmap.phys_map, efi.memmap.nr_map, efi.memmap.late); > > should work because the previous type of allocation should have been recorded in efi.memmap.late. > But, now I see this will fail for memblock_reserved() memory because it will be mistaken to > memblock_alloced() (I assumed both are almost similar :(). > >> So only allocations made with efi_memmap_alloc() should be freed here. > > Makes sense and I think that also means efi_memmap_free() should be called from function > that called efi_memmap_alloc() and not efi_memmap_install(). > >> I'm not sure /how/ we should keep track of that: perhaps it is simply a matter of >> replacing the boolean 'late' with an enum that describes where the memory >> came from that phys_map points to. > > I did try changing boolean late to enum and it seems to be working fine. I will do more > testing/clean up and will submit a patch for review. > > Also, could you please clarify if there is any specific reason why memory allocated > using memblock_reserve() shouldn't be freed. I mean, not with memblock_free() but I > think we could make it _available_ using free_bootmem() (or something similar, please > correct me if this is not the right API). On arm64, the memory map is provided to the core kernel by the stub, and after kexec, a pointer to the same memory map will be passed to the next kernel. So the kernel does not 'own' that allocation, and it should not free it or overwrite it. > If we allocate and install a new memory map (as > in case with efi_fake_memmap()), I think we should free the memory used by memory map > originally passed by EFI stub, because, at any point of time there should only be one active > memory map. If we don't free the original memory map passed by EFI stub, we will be having > two and hence will be leaking memory. > > Regards, > Sai