When crashkernel=X fails to reserve region under 4G, it will fall back to
reserve region above 4G and a region of the default size will also be reserved
under 4G. Unfortunately, /sys/kernel/kexec_crash_size only supports one crash
kernel region now, the user cannot sense the low memory reserved by reading
/sys/kernel/kexec_crash_size. Also, low memory cannot be freed by writing this
file.
For example:
resource_size(crashk_res) = 512M
resource_size(crashk_low_res) = 256M
The result of 'cat /sys/kernel/kexec_crash_size' is 512M, but it should be 768M.
When we execute 'echo 0 > /sys/kernel/kexec_crash_size', the size of crashk_res
becomes 0 and resource_size(crashk_low_res) is still 256 MB, which is incorrect.
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.
Zhen Lei (6):
kexec: fix a memory leak in crash_shrink_memory()
kexec: delete a useless check in crash_shrink_memory()
kexec: clear crashk_res if all its memory has been released
kexec: improve the readability of crash_shrink_memory()
kexec: add helper __crash_shrink_memory()
kexec: enable kexec_crash_size to support two crash kernel regions
kernel/kexec_core.c | 92 +++++++++++++++++++++++++++++++--------------
1 file changed, 64 insertions(+), 28 deletions(-)
--
2.25.1
If the resource of crashk_res has been released, it is better to clear
crashk_res.start and crashk_res.end. Because 'end = 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 <[email protected]>
---
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 = start + new_size;
crash_free_reserved_phys_range(end, crashk_res.end);
- if (start == end)
- release_resource(&crashk_res);
-
ram_res->start = end;
ram_res->end = crashk_res.end;
ram_res->flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM;
ram_res->name = "System RAM";
- crashk_res.end = end - 1;
+ if (start == end) {
+ release_resource(&crashk_res);
+ crashk_res.start = 0;
+ crashk_res.end = 0;
+ } else {
+ crashk_res.end = end - 1;
+ }
insert_resource(&iomem_resource, ram_res);
--
2.25.1
The major adjustments are:
1. end = 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 == end)' with 'if (!new_size)', clear indication that
all crashk memory will be shrunken.
No functional change.
Signed-off-by: Zhen Lei <[email protected]>
---
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 = 0;
- unsigned long start, end;
unsigned long old_size;
struct resource *ram_res;
@@ -1119,9 +1118,7 @@ int crash_shrink_memory(unsigned long new_size)
ret = -ENOENT;
goto unlock;
}
- start = crashk_res.start;
- end = crashk_res.end;
- old_size = (end == 0) ? 0 : end - start + 1;
+ old_size = !crashk_res.end ? 0 : resource_size(&crashk_res);
new_size = roundup(new_size, KEXEC_CRASH_MEM_ALIGN);
if (new_size >= old_size) {
ret = (new_size == old_size) ? 0 : -EINVAL;
@@ -1134,22 +1131,20 @@ int crash_shrink_memory(unsigned long new_size)
goto unlock;
}
- end = start + new_size;
- crash_free_reserved_phys_range(end, crashk_res.end);
-
- ram_res->start = end;
+ ram_res->start = crashk_res.start + new_size;
ram_res->end = crashk_res.end;
ram_res->flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM;
ram_res->name = "System RAM";
- if (start == end) {
+ if (!new_size) {
release_resource(&crashk_res);
crashk_res.start = 0;
crashk_res.end = 0;
} else {
- crashk_res.end = end - 1;
+ crashk_res.end = ram_res->start - 1;
}
+ crash_free_reserved_phys_range(ram_res->start, ram_res->end);
insert_resource(&iomem_resource, ram_res);
unlock:
--
2.25.1
On 05/27/23 at 08:34pm, Zhen Lei wrote:
> If the resource of crashk_res has been released, it is better to clear
> crashk_res.start and crashk_res.end. Because 'end = start - 1' is not
> reasonable, and in some places the test is based on crashk_res.end, not
> resource_size(&crashk_res).
This looks reasonable, at least I haven't think of any risk it could
bring. Thanks.
Acked-by: Baoquan He <[email protected]>
>
> Signed-off-by: Zhen Lei <[email protected]>
> ---
> 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 = start + new_size;
> crash_free_reserved_phys_range(end, crashk_res.end);
>
> - if (start == end)
> - release_resource(&crashk_res);
> -
> ram_res->start = end;
> ram_res->end = crashk_res.end;
> ram_res->flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM;
> ram_res->name = "System RAM";
>
> - crashk_res.end = end - 1;
> + if (start == end) {
> + release_resource(&crashk_res);
> + crashk_res.start = 0;
> + crashk_res.end = 0;
> + } else {
> + crashk_res.end = end - 1;
> + }
>
> insert_resource(&iomem_resource, ram_res);
>
> --
> 2.25.1
>
On 05/27/23 at 08:34pm, Zhen Lei wrote:
> The major adjustments are:
> 1. end = 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 == end)' with 'if (!new_size)', clear indication that
> all crashk memory will be shrunken.
>
> No functional change.
>
> Signed-off-by: Zhen Lei <[email protected]>
LGTM,
Acked-by: Baoquan He <[email protected]>
> ---
> 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 = 0;
> - unsigned long start, end;
> unsigned long old_size;
> struct resource *ram_res;
>
> @@ -1119,9 +1118,7 @@ int crash_shrink_memory(unsigned long new_size)
> ret = -ENOENT;
> goto unlock;
> }
> - start = crashk_res.start;
> - end = crashk_res.end;
> - old_size = (end == 0) ? 0 : end - start + 1;
> + old_size = !crashk_res.end ? 0 : resource_size(&crashk_res);
> new_size = roundup(new_size, KEXEC_CRASH_MEM_ALIGN);
> if (new_size >= old_size) {
> ret = (new_size == old_size) ? 0 : -EINVAL;
> @@ -1134,22 +1131,20 @@ int crash_shrink_memory(unsigned long new_size)
> goto unlock;
> }
>
> - end = start + new_size;
> - crash_free_reserved_phys_range(end, crashk_res.end);
> -
> - ram_res->start = end;
> + ram_res->start = crashk_res.start + new_size;
> ram_res->end = crashk_res.end;
> ram_res->flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM;
> ram_res->name = "System RAM";
>
> - if (start == end) {
> + if (!new_size) {
> release_resource(&crashk_res);
> crashk_res.start = 0;
> crashk_res.end = 0;
> } else {
> - crashk_res.end = end - 1;
> + crashk_res.end = ram_res->start - 1;
> }
>
> + crash_free_reserved_phys_range(ram_res->start, ram_res->end);
> insert_resource(&iomem_resource, ram_res);
>
> unlock:
> --
> 2.25.1
>