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.4 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,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 7F47FC43331 for ; Fri, 27 Mar 2020 02:34:48 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 3EBC420714 for ; Fri, 27 Mar 2020 02:34:48 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="YITPYQ58" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 3EBC420714 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id C462D6B0010; Thu, 26 Mar 2020 22:34:47 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BCFF36B0032; Thu, 26 Mar 2020 22:34:47 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A981D6B0036; Thu, 26 Mar 2020 22:34:47 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0173.hostedemail.com [216.40.44.173]) by kanga.kvack.org (Postfix) with ESMTP id 8DDC46B0010 for ; Thu, 26 Mar 2020 22:34:47 -0400 (EDT) Received: from smtpin04.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 6C8E8180AD815 for ; Fri, 27 Mar 2020 02:34:47 +0000 (UTC) X-FDA: 76639574214.04.women27_46e29ff93075a X-HE-Tag: women27_46e29ff93075a X-Filterd-Recvd-Size: 6298 Received: from us-smtp-delivery-74.mimecast.com (us-smtp-delivery-74.mimecast.com [63.128.21.74]) by imf03.hostedemail.com (Postfix) with ESMTP for ; Fri, 27 Mar 2020 02:34:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1585276486; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=fV94Jr1a00Ad+U5YFGLNM6S9xxbC/3ub7oqPk8qByeE=; b=YITPYQ583boCgF729kH8kvqJohnBpn6t6kWbyLK5wMPFvBn5Jsws2j0OryUDz2LXF9g2Br 7NEI3VVZQdOG/ctQK2LAfkEj7h48+zjNC7kt9/Oqcqjxr/6tWGk/PKgHMH6UfvZ40CweQ3 fmW+NBuTxyfCCTTvHcCBpv19xh0Y3/4= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-422-e7K05RUdPkODT17ZFxXtXg-1; Thu, 26 Mar 2020 22:34:44 -0400 X-MC-Unique: e7K05RUdPkODT17ZFxXtXg-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 17A3F1005509; Fri, 27 Mar 2020 02:34:43 +0000 (UTC) Received: from localhost (ovpn-12-80.pek2.redhat.com [10.72.12.80]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 474C55C219; Fri, 27 Mar 2020 02:34:41 +0000 (UTC) Date: Fri, 27 Mar 2020 10:34:39 +0800 From: Baoquan He To: James Morse Cc: kexec@lists.infradead.org, linux-mm@kvack.org, linux-arm-kernel@lists.infradead.org, Eric Biederman , Andrew Morton , Catalin Marinas , Will Deacon , Anshuman Khandual , Bhupesh Sharma Subject: Re: [PATCH 1/3] kexec: Prevent removal of memory in use by a loaded kexec image Message-ID: <20200327023439.GO3039@MiWiFi-R3L-srv> References: <20200326180730.4754-1-james.morse@arm.com> <20200326180730.4754-2-james.morse@arm.com> MIME-Version: 1.0 In-Reply-To: <20200326180730.4754-2-james.morse@arm.com> User-Agent: Mutt/1.10.1 (2018-07-13) X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable Content-Disposition: inline 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/20 at 06:07pm, 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. >=20 > Because mm is not aware that this memory is still in use, it allows it > to be removed. >=20 > 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. As I replied to the cover letter, usually we do loading and juming of kexec-ed kernel at one time. If we expect to do both of them at different time, I agree we should do something to make thing safer if someone really want to do since it's allowed, can we do anything with the existing notifier? Mem hotplug has got a notifier to notice it will offline a memory region. memory_notify(MEM_OFFLINE, &arg); >=20 > Signed-off-by: James Morse > --- > kernel/kexec_core.c | 56 +++++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 56 insertions(+) >=20 > 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) > =20 > void __weak arch_kexec_unprotect_crashkres(void) > {} > + > +/* > + * If user-space wants to offline memory that is in use by a loaded kexe= c > + * image, it should unload the image first. > + */ > +static int mem_remove_cb(struct notifier_block *nb, unsigned long action= , > +=09=09=09 void *data) > +{ > +=09int rv =3D NOTIFY_OK, i; > +=09struct memory_notify *arg =3D data; > +=09unsigned long pfn =3D arg->start_pfn; > +=09unsigned long nr_segments, sstart, send; > +=09unsigned long end_pfn =3D arg->start_pfn + arg->nr_pages; > + > +=09might_sleep(); > + > +=09if (action !=3D MEM_GOING_OFFLINE) > +=09=09return NOTIFY_DONE; > + > +=09mutex_lock(&kexec_mutex); > +=09if (kexec_image) { > +=09=09nr_segments =3D kexec_image->nr_segments; > + > +=09=09for (i =3D 0; i < nr_segments; i++) { > +=09=09=09sstart =3D PFN_DOWN(kexec_image->segment[i].mem); > +=09=09=09send =3D PFN_UP(kexec_image->segment[i].mem + > +=09=09=09=09 kexec_image->segment[i].memsz); > + > +=09=09=09if ((pfn <=3D sstart && sstart < end_pfn) || > +=09=09=09 (pfn <=3D send && send < end_pfn)) { > +=09=09=09=09pr_warn("Memory region in use\n"); > +=09=09=09=09rv =3D NOTIFY_BAD; > +=09=09=09=09break; > +=09=09=09} > +=09=09} > +=09} > +=09mutex_unlock(&kexec_mutex); > + > +=09return rv; > +} > + > +static struct notifier_block mem_remove_nb =3D { > +=09.notifier_call =3D mem_remove_cb, > +}; > + > +static int __init register_mem_remove_cb(void) > +{ > +=09if (IS_ENABLED(CONFIG_MEMORY_HOTPLUG)) > +=09=09return register_memory_notifier(&mem_remove_nb); > + > +=09return 0; > +} > +device_initcall(register_mem_remove_cb); > --=20 > 2.25.1 >=20 >=20