From patchwork Fri May 1 16:57:01 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Morse X-Patchwork-Id: 11522879 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 3A280139A for ; Fri, 1 May 2020 16:57:56 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 0B3902495F for ; Fri, 1 May 2020 16:57:56 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 0B3902495F Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=arm.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 44F6D8E0006; Fri, 1 May 2020 12:57:55 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 3FF508E0001; Fri, 1 May 2020 12:57:55 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3152C8E0006; Fri, 1 May 2020 12:57:55 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0023.hostedemail.com [216.40.44.23]) by kanga.kvack.org (Postfix) with ESMTP id 1B9848E0001 for ; Fri, 1 May 2020 12:57:55 -0400 (EDT) Received: from smtpin06.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id DB7E52497 for ; Fri, 1 May 2020 16:57:54 +0000 (UTC) X-FDA: 76768757268.06.dogs90_27f4e7f54a845 X-Spam-Summary: 2,0,0,1f97805b23238fa2,d41d8cd98f00b204,james.morse@arm.com,,RULES_HIT:41:355:379:541:800:960:966:967:968:973:988:989:1260:1261:1311:1314:1345:1437:1515:1534:1542:1711:1730:1747:1777:1792:2196:2199:2393:2525:2553:2559:2563:2682:2685:2859:2896:2933:2937:2939:2942:2945:2947:2951:2954:3022:3138:3139:3140:3141:3142:3354:3622:3865:3866:3867:3868:3870:3871:3872:3874:3934:3936:3938:3941:3944:3947:3950:3953:3956:3959:4321:4384:4385:4389:4395:5007:6261:6299:8634:8985:9010:9025:9389:10004:11026:11232:11473:11657:11658:11854:11914:12043:12291:12296:12297:12438:12517:12519:12555:12683:12895:12986:13161:13229:13894:13972:14096:14181:14394:14721:21063:21080:21325:21451:21627:21740:21795:21811:21990:30054:30069:30070:30079:30090,0,RBL:217.140.110.172:@arm.com:.lbl8.mailshell.net-62.2.0.100 64.100.201.201,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:22,LUA_SUMMARY:none X-HE-Tag: dogs90_27f4e7f54a845 X-Filterd-Recvd-Size: 3996 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf24.hostedemail.com (Postfix) with ESMTP for ; Fri, 1 May 2020 16:57:54 +0000 (UTC) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id DA28F30E; Fri, 1 May 2020 09:57:53 -0700 (PDT) Received: from melchizedek.cambridge.arm.com (melchizedek.cambridge.arm.com [10.1.196.50]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id C0BAE3F305; Fri, 1 May 2020 09:57:52 -0700 (PDT) From: James Morse To: kexec@lists.infradead.org, linux-mm@kvack.org, linux-arm-kernel@lists.infradead.org Cc: Eric Biederman , David Hildenbrand , Dave Young , Baoquan He , James Morse Subject: [PATCH] kexec: Discard loaded image on memory hotplug Date: Fri, 1 May 2020 17:57:01 +0100 Message-Id: <20200501165701.24587-1-james.morse@arm.com> X-Mailer: git-send-email 2.19.1 MIME-Version: 1.0 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On x86, the kexec payload contains a copy of the current memory map. If memory is added or removed, this copy of the memory map becomes stale. Getting this wrong may prevent the next kernel from booting. The first kernel may die if it tries to re-assemble the next kernel in memory that has been removed. Discard the loaded kexec image when the memory map changes, user-space should reload it. Kdump is unaffected, as it is placed within the crashkernel reserved memory area and only uses this memory. The stale memory map may affect generation of the vmcore, but the kdump kernel should be in a position to validate it. Signed-off-by: James Morse --- This patch obsoletes: * kexec/memory_hotplug: Prevent removal and accidental use https://lore.kernel.org/linux-arm-kernel/20200326180730.4754-1-james.morse@arm.com/ kernel/kexec_core.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/kernel/kexec_core.c b/kernel/kexec_core.c index c19c0dad1ebe..e1901e5bd4b5 100644 --- a/kernel/kexec_core.c +++ b/kernel/kexec_core.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -22,10 +23,12 @@ #include #include #include +#include #include #include #include #include +#include #include #include #include @@ -1219,3 +1222,40 @@ void __weak arch_kexec_protect_crashkres(void) void __weak arch_kexec_unprotect_crashkres(void) {} + +/* + * If the memory layout changes, any loaded kexec image should be evicted + * as it may contain a copy of the (now stale) memory map. This also means + * we don't need to check the memory is still present when re-assembling the + * new kernel at machine_kexec() time. + */ +static int mem_change_cb(struct notifier_block *nb, unsigned long action, + void *data) +{ + /* + * Actions are either a change, or a change being cancelled. + * A second discard for 'cancel's is harmless. + */ + + mutex_lock(&kexec_mutex); + if (kexec_image) { + kimage_free(xchg(&kexec_image, NULL)); + pr_warn("loaded image discarded due to memory hotplug"); + } + mutex_unlock(&kexec_mutex); + + return NOTIFY_DONE; +} + +static struct notifier_block mem_change_nb = { + .notifier_call = mem_change_cb, +}; + +static int __init register_mem_change_cb(void) +{ + if (IS_ENABLED(CONFIG_MEMORY_HOTPLUG)) + return register_memory_notifier(&mem_change_nb); + + return 0; +} +device_initcall(register_mem_change_cb);