From nobody Sun Apr 19 09:05:45 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E3344C43334 for ; Mon, 4 Jul 2022 11:41:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233153AbiGDLls (ORCPT ); Mon, 4 Jul 2022 07:41:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52994 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232909AbiGDLlp (ORCPT ); Mon, 4 Jul 2022 07:41:45 -0400 Received: from out30-131.freemail.mail.aliyun.com (out30-131.freemail.mail.aliyun.com [115.124.30.131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9471391 for ; Mon, 4 Jul 2022 04:41:44 -0700 (PDT) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R911e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018046049;MF=carlo.bai@linux.alibaba.com;NM=1;PH=DS;RN=5;SR=0;TI=SMTPD_---0VILd0lZ_1656934899; Received: from localhost(mailfrom:carlo.bai@linux.alibaba.com fp:SMTPD_---0VILd0lZ_1656934899) by smtp.aliyun-inc.com; Mon, 04 Jul 2022 19:41:42 +0800 From: Kaihao Bai To: ebiederm@xmission.com Cc: kexec@lists.infradead.org, linux-kernel@vger.kernel.org, baolin.wang@linux.alibaba.com, carlo.bai@linux.alibaba.com Subject: [PATCH 1/2] kexec: accumulate kexec_crash_size if crashk_low_res defined Date: Mon, 4 Jul 2022 19:41:34 +0800 Message-Id: <1656934895-12334-2-git-send-email-carlo.bai@linux.alibaba.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1656934895-12334-1-git-send-email-carlo.bai@linux.alibaba.com> References: <1656934895-12334-1-git-send-email-carlo.bai@linux.alibaba.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Currently x86 and arm64 support to reserve low memory range for crashkernel. When crashkernel=3DY,low is defined, the main kernel would reserve another memblock (instead of crashkernel=3DX,high, which stored in crashk_res) for crashkernel and store it in crashk_low_res. But the value of /sys/kernel/kexec_crash_size only calculates the size of crashk_res size is not calculated. To ensure the consistency of /sys/kernel/kexec_crash_size, when crashk_low_res is defined, its size needs to be accumulated to kexec_crash_size. Signed-off-by: Kaihao Bai --- kernel/kexec_core.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/kernel/kexec_core.c b/kernel/kexec_core.c index 4d34c78..137f6eb 100644 --- a/kernel/kexec_core.c +++ b/kernel/kexec_core.c @@ -1016,6 +1016,8 @@ size_t crash_get_memory_size(void) mutex_lock(&kexec_mutex); if (crashk_res.end !=3D crashk_res.start) size =3D resource_size(&crashk_res); + if (crashk_low_res.end !=3D crashk_low_res.start) + size +=3D resource_size(&crashk_low_res); mutex_unlock(&kexec_mutex); return size; } --=20 1.8.3.1 From nobody Sun Apr 19 09:05:45 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 26706C433EF for ; Mon, 4 Jul 2022 11:41:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233364AbiGDLlx (ORCPT ); Mon, 4 Jul 2022 07:41:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53110 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232909AbiGDLlt (ORCPT ); Mon, 4 Jul 2022 07:41:49 -0400 Received: from out30-42.freemail.mail.aliyun.com (out30-42.freemail.mail.aliyun.com [115.124.30.42]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D5DF6FD08 for ; Mon, 4 Jul 2022 04:41:47 -0700 (PDT) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R391e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018046049;MF=carlo.bai@linux.alibaba.com;NM=1;PH=DS;RN=5;SR=0;TI=SMTPD_---0VIKqSvs_1656934902; Received: from localhost(mailfrom:carlo.bai@linux.alibaba.com fp:SMTPD_---0VIKqSvs_1656934902) by smtp.aliyun-inc.com; Mon, 04 Jul 2022 19:41:45 +0800 From: Kaihao Bai To: ebiederm@xmission.com Cc: kexec@lists.infradead.org, linux-kernel@vger.kernel.org, baolin.wang@linux.alibaba.com, carlo.bai@linux.alibaba.com Subject: [PATCH 2/2] kexec: release reserved memory ranges to RAM if crashk_low_res defined Date: Mon, 4 Jul 2022 19:41:35 +0800 Message-Id: <1656934895-12334-3-git-send-email-carlo.bai@linux.alibaba.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1656934895-12334-1-git-send-email-carlo.bai@linux.alibaba.com> References: <1656934895-12334-1-git-send-email-carlo.bai@linux.alibaba.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" If reserving low memory range for crashkenrel, the range could not free to System RAM all the time. However, the high memory range corresponding to crashk_res can free to RAM through /sys/kernel/kexec_crash_size. If I write a smaller size to /sys/kernel/kexec_crash_size, the exceeded part of the new size would be released. To support releasing the low memory range, we should determine whether the new size is greater than the accumulated size. If not, the reserved high memory range will be released firstly. If the new size is smaller than the size of low memory range, we continue to release the reserved low memory range after completely releasing the high memory range. Signed-off-by: Kaihao Bai --- kernel/kexec_core.c | 75 +++++++++++++++++++++++++++++++++++++++----------= ---- 1 file changed, 56 insertions(+), 19 deletions(-) diff --git a/kernel/kexec_core.c b/kernel/kexec_core.c index 137f6eb..e89c171 100644 --- a/kernel/kexec_core.c +++ b/kernel/kexec_core.c @@ -1031,12 +1031,42 @@ void __weak crash_free_reserved_phys_range(unsigned= long begin, free_reserved_page(boot_pfn_to_page(addr >> PAGE_SHIFT)); } =20 +static int __crash_shrink_memory(struct resource *crashkernel, + unsigned long start, unsigned long end) +{ + int ret =3D 0; + struct resource *ram_res; + + ram_res =3D kzalloc(sizeof(*ram_res), GFP_KERNEL); + if (!ram_res) { + ret =3D -ENOMEM; + return ret; + } + + crash_free_reserved_phys_range(end, crashkernel->end); + + if ((start =3D=3D end) && (crashkernel->parent !=3D NULL)) + release_resource(crashkernel); + + ram_res->start =3D end; + ram_res->end =3D crashk_res.end; + ram_res->flags =3D IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM; + ram_res->name =3D "System RAM"; + + crashkernel->end =3D end - 1; + + insert_resource(&iomem_resource, ram_res); + + return ret; +} + int crash_shrink_memory(unsigned long new_size) { int ret =3D 0; unsigned long start, end; + unsigned long low_start, low_end; unsigned long old_size; - struct resource *ram_res; + unsigned long low_old_size; =20 mutex_lock(&kexec_mutex); =20 @@ -1047,33 +1077,40 @@ int crash_shrink_memory(unsigned long new_size) start =3D crashk_res.start; end =3D crashk_res.end; old_size =3D (end =3D=3D 0) ? 0 : end - start + 1; + low_start =3D crashk_low_res.start; + low_end =3D crashk_low_res.end; + low_old_size =3D (low_end =3D=3D 0) ? 0 : low_end - low_start + 1; + old_size +=3D low_old_size; + if (new_size >=3D old_size) { ret =3D (new_size =3D=3D old_size) ? 0 : -EINVAL; goto unlock; } + if (start !=3D end) { + start =3D roundup(start, KEXEC_CRASH_MEM_ALIGN); =20 - ram_res =3D kzalloc(sizeof(*ram_res), GFP_KERNEL); - if (!ram_res) { - ret =3D -ENOMEM; - goto unlock; - } - - start =3D roundup(start, KEXEC_CRASH_MEM_ALIGN); - end =3D roundup(start + new_size, KEXEC_CRASH_MEM_ALIGN); - - crash_free_reserved_phys_range(end, crashk_res.end); + /* + * If the new_size is smaller than the reserved lower memory + * range of crashkernel, it releases all higher memory range. + * Otherwise it releases part of higher range. + */ + end =3D (new_size <=3D low_old_size) ? + roundup(start, KEXEC_CRASH_MEM_ALIGN) : + roundup(start + new_size - low_old_size, + KEXEC_CRASH_MEM_ALIGN); =20 - if ((start =3D=3D end) && (crashk_res.parent !=3D NULL)) - release_resource(&crashk_res); + ret =3D __crash_shrink_memory(&crashk_res, start, end); =20 - ram_res->start =3D end; - ram_res->end =3D crashk_res.end; - ram_res->flags =3D IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM; - ram_res->name =3D "System RAM"; + if (ret) + goto unlock; + } =20 - crashk_res.end =3D end - 1; + if (new_size < low_old_size) { + low_start =3D roundup(low_start, KEXEC_CRASH_MEM_ALIGN); + low_end =3D roundup(low_start + new_size, KEXEC_CRASH_MEM_ALIGN); =20 - insert_resource(&iomem_resource, ram_res); + ret =3D __crash_shrink_memory(&crashk_low_res, low_start, low_end); + } =20 unlock: mutex_unlock(&kexec_mutex); --=20 1.8.3.1