From patchwork Tue Aug 29 18:55:31 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Graf X-Patchwork-Id: 111241 Delivered-To: patch@linaro.org Received: by 10.140.95.112 with SMTP id h103csp1785993qge; Tue, 29 Aug 2017 11:56:38 -0700 (PDT) X-Received: by 10.200.52.214 with SMTP id x22mr6933328qtb.272.1504032998729; Tue, 29 Aug 2017 11:56:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1504032998; cv=none; d=google.com; s=arc-20160816; b=DoiXFDiZo1vEzOxpkHjBwgwQmLgK082z/Gm1yr1jNNchyx/+jAfB9JnbQijZ6HnATC GYMJI2YgnkIsScs8AJ8Qr8hHuQ9RsU9OGe6sggE30z63uXkv9s2P9YRG+mAsAebAbXls jADexl3sn1i+JgSRjnh6h3eiIo5DO3lZC95dhTsGEIeqm934fMjzNoJV+PWo8uVA4j7g PFAFcFCAk8Zlj6KnIy4T7Qq2LJxr1iWpf8Vjc3JGZA6phRrlpkktrjmQXfEwfWh1lnpd Q9TtyIAznlJu0V5uScA9nixfo8wclVs7OAgLrjWOKgiZpXYsgOh/0pRLWxxSm3awAEEN 9LhQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:references:in-reply-to:message-id:date:subject :to:from:arc-authentication-results; bh=XkMT9Jr2KFJKBAKTt1/0xXM4nrp88i4Lzty76K8PyoY=; b=GL6SHrvNAds9S09EiRegubootu8i0RRbqX+WnamJj0pQ8z4yzoD6uAWLY/eL1GlJWO RaGBrJXJUNJCmZ52oDGTeqU0u/lwnR/Ck/2TNxEvthAg6Syr+3mh33CLwcr0eK6zk9oZ v8uy5WxSLTfSkrDR9+CfY43FBYyUiIqzvzLPiWGR1CRNwKluhwFz1fgiVEyxgxai5/mK yYply+ciHPvIomQ1VISdxfeJncdSAR7X3DoeQvD9GS4u3GoRoLQ0f1W+7FZdaMSqhZAh dei2MibLQgXYXTrNkHkJRXfIifAml2CskCMu805xZBu+u/aU0B0J9PaSGu8JsvoSKdsl T57Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of grub-devel-bounces+patch=linaro.org@gnu.org designates 2001:4830:134:3::11 as permitted sender) smtp.mailfrom=grub-devel-bounces+patch=linaro.org@gnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [2001:4830:134:3::11]) by mx.google.com with ESMTPS id l125si1181087qkf.173.2017.08.29.11.56.38 for (version=TLS1 cipher=AES128-SHA bits=128/128); Tue, 29 Aug 2017 11:56:38 -0700 (PDT) Received-SPF: pass (google.com: domain of grub-devel-bounces+patch=linaro.org@gnu.org designates 2001:4830:134:3::11 as permitted sender) client-ip=2001:4830:134:3::11; Authentication-Results: mx.google.com; spf=pass (google.com: domain of grub-devel-bounces+patch=linaro.org@gnu.org designates 2001:4830:134:3::11 as permitted sender) smtp.mailfrom=grub-devel-bounces+patch=linaro.org@gnu.org Received: from localhost ([::1]:46544 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dmlgi-0006o3-Ot for patch@linaro.org; Tue, 29 Aug 2017 14:56:36 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:53519) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dmlfm-00063z-DR for grub-devel@gnu.org; Tue, 29 Aug 2017 14:55:39 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dmlfj-00022z-Ax for grub-devel@gnu.org; Tue, 29 Aug 2017 14:55:38 -0400 Received: from mx2.suse.de ([195.135.220.15]:48902 helo=mx1.suse.de) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dmlfi-000223-Oq for grub-devel@gnu.org; Tue, 29 Aug 2017 14:55:35 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay1.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 3AFFBAF4F; Tue, 29 Aug 2017 18:55:33 +0000 (UTC) From: Alexander Graf To: grub-devel@gnu.org Subject: [PATCH v4 1/2] efi: Move grub_reboot() into kernel Date: Tue, 29 Aug 2017 20:55:31 +0200 Message-Id: <20170829185532.48943-2-agraf@suse.de> X-Mailer: git-send-email 2.12.3 In-Reply-To: <20170829185532.48943-1-agraf@suse.de> References: <20170829185532.48943-1-agraf@suse.de> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x (no timestamps) [generic] [fuzzy] X-Received-From: 195.135.220.15 X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: The development of GNU GRUB Cc: Leif Lindholm , phcoder@gmail.com, mchang@suse.com, Daniel Kiper , elliott@hpe.com MIME-Version: 1.0 Errors-To: grub-devel-bounces+patch=linaro.org@gnu.org Sender: "Grub-devel" The reboot function calls machine_fini() and then reboots the system. Currently it lives in lib/ which means it gets compiled into the reboot module which lives on the heap. In a following patch, I want to free the heap on machine_fini() though, so we would free the memory that the code is running in. That obviously breaks with smarter UEFI implementations. So this patch moves it into the core. That way we ensure that all code running after machine_fini() in the UEFI case is running from memory that got allocated (and gets deallocated) by the UEFI core. Signed-off-by: Alexander Graf --- v3 -> v4: - Move grub_reboot to kern/efi/efi.c --- grub-core/Makefile.core.def | 4 ---- grub-core/kern/efi/efi.c | 9 +++++++++ grub-core/lib/efi/reboot.c | 33 --------------------------------- include/grub/misc.h | 3 ++- 4 files changed, 11 insertions(+), 38 deletions(-) delete mode 100644 grub-core/lib/efi/reboot.c -- 2.12.3 _______________________________________________ Grub-devel mailing list Grub-devel@gnu.org https://lists.gnu.org/mailman/listinfo/grub-devel diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def index a65c27f7f..1cc4ce374 100644 --- a/grub-core/Makefile.core.def +++ b/grub-core/Makefile.core.def @@ -872,10 +872,6 @@ module = { name = reboot; i386 = lib/i386/reboot.c; i386 = lib/i386/reboot_trampoline.S; - ia64_efi = lib/efi/reboot.c; - x86_64_efi = lib/efi/reboot.c; - arm_efi = lib/efi/reboot.c; - arm64_efi = lib/efi/reboot.c; powerpc_ieee1275 = lib/ieee1275/reboot.c; sparc64_ieee1275 = lib/ieee1275/reboot.c; mips_arc = lib/mips/arc/reboot.c; diff --git a/grub-core/kern/efi/efi.c b/grub-core/kern/efi/efi.c index d467785fc..708581fcb 100644 --- a/grub-core/kern/efi/efi.c +++ b/grub-core/kern/efi/efi.c @@ -155,6 +155,15 @@ grub_efi_get_loaded_image (grub_efi_handle_t image_handle) } void +grub_reboot (void) +{ + grub_machine_fini (GRUB_LOADER_FLAG_NORETURN); + efi_call_4 (grub_efi_system_table->runtime_services->reset_system, + GRUB_EFI_RESET_COLD, GRUB_EFI_SUCCESS, 0, NULL); + for (;;) ; +} + +void grub_exit (void) { grub_machine_fini (GRUB_LOADER_FLAG_NORETURN); diff --git a/grub-core/lib/efi/reboot.c b/grub-core/lib/efi/reboot.c deleted file mode 100644 index 7de8bcb5d..000000000 --- a/grub-core/lib/efi/reboot.c +++ /dev/null @@ -1,33 +0,0 @@ -/* - * GRUB -- GRand Unified Bootloader - * Copyright (C) 2011 Free Software Foundation, Inc. - * - * GRUB is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * GRUB is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with GRUB. If not, see . - */ - -#include -#include -#include -#include -#include -#include - -void -grub_reboot (void) -{ - grub_machine_fini (GRUB_LOADER_FLAG_NORETURN); - efi_call_4 (grub_efi_system_table->runtime_services->reset_system, - GRUB_EFI_RESET_COLD, GRUB_EFI_SUCCESS, 0, NULL); - for (;;) ; -} diff --git a/include/grub/misc.h b/include/grub/misc.h index 2a9f87cc2..372f009e8 100644 --- a/include/grub/misc.h +++ b/include/grub/misc.h @@ -396,7 +396,8 @@ grub_abs (int x) } /* Reboot the machine. */ -#if defined (GRUB_MACHINE_EMU) || defined (GRUB_MACHINE_QEMU_MIPS) +#if defined (GRUB_MACHINE_EMU) || defined (GRUB_MACHINE_QEMU_MIPS) || \ + defined (GRUB_MACHINE_EFI) void EXPORT_FUNC(grub_reboot) (void) __attribute__ ((noreturn)); #else void grub_reboot (void) __attribute__ ((noreturn)); From patchwork Tue Aug 29 18:55:32 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Graf X-Patchwork-Id: 111240 Delivered-To: patch@linaro.org Received: by 10.140.95.112 with SMTP id h103csp1785912qge; Tue, 29 Aug 2017 11:56:32 -0700 (PDT) X-Received: by 10.200.55.130 with SMTP id d2mr7375210qtc.65.1504032992806; Tue, 29 Aug 2017 11:56:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1504032992; cv=none; d=google.com; s=arc-20160816; b=ib494NFrcck/BZSVBURTG8XX7EG12nE/E2oEceIy/IUFWldWV6mZsejYPBZzYSRGKE j3jA95jj4tRYQlXFYcRZU2UHaQ9sFZNcqooAG1+3IfkARPLludY3bfEuaWhh14eSZAsP ENVgaRxqUK+l5M0akBxF8PdQfy8ISI7KhR2HtkJobGJdgNxtwFgTEJ93q99VoIUHfIqG gc+4ff6Bb5+X9EWGNPYlGxpjN/xhO9BRfKrP1+7dK7fLbEwHpqYS32pcKRYaqpfrvB+/ sEioTq2zOWF4G8a3Mpu5HEeGSLQE4jVfNC54l21BkWWNphwAkY81euJZMYfl8tQ9IA8p KxPQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:references:in-reply-to:message-id:date:subject :to:from:arc-authentication-results; bh=SjhPOx2UYZmqLUfeOvSlHJ0nPYYpNalQOPIYJSqCbSo=; b=A8876NyL33MoGu+7hhpIDWCMUk+eCQz3l8nyoG1mzXrkqGbI5TTlUSZ7ONb84g33M/ SmqdU1JyFkOrYOr6WF+HLPDCsFv1GB2oj5ztI39uVN4n27xTILW1DvRx188shrV5EARy x0c+7kTwsXE7jjWBhmgNRcYGg2Y+K3ge7jHnbTVBVXPy5q8NYfwmM6pd/HLJIAKSjIpS DrW/+iSHJhp3xB1EwBfgq5Plx8Ku05PUB8Yokg7ap5BGJiz40sGg9LalqB+2PB5aJJyr TGqKfz/TGju07FBGP1+2AnNpfyyQFLLOOQ3yGNJEG9t2RjsWQgrYctGWFc803bHukZqg TSRw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of grub-devel-bounces+patch=linaro.org@gnu.org designates 2001:4830:134:3::11 as permitted sender) smtp.mailfrom=grub-devel-bounces+patch=linaro.org@gnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [2001:4830:134:3::11]) by mx.google.com with ESMTPS id d11si3449583qka.339.2017.08.29.11.56.32 for (version=TLS1 cipher=AES128-SHA bits=128/128); Tue, 29 Aug 2017 11:56:32 -0700 (PDT) Received-SPF: pass (google.com: domain of grub-devel-bounces+patch=linaro.org@gnu.org designates 2001:4830:134:3::11 as permitted sender) client-ip=2001:4830:134:3::11; Authentication-Results: mx.google.com; spf=pass (google.com: domain of grub-devel-bounces+patch=linaro.org@gnu.org designates 2001:4830:134:3::11 as permitted sender) smtp.mailfrom=grub-devel-bounces+patch=linaro.org@gnu.org Received: from localhost ([::1]:46543 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dmlgc-00066W-Pq for patch@linaro.org; Tue, 29 Aug 2017 14:56:30 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:53517) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dmlfm-00063y-Co for grub-devel@gnu.org; Tue, 29 Aug 2017 14:55:39 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dmlfj-00022u-AQ for grub-devel@gnu.org; Tue, 29 Aug 2017 14:55:38 -0400 Received: from mx2.suse.de ([195.135.220.15]:48903 helo=mx1.suse.de) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dmlfi-000222-Ov for grub-devel@gnu.org; Tue, 29 Aug 2017 14:55:35 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 47B22AF51; Tue, 29 Aug 2017 18:55:33 +0000 (UTC) From: Alexander Graf To: grub-devel@gnu.org Subject: [PATCH v4 2/2] efi: Free malloc regions on exit Date: Tue, 29 Aug 2017 20:55:32 +0200 Message-Id: <20170829185532.48943-3-agraf@suse.de> X-Mailer: git-send-email 2.12.3 In-Reply-To: <20170829185532.48943-1-agraf@suse.de> References: <20170829185532.48943-1-agraf@suse.de> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x (no timestamps) [generic] [fuzzy] X-Received-From: 195.135.220.15 X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: The development of GNU GRUB Cc: Leif Lindholm , phcoder@gmail.com, mchang@suse.com, Daniel Kiper , elliott@hpe.com MIME-Version: 1.0 Errors-To: grub-devel-bounces+patch=linaro.org@gnu.org Sender: "Grub-devel" When we exit grub, we don't free all the memory that we allocated earlier for our heap region. This can cause problems with setups where you try to descend the boot order using "exit" entries, such as PXE -> HD boot scenarios. Signed-off-by: Alexander Graf --- v1 -> v2: - add comment explaining the number of regions - move nr of regions into a define - add warning if we exceed the number of freeable regions - reset region counter to 0 on fini v2 -> v3: - use dynamic list instead of static array at runtime - use allocate_pool for list, so we are not bound by heap or random numbers - remember all allocations, not just the heap --- grub-core/kern/efi/init.c | 1 + grub-core/kern/efi/mm.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++ include/grub/efi/efi.h | 1 + 3 files changed, 67 insertions(+) -- 2.12.3 _______________________________________________ Grub-devel mailing list Grub-devel@gnu.org https://lists.gnu.org/mailman/listinfo/grub-devel diff --git a/grub-core/kern/efi/init.c b/grub-core/kern/efi/init.c index 2c31847bf..3dfdf2d22 100644 --- a/grub-core/kern/efi/init.c +++ b/grub-core/kern/efi/init.c @@ -80,4 +80,5 @@ grub_efi_fini (void) { grub_efidisk_fini (); grub_console_fini (); + grub_efi_memory_fini (); } diff --git a/grub-core/kern/efi/mm.c b/grub-core/kern/efi/mm.c index ac2a4c556..63c660eba 100644 --- a/grub-core/kern/efi/mm.c +++ b/grub-core/kern/efi/mm.c @@ -49,6 +49,41 @@ static grub_efi_uintn_t finish_desc_size; static grub_efi_uint32_t finish_desc_version; int grub_efi_is_finished = 0; +/* + * We need to roll back EFI allocations on exit. Remember allocations that + * we'll free on exit. In most cases only one will be used. + */ +struct efi_allocation; +struct efi_allocation { + struct efi_allocation *next; + grub_efi_physical_address_t start_addr; + grub_efi_uint64_t pages; +}; +static struct efi_allocation *efi_allocated_memory; + +static void +grub_efi_remember_pages (grub_efi_physical_address_t address, + grub_efi_uintn_t pages) +{ + grub_efi_boot_services_t *b; + struct efi_allocation *alloc; + grub_efi_status_t status; + + b = grub_efi_system_table->boot_services; + status = efi_call_3 (b->allocate_pool, GRUB_EFI_LOADER_DATA, + sizeof(*alloc), (void**)&alloc); + if (status == GRUB_EFI_SUCCESS) + { + alloc->next = efi_allocated_memory; + alloc->start_addr = address; + alloc->pages = pages; + efi_allocated_memory = alloc; + } + else + grub_printf ("Could not malloc memory to remember EFI allocation. " + "Exiting grub2 won't free all memory.\n"); +} + /* Allocate pages. Return the pointer to the first of allocated pages. */ void * grub_efi_allocate_pages_real (grub_efi_physical_address_t address, @@ -79,6 +114,7 @@ grub_efi_allocate_pages_real (grub_efi_physical_address_t address, return 0; } + grub_efi_remember_pages (address, pages); return (void *) ((grub_addr_t) address); } @@ -422,6 +458,35 @@ add_memory_regions (grub_efi_memory_descriptor_t *memory_map, grub_fatal ("too little memory"); } +void +grub_efi_memory_fini (void) +{ + struct efi_allocation *alloc; + grub_efi_boot_services_t *b; + + b = grub_efi_system_table->boot_services; + + for (alloc = efi_allocated_memory; alloc;) + { + struct efi_allocation *next; + + /* Free the allocation */ + grub_efi_free_pages (alloc->start_addr, + alloc->pages); + + /* Remember the next entry */ + next = alloc->next; + + /* Free the current list entry */ + efi_call_1 (b->free_pool, alloc); + + /* And move on to the next */ + alloc = next; + } + + efi_allocated_memory = NULL; +} + #if 0 /* Print the memory map. */ static void diff --git a/include/grub/efi/efi.h b/include/grub/efi/efi.h index 3fa082816..c996913e5 100644 --- a/include/grub/efi/efi.h +++ b/include/grub/efi/efi.h @@ -55,6 +55,7 @@ EXPORT_FUNC(grub_efi_get_memory_map) (grub_efi_uintn_t *memory_map_size, grub_efi_uintn_t *map_key, grub_efi_uintn_t *descriptor_size, grub_efi_uint32_t *descriptor_version); +void grub_efi_memory_fini (void); grub_efi_loaded_image_t *EXPORT_FUNC(grub_efi_get_loaded_image) (grub_efi_handle_t image_handle); void EXPORT_FUNC(grub_efi_print_device_path) (grub_efi_device_path_t *dp); char *EXPORT_FUNC(grub_efi_get_filename) (grub_efi_device_path_t *dp);