From patchwork Wed Mar 15 09:59:36 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AKASHI Takahiro X-Patchwork-Id: 9625147 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 1589960244 for ; Wed, 15 Mar 2017 09:59:07 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id EEBA128304 for ; Wed, 15 Mar 2017 09:59:06 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E1F5C283BA; Wed, 15 Mar 2017 09:59:06 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-1.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 4FF7328304 for ; Wed, 15 Mar 2017 09:59:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=IxutuSeNXJgRxG3nx1G9aUlDaL38JEiSu0ekhfxu7vk=; b=syngiDDUCyxPv+VLWMkrDZsSlw UgXgreDBufBzoywG0SHM2pKrYSBp+jHExJcQssy95L5OiVocFXzSdcsQuKzWZWNg4pWScOST8DtwW hvqyeg7hSmboJjHUpC99yu04e5rqzdGB0xidsY9pPlsFzvL/i8Eul97K72X/gGpYYrJn4QYHHrltk pjxIV4n7oe88t+jfQ32peHimMelbpxVWDN5ROmG6iM/pVpscS9L8me1yLHVdU4Aa0xuDPGmHx8Qlq 64RReAJD0ADMGkeejjFheIbw8eEVZrlyoHjjRopY1OIfS4NrLDrK+dvEqrf74DJhoyRUloI530Prz 2k2BWpEA==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1co5hx-00053J-K4; Wed, 15 Mar 2017 09:59:05 +0000 Received: from mail-pg0-x231.google.com ([2607:f8b0:400e:c05::231]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1co5gl-0003R2-QB for linux-arm-kernel@lists.infradead.org; Wed, 15 Mar 2017 09:57:55 +0000 Received: by mail-pg0-x231.google.com with SMTP id g2so6982838pge.3 for ; Wed, 15 Mar 2017 02:57:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=DT1IWhndUDcAeaHGI3QXGOzxpZ3skyIR/Y2AcpqIdvA=; b=MdLqBpqx972EDxapF22oFMzEiZkB4hdDZKF3j6mHaPl5NpUFkGrkMyZp/oDDLgsxRp kl7a0ePQxN6xJn9RiD/eYYAUa1uZDtHpU+6Lw0SUjC8zrY2sZJZSMu2VeDP9wvnPNLfI PK8kuOZIPHko+BxG2AA4Nw24H5gxsOZ8/zzlE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=DT1IWhndUDcAeaHGI3QXGOzxpZ3skyIR/Y2AcpqIdvA=; b=qc6LO+HHP7eZgKLNJFy8rrKGvPS8PEVdkwROcxn9Uh10ulnr0dmRxPEp4sAVjQfoaC FaBpIjPuWaSP5xIh1l4i4VMBD0ovfPr5x9MZU0n8D2/pJiQja00XK2RBZxM2BvYM19oP rsB83KCGi0VfwCUr5BFoXYR3B5yY5YYeOaIK4Hhjc87Y5x3R+PIONnP6IC2FMJpbIYR2 lgaWLNWagr+nwFzQdwDmQR2QubhkF/yz/OJmfaLA6dKycNOgTgu/qYnX1nze7gHBBX33 ayLgXmzRYidLOz3F49YOE7jbDjkIkHVYO+02P3vCT36P/XGaBuixlHs327SZBIzN5pHa Sevw== X-Gm-Message-State: AFeK/H3Y9BpdE/Ugr7lZRSkrHOu4MkyHqpyVqdcTBiuc21ZZf3n8h4F+gxRtFNoqPF9MaFC3 X-Received: by 10.98.214.3 with SMTP id r3mr2598037pfg.255.1489571855158; Wed, 15 Mar 2017 02:57:35 -0700 (PDT) Received: from linaro.org ([121.95.100.191]) by smtp.googlemail.com with ESMTPSA id v143sm3213383pgb.47.2017.03.15.02.57.34 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 15 Mar 2017 02:57:34 -0700 (PDT) From: AKASHI Takahiro To: catalin.marinas@arm.com, will.deacon@arm.com Subject: [PATCH v33 07/14] arm64: hibernate: preserve kdump image around hibernation Date: Wed, 15 Mar 2017 18:59:36 +0900 Message-Id: <20170315095941.25119-5-takahiro.akashi@linaro.org> X-Mailer: git-send-email 2.11.1 In-Reply-To: <20170315095656.24992-1-takahiro.akashi@linaro.org> References: <20170315095656.24992-1-takahiro.akashi@linaro.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20170315_025752_093926_C1B91703 X-CRM114-Status: GOOD ( 19.82 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: mark.rutland@arm.com, geoff@infradead.org, kexec@lists.infradead.org, AKASHI Takahiro , james.morse@arm.com, bauerman@linux.vnet.ibm.com, dyoung@redhat.com, linux-arm-kernel@lists.infradead.org MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP Since arch_kexec_protect_crashkres() removes a mapping for crash dump kernel image, the loaded data won't be preserved around hibernation. In this patch, helper functions, kexec_prepare_suspend()/ kexec_post_resume(), are additionally called before/after hibernation so that the relevant memory segments will be mapped again and preserved just as the others are. In addition, to minimize the size of hibernation image, kexec_is_chraskres_nosave() is added to pfn_is_nosave() in order to recoginize only the pages that hold loaded crash dump kernel image as saveable. Hibernation excludes any pages that are marked as Reserved and yet "nosave." Signed-off-by: AKASHI Takahiro Reviewed-by: James Morse --- arch/arm64/include/asm/kexec.h | 10 ++++++ arch/arm64/kernel/hibernate.c | 10 +++++- arch/arm64/kernel/machine_kexec.c | 73 ++++++++++++++++++++++++++++++++++++++- arch/arm64/mm/init.c | 27 +++++++++++++++ 4 files changed, 118 insertions(+), 2 deletions(-) diff --git a/arch/arm64/include/asm/kexec.h b/arch/arm64/include/asm/kexec.h index 04744dc5fb61..b9b31fc781b9 100644 --- a/arch/arm64/include/asm/kexec.h +++ b/arch/arm64/include/asm/kexec.h @@ -43,6 +43,16 @@ static inline void crash_setup_regs(struct pt_regs *newregs, /* Empty routine needed to avoid build errors. */ } +#if defined(CONFIG_KEXEC_CORE) && defined(CONFIG_HIBERNATION) +extern bool kexec_is_crashkres_nosave(unsigned long pfn); +extern void kexec_prepare_suspend(void); +extern void kexec_post_resume(void); +#else +static inline bool kexec_is_crashkres_nosave(unsigned long pfn) {return false; } +static inline void kexec_prepare_suspend(void) {} +static inline void kexec_post_resume(void) {} +#endif + #endif /* __ASSEMBLY__ */ #endif diff --git a/arch/arm64/kernel/hibernate.c b/arch/arm64/kernel/hibernate.c index 97a7384100f3..1e10fafa59bd 100644 --- a/arch/arm64/kernel/hibernate.c +++ b/arch/arm64/kernel/hibernate.c @@ -28,6 +28,7 @@ #include #include #include +#include #include #include #include @@ -102,7 +103,8 @@ int pfn_is_nosave(unsigned long pfn) unsigned long nosave_begin_pfn = sym_to_pfn(&__nosave_begin); unsigned long nosave_end_pfn = sym_to_pfn(&__nosave_end - 1); - return (pfn >= nosave_begin_pfn) && (pfn <= nosave_end_pfn); + return ((pfn >= nosave_begin_pfn) && (pfn <= nosave_end_pfn)) || + kexec_is_crashkres_nosave(pfn); } void notrace save_processor_state(void) @@ -286,6 +288,9 @@ int swsusp_arch_suspend(void) local_dbg_save(flags); if (__cpu_suspend_enter(&state)) { + /* make the crash dump kernel image visible/saveable */ + kexec_prepare_suspend(); + sleep_cpu = smp_processor_id(); ret = swsusp_save(); } else { @@ -297,6 +302,9 @@ int swsusp_arch_suspend(void) if (el2_reset_needed()) dcache_clean_range(__hyp_idmap_text_start, __hyp_idmap_text_end); + /* make the crash dump kernel image protected again */ + kexec_post_resume(); + /* * Tell the hibernation core that we've just restored * the memory diff --git a/arch/arm64/kernel/machine_kexec.c b/arch/arm64/kernel/machine_kexec.c index 02e4f929db3b..82f48db589cf 100644 --- a/arch/arm64/kernel/machine_kexec.c +++ b/arch/arm64/kernel/machine_kexec.c @@ -10,6 +10,7 @@ */ #include +#include #include #include @@ -220,7 +221,6 @@ void arch_kexec_protect_crashkres(void) kexec_crash_image->segment[i].memsz, PAGE_KERNEL_INVALID, true); - flush_tlb_all(); } @@ -233,3 +233,74 @@ void arch_kexec_unprotect_crashkres(void) __phys_to_virt(kexec_crash_image->segment[i].mem), kexec_crash_image->segment[i].memsz, PAGE_KERNEL, true); } + +#ifdef CONFIG_HIBERNATION +/* + * To preserve the crash dump kernel image, the relevant memory segments + * should be mapped again around the hibernation. + */ +void kexec_prepare_suspend(void) +{ + if (kexec_crash_image) + arch_kexec_unprotect_crashkres(); +} + +void kexec_post_resume(void) +{ + if (kexec_crash_image) + arch_kexec_protect_crashkres(); +} + +/* + * kexec_is_crashres_nosave + * + * Return true only if a page is part of reserved memory for crash dump kernel, + * but does not hold any data of loaded kernel image. + * + * Note that all the pages in crash dump kernel memory have been initially + * marked as Reserved in kexec_reserve_crashkres_pages(). + * + * In hibernation, the pages which are Reserved and yet "nosave" + * are excluded from the hibernation iamge. kexec_is_crashkres_nosave() + * does this check for crash dump kernel and will reduce the total size + * of hibernation image. + */ + +bool kexec_is_crashkres_nosave(unsigned long pfn) +{ + int i; + phys_addr_t addr; + + /* in reserved memory? */ + if (!crashk_res.end) + return false; + + addr = __pfn_to_phys(pfn); + if ((addr < crashk_res.start) || (crashk_res.end < addr)) + return false; + + /* not part of loaded kernel image? */ + if (!kexec_crash_image) + return true; + + for (i = 0; i < kexec_crash_image->nr_segments; i++) + if (addr >= kexec_crash_image->segment[i].mem && + addr < (kexec_crash_image->segment[i].mem + + kexec_crash_image->segment[i].memsz)) + return false; + + return true; +} + +void crash_free_reserved_phys_range(unsigned long begin, unsigned long end) +{ + unsigned long addr; + struct page *page; + + for (addr = begin; addr < end; addr += PAGE_SIZE) { + page = phys_to_page(addr); + ClearPageReserved(page); + free_reserved_page(page); + } +} +#endif /* CONFIG_HIBERNATION */ diff --git a/arch/arm64/mm/init.c b/arch/arm64/mm/init.c index 09d19207362d..89ba3cd0fe44 100644 --- a/arch/arm64/mm/init.c +++ b/arch/arm64/mm/init.c @@ -134,10 +134,35 @@ static void __init reserve_crashkernel(void) crashk_res.start = crash_base; crashk_res.end = crash_base + crash_size - 1; } + +static void __init kexec_reserve_crashkres_pages(void) +{ +#ifdef CONFIG_HIBERNATION + phys_addr_t addr; + struct page *page; + + if (!crashk_res.end) + return; + + /* + * To reduce the size of hibernation image, all the pages are + * marked as Reserved initially. + */ + for (addr = crashk_res.start; addr < (crashk_res.end + 1); + addr += PAGE_SIZE) { + page = phys_to_page(addr); + SetPageReserved(page); + } +#endif +} #else static void __init reserve_crashkernel(void) { } + +static void __init kexec_reserve_crashkres_pages(void) +{ +} #endif /* CONFIG_KEXEC_CORE */ /* @@ -517,6 +542,8 @@ void __init mem_init(void) /* this will put all unused low memory onto the freelists */ free_all_bootmem(); + kexec_reserve_crashkres_pages(); + mem_init_print_info(NULL); #define MLK(b, t) b, t, ((t) - (b)) >> 10