Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp4308946imm; Mon, 18 Jun 2018 12:39:42 -0700 (PDT) X-Google-Smtp-Source: ADUXVKIadLREJIn/B+mYeykVADjLct+IxfKzSMieXJcUteNAjWeaxZuLHST4l0vJ2A13t0qclDfo X-Received: by 2002:a62:e903:: with SMTP id j3-v6mr14985889pfh.228.1529350782335; Mon, 18 Jun 2018 12:39:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1529350782; cv=none; d=google.com; s=arc-20160816; b=hYEhwX0zTioYMF1u0il4f8wJen4o6ZL6tADgW/D0b4UxO9xtPQNv7RZDshtlpCJZJO u5QveJBtMrXk3Z0rLAZTZsmB41JoTft/QO2iuXb22EhJxPVdX9s2dzuVJ/M5kI1wD9Ma PWs08idr5EcrEAYBllyIc2lmdBk+k1uOmnb7DMq/zjHevNvnAnk1egqa7FPLKFcYzvbI ApccFbgmd0tV+F7y9NLzaUdWEd6kefqv/PlAOEgswPA+Rj9hD2j9f4khx51kuP044eEn 8J4NAazUs0AV61qD8dzdarNsu3IEjsTzNkSnn9Uhe7I3AsutFE0vEubI6GSzfBbsmO4E OmiA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :arc-authentication-results; bh=MxkRpo2wcXwL3YVECalLH4gNGTkoeKhwa2Yd2QxrSvI=; b=d6OTuE8JDG4lQik9rX5YrIacNg5gnjSblkrnZxvXk7VrCnSsqQhDLK1bo+EyTrwpLI rNrTiY0Mr14gPdEkyrm+FcV767blwDyTwMQRg8hE3EqrB+o0IASWrCvoiTF4QRspsLxF heEG8Uq37+Zy6R5P9DxVW5HYFqQTLpaATTPFwq93R5KkFs6HN7y8AYo/sQQL35lBi5rd 90MZ9mdgE8mBokGfLdjFxNtKFDGg+vXrli0J1YvFVSUqazXyBfVaFXLQapH4IUXQN1UJ evNAA+tKVEmfC67qiakT46L/YEbpjwShI1iDXwCU2W+C92tFZkK17d4MlHAfXJz1PN8j NHcw== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id k6-v6si15548861pla.78.2018.06.18.12.39.28; Mon, 18 Jun 2018 12:39:42 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S936330AbeFRThH (ORCPT + 99 others); Mon, 18 Jun 2018 15:37:07 -0400 Received: from mga09.intel.com ([134.134.136.24]:23422 "EHLO mga09.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S935923AbeFRThF (ORCPT ); Mon, 18 Jun 2018 15:37:05 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga102.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 18 Jun 2018 12:37:05 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.51,240,1526367600"; d="scan'208";a="50313564" Received: from sai-dev-mach.sc.intel.com ([143.183.140.145]) by orsmga008.jf.intel.com with ESMTP; 18 Jun 2018 12:37:04 -0700 From: Sai Praneeth Prakhya To: linux-efi@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Sai Praneeth , Lee Chun-Yi , Borislav Petkov , Tony Luck , Dave Hansen , Bhupesh Sharma , Ricardo Neri , Peter Zijlstra , Ravi Shankar , Matt Fleming , Ard Biesheuvel Subject: [PATCH V2] x86/efi: Free allocated memory if remap fails Date: Mon, 18 Jun 2018 12:36:54 -0700 Message-Id: <1529350614-15555-1-git-send-email-sai.praneeth.prakhya@intel.com> X-Mailer: git-send-email 2.7.4 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Sai Praneeth efi_memmap_alloc(), as the name suggests, allocates memory for a new efi memory map. It's referenced from couple of places, namely, efi_arch_mem_reserve() and efi_free_boot_services(). These callers, after allocating memory, remap it for further use. As usual, a routine check is performed to confirm successful remap. If the remap fails, ideally, the allocated memory should be freed but presently we just return without freeing it up. Hence, fix this bug by freeing up the memory appropriately. As efi_memmap_alloc() allocates memory depending on whether mm_init() has already been invoked or not, similarly, while freeing use memblock_free() to free memory allocated before invoking mm_init() and __free_pages() to free memory allocated after invoking mm_init(). It's a fact that memremap() and early_memremap() might never fail and this code might never get a chance to run but to maintain good kernel programming semantics, we might need this patch. Signed-off-by: Sai Praneeth Prakhya Cc: Lee Chun-Yi Cc: Borislav Petkov Cc: Tony Luck Cc: Dave Hansen Cc: Bhupesh Sharma Cc: Ricardo Neri Cc: Peter Zijlstra Cc: Ravi Shankar Cc: Matt Fleming Cc: Ard Biesheuvel --- I found this bug when working on a different patch set which uses efi_memmap_alloc() and then noticed that I never freed the allocated memory. I found it weird, in the sense that, memory is allocated but is not freed (upon returning from an error). So, wasn't sure if that should be treated as a bug or should I just leave it as is because everything works fine even without this patch. Since the effort for the patch is very minimal, I just went ahead and posted one, so that I could know your thoughts on it. Changes from V1 to V2: ---------------------- 1. Fix the bug of freeing memory map that was just installed by correctly calling free_pages(). 2. Call memblock_free() and __free_pages() directly from the appropriate places instead of efi_memmap_free(). Note: Patch based on Linus's mainline tree V4.18-rc1 arch/x86/platform/efi/quirks.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/arch/x86/platform/efi/quirks.c b/arch/x86/platform/efi/quirks.c index 36c1f8b9f7e0..cfa93af97def 100644 --- a/arch/x86/platform/efi/quirks.c +++ b/arch/x86/platform/efi/quirks.c @@ -285,6 +285,7 @@ void __init efi_arch_mem_reserve(phys_addr_t addr, u64 size) new = early_memremap(new_phys, new_size); if (!new) { pr_err("Failed to map new boot services memmap\n"); + memblock_free(new_phys, new_size); return; } @@ -429,6 +430,7 @@ void __init efi_free_boot_services(void) new = memremap(new_phys, new_size, MEMREMAP_WB); if (!new) { pr_err("Failed to map new EFI memmap\n"); + __free_pages(pfn_to_page(PHYS_PFN(new_phys)), get_order(new_size)); return; } @@ -452,6 +454,7 @@ void __init efi_free_boot_services(void) if (efi_memmap_install(new_phys, num_entries)) { pr_err("Could not install new EFI memmap\n"); + __free_pages(pfn_to_page(PHYS_PFN(new_phys)), get_order(new_size)); return; } } -- 2.7.4