From nobody Sun Feb 8 11:16:41 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 1EF6BC77B7C for ; Sat, 27 May 2023 12:36:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232596AbjE0MgT (ORCPT ); Sat, 27 May 2023 08:36:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45312 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232540AbjE0MgL (ORCPT ); Sat, 27 May 2023 08:36:11 -0400 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D315E13D for ; Sat, 27 May 2023 05:35:50 -0700 (PDT) Received: from dggpemm500006.china.huawei.com (unknown [172.30.72.53]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4QT1V70jw7zTkdx; Sat, 27 May 2023 20:35:43 +0800 (CST) Received: from thunder-town.china.huawei.com (10.174.178.55) by dggpemm500006.china.huawei.com (7.185.36.236) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Sat, 27 May 2023 20:35:47 +0800 From: Zhen Lei To: Eric Biederman , Baoquan He , , CC: Zhen Lei , Michael Holzheu , Andrew Morton , Amerigo Wang Subject: [PATCH 1/6] kexec: fix a memory leak in crash_shrink_memory() Date: Sat, 27 May 2023 20:34:34 +0800 Message-ID: <20230527123439.772-2-thunder.leizhen@huawei.com> X-Mailer: git-send-email 2.37.3.windows.1 In-Reply-To: <20230527123439.772-1-thunder.leizhen@huawei.com> References: <20230527123439.772-1-thunder.leizhen@huawei.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.174.178.55] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To dggpemm500006.china.huawei.com (7.185.36.236) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" If the value of parameter 'new_size' is in the semi-open and semi-closed interval (crashk_res.end - KEXEC_CRASH_MEM_ALIGN + 1, crashk_res.end], the calculation result of ram_res is: ram_res->start =3D crashk_res.end + 1 ram_res->end =3D crashk_res.end The operation of function insert_resource() fails, and ram_res is not added to iomem_resource. As a result, the memory of the control block ram_res is leaked. In fact, on all architectures, the start address and size of crashk_res are already aligned by KEXEC_CRASH_MEM_ALIGN. Therefore, we do not need to round up crashk_res.start again. Instead, we should round up 'new_size' in advance. Fixes: 6480e5a09237 ("kdump: add missing RAM resource in crash_shrink_memor= y()") Fixes: 06a7f711246b ("kexec: premit reduction of the reserved memory size") Signed-off-by: Zhen Lei Acked-by: Baoquan He --- kernel/kexec_core.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/kernel/kexec_core.c b/kernel/kexec_core.c index 3d578c6fefee385..22acee18195a591 100644 --- a/kernel/kexec_core.c +++ b/kernel/kexec_core.c @@ -1122,6 +1122,7 @@ 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; + new_size =3D roundup(new_size, KEXEC_CRASH_MEM_ALIGN); if (new_size >=3D old_size) { ret =3D (new_size =3D=3D old_size) ? 0 : -EINVAL; goto unlock; @@ -1133,9 +1134,7 @@ int crash_shrink_memory(unsigned long new_size) goto unlock; } =20 - start =3D roundup(start, KEXEC_CRASH_MEM_ALIGN); - end =3D roundup(start + new_size, KEXEC_CRASH_MEM_ALIGN); - + end =3D start + new_size; crash_free_reserved_phys_range(end, crashk_res.end); =20 if ((start =3D=3D end) && (crashk_res.parent !=3D NULL)) --=20 2.25.1 From nobody Sun Feb 8 11:16:41 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 F1E3FC77B7C for ; Sat, 27 May 2023 12:36:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232580AbjE0MgP (ORCPT ); Sat, 27 May 2023 08:36:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45314 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232541AbjE0MgL (ORCPT ); Sat, 27 May 2023 08:36:11 -0400 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5B3A91BB for ; Sat, 27 May 2023 05:35:51 -0700 (PDT) Received: from dggpemm500006.china.huawei.com (unknown [172.30.72.54]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4QT1V75096zTkf3; Sat, 27 May 2023 20:35:43 +0800 (CST) Received: from thunder-town.china.huawei.com (10.174.178.55) by dggpemm500006.china.huawei.com (7.185.36.236) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Sat, 27 May 2023 20:35:47 +0800 From: Zhen Lei To: Eric Biederman , Baoquan He , , CC: Zhen Lei , Michael Holzheu , Andrew Morton , Amerigo Wang Subject: [PATCH 2/6] kexec: delete a useless check in crash_shrink_memory() Date: Sat, 27 May 2023 20:34:35 +0800 Message-ID: <20230527123439.772-3-thunder.leizhen@huawei.com> X-Mailer: git-send-email 2.37.3.windows.1 In-Reply-To: <20230527123439.772-1-thunder.leizhen@huawei.com> References: <20230527123439.772-1-thunder.leizhen@huawei.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.174.178.55] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To dggpemm500006.china.huawei.com (7.185.36.236) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The check '(crashk_res.parent !=3D NULL)' is added by commit e05bd3367bd3 ("kexec: fix Oops in crash_shrink_memory()"), but it's stale now. Because if 'crashk_res' is not reserved, it will be zero in size and will be intercepted by the above 'if (new_size >=3D old_size)'. Ago: if (new_size >=3D end - start + 1) Now: old_size =3D (end =3D=3D 0) ? 0 : end - start + 1; if (new_size >=3D old_size) Signed-off-by: Zhen Lei --- kernel/kexec_core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/kexec_core.c b/kernel/kexec_core.c index 22acee18195a591..d1ab139dd49035e 100644 --- a/kernel/kexec_core.c +++ b/kernel/kexec_core.c @@ -1137,7 +1137,7 @@ int crash_shrink_memory(unsigned long new_size) end =3D start + new_size; crash_free_reserved_phys_range(end, crashk_res.end); =20 - if ((start =3D=3D end) && (crashk_res.parent !=3D NULL)) + if (start =3D=3D end) release_resource(&crashk_res); =20 ram_res->start =3D end; --=20 2.25.1 From nobody Sun Feb 8 11:16:41 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 DED84C77B7E for ; Sat, 27 May 2023 12:36:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232577AbjE0MgZ (ORCPT ); Sat, 27 May 2023 08:36:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45332 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232555AbjE0MgM (ORCPT ); Sat, 27 May 2023 08:36:12 -0400 Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 15C6B1BD for ; Sat, 27 May 2023 05:35:51 -0700 (PDT) Received: from dggpemm500006.china.huawei.com (unknown [172.30.72.57]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4QT1Nz3J4hz18LRM; Sat, 27 May 2023 20:31:15 +0800 (CST) Received: from thunder-town.china.huawei.com (10.174.178.55) by dggpemm500006.china.huawei.com (7.185.36.236) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Sat, 27 May 2023 20:35:48 +0800 From: Zhen Lei To: Eric Biederman , Baoquan He , , CC: Zhen Lei , Michael Holzheu , Andrew Morton , Amerigo Wang Subject: [PATCH 3/6] kexec: clear crashk_res if all its memory has been released Date: Sat, 27 May 2023 20:34:36 +0800 Message-ID: <20230527123439.772-4-thunder.leizhen@huawei.com> X-Mailer: git-send-email 2.37.3.windows.1 In-Reply-To: <20230527123439.772-1-thunder.leizhen@huawei.com> References: <20230527123439.772-1-thunder.leizhen@huawei.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.174.178.55] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To dggpemm500006.china.huawei.com (7.185.36.236) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" If the resource of crashk_res has been released, it is better to clear crashk_res.start and crashk_res.end. Because 'end =3D start - 1' is not reasonable, and in some places the test is based on crashk_res.end, not resource_size(&crashk_res). Signed-off-by: Zhen Lei Acked-by: Baoquan He --- kernel/kexec_core.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/kernel/kexec_core.c b/kernel/kexec_core.c index d1ab139dd49035e..bcc86a250ab3bf9 100644 --- a/kernel/kexec_core.c +++ b/kernel/kexec_core.c @@ -1137,15 +1137,18 @@ int crash_shrink_memory(unsigned long new_size) end =3D start + new_size; crash_free_reserved_phys_range(end, crashk_res.end); =20 - if (start =3D=3D end) - release_resource(&crashk_res); - 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"; =20 - crashk_res.end =3D end - 1; + if (start =3D=3D end) { + release_resource(&crashk_res); + crashk_res.start =3D 0; + crashk_res.end =3D 0; + } else { + crashk_res.end =3D end - 1; + } =20 insert_resource(&iomem_resource, ram_res); =20 --=20 2.25.1 From nobody Sun Feb 8 11:16:41 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 28606C77B7E for ; Sat, 27 May 2023 12:36:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232658AbjE0Mg2 (ORCPT ); Sat, 27 May 2023 08:36:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45328 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232557AbjE0MgM (ORCPT ); Sat, 27 May 2023 08:36:12 -0400 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0AE29124 for ; Sat, 27 May 2023 05:35:52 -0700 (PDT) Received: from dggpemm500006.china.huawei.com (unknown [172.30.72.56]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4QT1Nz25QszqScm; Sat, 27 May 2023 20:31:15 +0800 (CST) Received: from thunder-town.china.huawei.com (10.174.178.55) by dggpemm500006.china.huawei.com (7.185.36.236) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Sat, 27 May 2023 20:35:48 +0800 From: Zhen Lei To: Eric Biederman , Baoquan He , , CC: Zhen Lei , Michael Holzheu , Andrew Morton , Amerigo Wang Subject: [PATCH 4/6] kexec: improve the readability of crash_shrink_memory() Date: Sat, 27 May 2023 20:34:37 +0800 Message-ID: <20230527123439.772-5-thunder.leizhen@huawei.com> X-Mailer: git-send-email 2.37.3.windows.1 In-Reply-To: <20230527123439.772-1-thunder.leizhen@huawei.com> References: <20230527123439.772-1-thunder.leizhen@huawei.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.174.178.55] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To dggpemm500006.china.huawei.com (7.185.36.236) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The major adjustments are: 1. end =3D start + new_size. The 'end' here is not an accurate representation, because it is not the new end of crashk_res, but the start of ram_res, difference 1. So eliminate it and replace it with ram_res->start. 2. Use 'ram_res->start' and 'ram_res->end' as arguments to crash_free_reserved_phys_range() to indicate that the memory covered by 'ram_res' is released from the crashk. And keep it close to insert_resource(). 3. Replace 'if (start =3D=3D end)' with 'if (!new_size)', clear indication = that all crashk memory will be shrunken. No functional change. Signed-off-by: Zhen Lei Acked-by: Baoquan He --- kernel/kexec_core.c | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/kernel/kexec_core.c b/kernel/kexec_core.c index bcc86a250ab3bf9..69fe92141b0b62d 100644 --- a/kernel/kexec_core.c +++ b/kernel/kexec_core.c @@ -1108,7 +1108,6 @@ ssize_t crash_get_memory_size(void) int crash_shrink_memory(unsigned long new_size) { int ret =3D 0; - unsigned long start, end; unsigned long old_size; struct resource *ram_res; =20 @@ -1119,9 +1118,7 @@ int crash_shrink_memory(unsigned long new_size) ret =3D -ENOENT; goto unlock; } - start =3D crashk_res.start; - end =3D crashk_res.end; - old_size =3D (end =3D=3D 0) ? 0 : end - start + 1; + old_size =3D !crashk_res.end ? 0 : resource_size(&crashk_res); new_size =3D roundup(new_size, KEXEC_CRASH_MEM_ALIGN); if (new_size >=3D old_size) { ret =3D (new_size =3D=3D old_size) ? 0 : -EINVAL; @@ -1134,22 +1131,20 @@ int crash_shrink_memory(unsigned long new_size) goto unlock; } =20 - end =3D start + new_size; - crash_free_reserved_phys_range(end, crashk_res.end); - - ram_res->start =3D end; + ram_res->start =3D crashk_res.start + new_size; ram_res->end =3D crashk_res.end; ram_res->flags =3D IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM; ram_res->name =3D "System RAM"; =20 - if (start =3D=3D end) { + if (!new_size) { release_resource(&crashk_res); crashk_res.start =3D 0; crashk_res.end =3D 0; } else { - crashk_res.end =3D end - 1; + crashk_res.end =3D ram_res->start - 1; } =20 + crash_free_reserved_phys_range(ram_res->start, ram_res->end); insert_resource(&iomem_resource, ram_res); =20 unlock: --=20 2.25.1 From nobody Sun Feb 8 11:16:41 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 E8B01C77B7C for ; Sat, 27 May 2023 12:36:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231178AbjE0MgW (ORCPT ); Sat, 27 May 2023 08:36:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45272 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232254AbjE0MgL (ORCPT ); Sat, 27 May 2023 08:36:11 -0400 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D279013A for ; Sat, 27 May 2023 05:35:51 -0700 (PDT) Received: from dggpemm500006.china.huawei.com (unknown [172.30.72.54]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4QT1Qr2ln2zLpvQ; Sat, 27 May 2023 20:32:52 +0800 (CST) Received: from thunder-town.china.huawei.com (10.174.178.55) by dggpemm500006.china.huawei.com (7.185.36.236) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Sat, 27 May 2023 20:35:49 +0800 From: Zhen Lei To: Eric Biederman , Baoquan He , , CC: Zhen Lei , Michael Holzheu , Andrew Morton , Amerigo Wang Subject: [PATCH 5/6] kexec: add helper __crash_shrink_memory() Date: Sat, 27 May 2023 20:34:38 +0800 Message-ID: <20230527123439.772-6-thunder.leizhen@huawei.com> X-Mailer: git-send-email 2.37.3.windows.1 In-Reply-To: <20230527123439.772-1-thunder.leizhen@huawei.com> References: <20230527123439.772-1-thunder.leizhen@huawei.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.174.178.55] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To dggpemm500006.china.huawei.com (7.185.36.236) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" No functional change, in preparation for the next patch so that it is easier to review. Signed-off-by: Zhen Lei Acked-by: Baoquan He --- kernel/kexec_core.c | 50 +++++++++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/kernel/kexec_core.c b/kernel/kexec_core.c index 69fe92141b0b62d..e82bc6d6634136a 100644 --- a/kernel/kexec_core.c +++ b/kernel/kexec_core.c @@ -1105,11 +1105,37 @@ ssize_t crash_get_memory_size(void) return size; } =20 +int __crash_shrink_memory(struct resource *old_res, unsigned long new_size) +{ + struct resource *ram_res; + + ram_res =3D kzalloc(sizeof(*ram_res), GFP_KERNEL); + if (!ram_res) + return -ENOMEM; + + ram_res->start =3D old_res->start + new_size; + ram_res->end =3D old_res->end; + ram_res->flags =3D IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM; + ram_res->name =3D "System RAM"; + + if (!new_size) { + release_resource(old_res); + old_res->start =3D 0; + old_res->end =3D 0; + } else { + crashk_res.end =3D ram_res->start - 1; + } + + crash_free_reserved_phys_range(ram_res->start, ram_res->end); + insert_resource(&iomem_resource, ram_res); + + return 0; +} + int crash_shrink_memory(unsigned long new_size) { int ret =3D 0; unsigned long old_size; - struct resource *ram_res; =20 if (!kexec_trylock()) return -EBUSY; @@ -1125,27 +1151,7 @@ int crash_shrink_memory(unsigned long new_size) goto unlock; } =20 - ram_res =3D kzalloc(sizeof(*ram_res), GFP_KERNEL); - if (!ram_res) { - ret =3D -ENOMEM; - goto unlock; - } - - ram_res->start =3D crashk_res.start + new_size; - ram_res->end =3D crashk_res.end; - ram_res->flags =3D IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM; - ram_res->name =3D "System RAM"; - - if (!new_size) { - release_resource(&crashk_res); - crashk_res.start =3D 0; - crashk_res.end =3D 0; - } else { - crashk_res.end =3D ram_res->start - 1; - } - - crash_free_reserved_phys_range(ram_res->start, ram_res->end); - insert_resource(&iomem_resource, ram_res); + ret =3D __crash_shrink_memory(&crashk_res, new_size); =20 unlock: kexec_unlock(); --=20 2.25.1 From nobody Sun Feb 8 11:16:41 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 E05A7C7EE2E for ; Sat, 27 May 2023 12:36:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232591AbjE0Mga (ORCPT ); Sat, 27 May 2023 08:36:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45316 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232569AbjE0MgN (ORCPT ); Sat, 27 May 2023 08:36:13 -0400 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C1C06194 for ; Sat, 27 May 2023 05:35:53 -0700 (PDT) Received: from dggpemm500006.china.huawei.com (unknown [172.30.72.53]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4QT1Qr6x5XzLpvb; Sat, 27 May 2023 20:32:52 +0800 (CST) Received: from thunder-town.china.huawei.com (10.174.178.55) by dggpemm500006.china.huawei.com (7.185.36.236) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Sat, 27 May 2023 20:35:50 +0800 From: Zhen Lei To: Eric Biederman , Baoquan He , , CC: Zhen Lei , Michael Holzheu , Andrew Morton , Amerigo Wang Subject: [PATCH 6/6] kexec: enable kexec_crash_size to support two crash kernel regions Date: Sat, 27 May 2023 20:34:39 +0800 Message-ID: <20230527123439.772-7-thunder.leizhen@huawei.com> X-Mailer: git-send-email 2.37.3.windows.1 In-Reply-To: <20230527123439.772-1-thunder.leizhen@huawei.com> References: <20230527123439.772-1-thunder.leizhen@huawei.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.174.178.55] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To dggpemm500006.china.huawei.com (7.185.36.236) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The crashk_low_res should be considered by /sys/kernel/kexec_crash_size to support two crash kernel regions. Since crashk_res manages the memory with high address and crashk_low_res manages the memory with low address, crashk_low_res is shrunken only when all crashk_res is shrunken. And because when there is only one crash kernel region, crashk_res is always used. Therefore, if all crashk_res is shrunken and crashk_low_res still exists, swap them. Signed-off-by: Zhen Lei Acked-by: Baoquan He --- kernel/kexec_core.c | 43 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/kernel/kexec_core.c b/kernel/kexec_core.c index e82bc6d6634136a..c1d50f6566300d9 100644 --- a/kernel/kexec_core.c +++ b/kernel/kexec_core.c @@ -1091,6 +1091,11 @@ __bpf_kfunc void crash_kexec(struct pt_regs *regs) } } =20 +static inline resource_size_t crash_resource_size(const struct resource *r= es) +{ + return !res->end ? 0 : resource_size(res); +} + ssize_t crash_get_memory_size(void) { ssize_t size =3D 0; @@ -1098,8 +1103,8 @@ ssize_t crash_get_memory_size(void) if (!kexec_trylock()) return -EBUSY; =20 - if (crashk_res.end !=3D crashk_res.start) - size =3D resource_size(&crashk_res); + size +=3D crash_resource_size(&crashk_res); + size +=3D crash_resource_size(&crashk_low_res); =20 kexec_unlock(); return size; @@ -1135,7 +1140,7 @@ int __crash_shrink_memory(struct resource *old_res, u= nsigned long new_size) int crash_shrink_memory(unsigned long new_size) { int ret =3D 0; - unsigned long old_size; + unsigned long old_size, low_size; =20 if (!kexec_trylock()) return -EBUSY; @@ -1144,14 +1149,42 @@ int crash_shrink_memory(unsigned long new_size) ret =3D -ENOENT; goto unlock; } - old_size =3D !crashk_res.end ? 0 : resource_size(&crashk_res); + + low_size =3D crash_resource_size(&crashk_low_res); + old_size =3D crash_resource_size(&crashk_res) + low_size; new_size =3D roundup(new_size, KEXEC_CRASH_MEM_ALIGN); if (new_size >=3D old_size) { ret =3D (new_size =3D=3D old_size) ? 0 : -EINVAL; goto unlock; } =20 - ret =3D __crash_shrink_memory(&crashk_res, new_size); + /* + * (low_size > new_size) implies that low_size is greater than zero. + * This also means that if low_size is zero, the else branch is taken. + * + * If low_size is greater than 0, (low_size > new_size) indicates that + * crashk_low_res also needs to be shrunken. Otherwise, only crashk_res + * needs to be shrunken. + */ + if (low_size > new_size) { + ret =3D __crash_shrink_memory(&crashk_res, 0); + if (ret) + goto unlock; + + ret =3D __crash_shrink_memory(&crashk_low_res, new_size); + } else { + ret =3D __crash_shrink_memory(&crashk_res, new_size - low_size); + } + + /* Swap crashk_res and crashk_low_res if needed */ + if (!crashk_res.end && crashk_low_res.end) { + crashk_res.start =3D crashk_low_res.start; + crashk_res.end =3D crashk_low_res.end; + release_resource(&crashk_low_res); + crashk_low_res.start =3D 0; + crashk_low_res.end =3D 0; + insert_resource(&iomem_resource, &crashk_res); + } =20 unlock: kexec_unlock(); --=20 2.25.1