From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.2 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 05E2BC43331 for ; Fri, 27 Mar 2020 00:43:19 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id B302D20714 for ; Fri, 27 Mar 2020 00:43:18 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org B302D20714 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 5BC676B000A; Thu, 26 Mar 2020 20:43:18 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 56BC56B000C; Thu, 26 Mar 2020 20:43:18 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4A9E46B000D; Thu, 26 Mar 2020 20:43:18 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0254.hostedemail.com [216.40.44.254]) by kanga.kvack.org (Postfix) with ESMTP id 32B676B000A for ; Thu, 26 Mar 2020 20:43:18 -0400 (EDT) Received: from smtpin02.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 18F668248047 for ; Fri, 27 Mar 2020 00:43:18 +0000 (UTC) X-FDA: 76639293276.02.pigs78_74122a24ad90f X-HE-Tag: pigs78_74122a24ad90f X-Filterd-Recvd-Size: 5242 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf21.hostedemail.com (Postfix) with ESMTP for ; Fri, 27 Mar 2020 00:43:17 +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 6F46E30E; Thu, 26 Mar 2020 17:43:16 -0700 (PDT) Received: from [10.163.1.31] (unknown [10.163.1.31]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 627833F52E; Thu, 26 Mar 2020 17:43:13 -0700 (PDT) Subject: Re: [PATCH 1/3] kexec: Prevent removal of memory in use by a loaded kexec image To: James Morse , kexec@lists.infradead.org, linux-mm@kvack.org, linux-arm-kernel@lists.infradead.org Cc: Eric Biederman , Andrew Morton , Catalin Marinas , Will Deacon , Bhupesh Sharma References: <20200326180730.4754-1-james.morse@arm.com> <20200326180730.4754-2-james.morse@arm.com> From: Anshuman Khandual Message-ID: Date: Fri, 27 Mar 2020 06:13:06 +0530 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 MIME-Version: 1.0 In-Reply-To: <20200326180730.4754-2-james.morse@arm.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit 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 03/26/2020 11:37 PM, James Morse wrote: > An image loaded for kexec is not stored in place, instead its segments > are scattered through memory, and are re-assembled when needed. In the > meantime, the target memory may have been removed. > > Because mm is not aware that this memory is still in use, it allows it > to be removed. Why the isolation process does not fail when these pages are currently being used by kexec ? > > Add a memory notifier to prevent the removal of memory regions that > overlap with a loaded kexec image segment. e.g., when triggered from the > Qemu console: > | kexec_core: memory region in use > | memory memory32: Offline failed. Yes this is definitely an added protection for these kexec loaded kernels memory areas from being offlined but I would have expected the preceding offlining to have failed as well. > > Signed-off-by: James Morse > --- > kernel/kexec_core.c | 56 +++++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 56 insertions(+) > > diff --git a/kernel/kexec_core.c b/kernel/kexec_core.c > index c19c0dad1ebe..ba1d91e868ca 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,56 @@ void __weak arch_kexec_protect_crashkres(void) > > void __weak arch_kexec_unprotect_crashkres(void) > {} > + > +/* > + * If user-space wants to offline memory that is in use by a loaded kexec > + * image, it should unload the image first. > + */ Probably this would need kexec user manual and related system call man pages update as well. > +static int mem_remove_cb(struct notifier_block *nb, unsigned long action, > + void *data) > +{ > + int rv = NOTIFY_OK, i; > + struct memory_notify *arg = data; > + unsigned long pfn = arg->start_pfn; > + unsigned long nr_segments, sstart, send; > + unsigned long end_pfn = arg->start_pfn + arg->nr_pages; > + > + might_sleep(); Required ? > + > + if (action != MEM_GOING_OFFLINE) > + return NOTIFY_DONE; > + > + mutex_lock(&kexec_mutex); > + if (kexec_image) { > + nr_segments = kexec_image->nr_segments; > + > + for (i = 0; i < nr_segments; i++) { > + sstart = PFN_DOWN(kexec_image->segment[i].mem); > + send = PFN_UP(kexec_image->segment[i].mem + > + kexec_image->segment[i].memsz); > + > + if ((pfn <= sstart && sstart < end_pfn) || > + (pfn <= send && send < end_pfn)) { > + pr_warn("Memory region in use\n"); > + rv = NOTIFY_BAD; > + break; > + } > + } > + } > + mutex_unlock(&kexec_mutex); > + > + return rv; Variable 'rv' is redundant, should use NOTIFY_[BAD|OK] directly instead. > +} > + > +static struct notifier_block mem_remove_nb = { > + .notifier_call = mem_remove_cb, > +}; > + > +static int __init register_mem_remove_cb(void) > +{ > + if (IS_ENABLED(CONFIG_MEMORY_HOTPLUG)) Should not all these new code here be wrapped with CONFIG_MEMORY_HOTREMOVE to reduce the scope as well as final code size when the config is disabled. > + return register_memory_notifier(&mem_remove_nb); > + > + return 0; > +} > +device_initcall(register_mem_remove_cb); >