diff mbox series

powerpc/kexec_file: Fix division by zero in extra size estimation

Message ID 20230130014707.541110-1-mpe@ellerman.id.au (mailing list archive)
State Accepted
Commit 7294194b47e994753a86eee8cf1c61f3f36458a3
Headers show
Series powerpc/kexec_file: Fix division by zero in extra size estimation | expand

Checks

Context Check Description
snowpatch_ozlabs/github-powerpc_ppctests success Successfully ran 8 jobs.
snowpatch_ozlabs/github-powerpc_selftests success Successfully ran 8 jobs.
snowpatch_ozlabs/github-powerpc_kernel_qemu success Successfully ran 24 jobs.
snowpatch_ozlabs/github-powerpc_sparse success Successfully ran 4 jobs.
snowpatch_ozlabs/github-powerpc_clang success Successfully ran 6 jobs.

Commit Message

Michael Ellerman Jan. 30, 2023, 1:47 a.m. UTC
In kexec_extra_fdt_size_ppc64() there's logic to estimate how much
extra space will be needed in the device tree for some memory related
properties.

That logic uses the size of RAM divided by drmem_lmb_size() to do the
estimation. However drmem_lmb_size() can be zero if the machine has no
hotpluggable memory configured, which is the case when booting with qemu
and no maxmem=x parameter is passed (the default).

The division by zero is reported by UBSAN, and can also lead to an
overflow and a warning from kvmalloc, and kdump kernel loading fails:

  WARNING: CPU: 0 PID: 133 at mm/util.c:596 kvmalloc_node+0x15c/0x160
  Modules linked in:
  CPU: 0 PID: 133 Comm: kexec Not tainted 6.2.0-rc5-03455-g07358bd97810 #223
  Hardware name: IBM pSeries (emulated by qemu) POWER9 (raw) 0x4e1200 0xf000005 of:SLOF,git-dd0dca pSeries
  NIP:  c00000000041ff4c LR: c00000000041fe58 CTR: 0000000000000000
  REGS: c0000000096ef750 TRAP: 0700   Not tainted  (6.2.0-rc5-03455-g07358bd97810)
  MSR:  800000000282b033 <SF,VEC,VSX,EE,FP,ME,IR,DR,RI,LE>  CR: 24248242  XER: 2004011e
  CFAR: c00000000041fed0 IRQMASK: 0
  ...
  NIP kvmalloc_node+0x15c/0x160
  LR  kvmalloc_node+0x68/0x160
  Call Trace:
    kvmalloc_node+0x68/0x160 (unreliable)
    of_kexec_alloc_and_setup_fdt+0xb8/0x7d0
    elf64_load+0x25c/0x4a0
    kexec_image_load_default+0x58/0x80
    sys_kexec_file_load+0x5c0/0x920
    system_call_exception+0x128/0x330
    system_call_vectored_common+0x15c/0x2ec

To fix it, skip the calculation if drmem_lmb_size() is zero.

Fixes: 2377c92e37fe ("powerpc/kexec_file: fix FDT size estimation for kdump kernel")
Cc: stable@vger.kernel.org # v5.12+
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
---
 arch/powerpc/kexec/file_load_64.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

Comments

Sourabh Jain Jan. 31, 2023, 9:02 a.m. UTC | #1
Hello Michael,

On 30/01/23 07:17, Michael Ellerman wrote:
> In kexec_extra_fdt_size_ppc64() there's logic to estimate how much
> extra space will be needed in the device tree for some memory related
> properties.
>
> That logic uses the size of RAM divided by drmem_lmb_size() to do the
> estimation. However drmem_lmb_size() can be zero if the machine has no
> hotpluggable memory configured, which is the case when booting with qemu
> and no maxmem=x parameter is passed (the default).
>
> The division by zero is reported by UBSAN, and can also lead to an
> overflow and a warning from kvmalloc, and kdump kernel loading fails:
>
>    WARNING: CPU: 0 PID: 133 at mm/util.c:596 kvmalloc_node+0x15c/0x160
>    Modules linked in:
>    CPU: 0 PID: 133 Comm: kexec Not tainted 6.2.0-rc5-03455-g07358bd97810 #223
>    Hardware name: IBM pSeries (emulated by qemu) POWER9 (raw) 0x4e1200 0xf000005 of:SLOF,git-dd0dca pSeries
>    NIP:  c00000000041ff4c LR: c00000000041fe58 CTR: 0000000000000000
>    REGS: c0000000096ef750 TRAP: 0700   Not tainted  (6.2.0-rc5-03455-g07358bd97810)
>    MSR:  800000000282b033 <SF,VEC,VSX,EE,FP,ME,IR,DR,RI,LE>  CR: 24248242  XER: 2004011e
>    CFAR: c00000000041fed0 IRQMASK: 0
>    ...
>    NIP kvmalloc_node+0x15c/0x160
>    LR  kvmalloc_node+0x68/0x160
>    Call Trace:
>      kvmalloc_node+0x68/0x160 (unreliable)
>      of_kexec_alloc_and_setup_fdt+0xb8/0x7d0
>      elf64_load+0x25c/0x4a0
>      kexec_image_load_default+0x58/0x80
>      sys_kexec_file_load+0x5c0/0x920
>      system_call_exception+0x128/0x330
>      system_call_vectored_common+0x15c/0x2ec
>
> To fix it, skip the calculation if drmem_lmb_size() is zero.
>
> Fixes: 2377c92e37fe ("powerpc/kexec_file: fix FDT size estimation for kdump kernel")
> Cc: stable@vger.kernel.org # v5.12+
> Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
> ---
>   arch/powerpc/kexec/file_load_64.c | 10 ++++++----
>   1 file changed, 6 insertions(+), 4 deletions(-)
>
> diff --git a/arch/powerpc/kexec/file_load_64.c b/arch/powerpc/kexec/file_load_64.c
> index af8854f9eae3..3caee570e79b 100644
> --- a/arch/powerpc/kexec/file_load_64.c
> +++ b/arch/powerpc/kexec/file_load_64.c
> @@ -989,10 +989,12 @@ unsigned int kexec_extra_fdt_size_ppc64(struct kimage *image)
>   	 * linux,drconf-usable-memory properties. Get an approximate on the
>   	 * number of usable memory entries and use for FDT size estimation.
>   	 */
> -	usm_entries = ((memblock_end_of_DRAM() / drmem_lmb_size()) +
> -		       (2 * (resource_size(&crashk_res) / drmem_lmb_size())));
> -
> -	extra_size = (unsigned int)(usm_entries * sizeof(u64));
> +	if (drmem_lmb_size()) {
> +		usm_entries = ((memblock_end_of_DRAM() / drmem_lmb_size()) +
> +			       (2 * (resource_size(&crashk_res) / drmem_lmb_size())));
> +		extra_size = (unsigned int)(usm_entries * sizeof(u64));
> +	} else
> +		extra_size = 0;
>   
>   	/*
>   	 * Get the number of CPU nodes in the current DT. This allows to

I failed to replicate this issue.

Qemu command used:
$ qemu-system-ppc64 -enable-kvm -smp 4,cores=2 -drive 
file=my-image.qcow2 -nographic -m 2G


lsmem (inside guest):
RANGE                                 SIZE  STATE REMOVABLE BLOCK
0x0000000000000000-0x000000007fffffff   2G online       yes 0-127

Memory block size:        16M
Total online memory:       2G
Total offline memory:      0B

Not sure what I am missing, but changes looks good to me.

Thanks,
Sourabh Jain
Michael Ellerman Feb. 1, 2023, 1:55 a.m. UTC | #2
Sourabh Jain <sourabhjain@linux.ibm.com> writes:
> On 30/01/23 07:17, Michael Ellerman wrote:
>> In kexec_extra_fdt_size_ppc64() there's logic to estimate how much
>> extra space will be needed in the device tree for some memory related
>> properties.
>>
>> That logic uses the size of RAM divided by drmem_lmb_size() to do the
>> estimation. However drmem_lmb_size() can be zero if the machine has no
>> hotpluggable memory configured, which is the case when booting with qemu
>> and no maxmem=x parameter is passed (the default).
>>
>> The division by zero is reported by UBSAN, and can also lead to an
>> overflow and a warning from kvmalloc, and kdump kernel loading fails:
>>
>>    WARNING: CPU: 0 PID: 133 at mm/util.c:596 kvmalloc_node+0x15c/0x160
>>    Modules linked in:
>>    CPU: 0 PID: 133 Comm: kexec Not tainted 6.2.0-rc5-03455-g07358bd97810 #223
>>    Hardware name: IBM pSeries (emulated by qemu) POWER9 (raw) 0x4e1200 0xf000005 of:SLOF,git-dd0dca pSeries
>>    NIP:  c00000000041ff4c LR: c00000000041fe58 CTR: 0000000000000000
>>    REGS: c0000000096ef750 TRAP: 0700   Not tainted  (6.2.0-rc5-03455-g07358bd97810)
>>    MSR:  800000000282b033 <SF,VEC,VSX,EE,FP,ME,IR,DR,RI,LE>  CR: 24248242  XER: 2004011e
>>    CFAR: c00000000041fed0 IRQMASK: 0
>>    ...
>>    NIP kvmalloc_node+0x15c/0x160
>>    LR  kvmalloc_node+0x68/0x160
>>    Call Trace:
>>      kvmalloc_node+0x68/0x160 (unreliable)
>>      of_kexec_alloc_and_setup_fdt+0xb8/0x7d0
>>      elf64_load+0x25c/0x4a0
>>      kexec_image_load_default+0x58/0x80
>>      sys_kexec_file_load+0x5c0/0x920
>>      system_call_exception+0x128/0x330
>>      system_call_vectored_common+0x15c/0x2ec
>>
>> To fix it, skip the calculation if drmem_lmb_size() is zero.
>>
>> Fixes: 2377c92e37fe ("powerpc/kexec_file: fix FDT size estimation for kdump kernel")
>> Cc: stable@vger.kernel.org # v5.12+
>> Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
>> ---
>>   arch/powerpc/kexec/file_load_64.c | 10 ++++++----
>>   1 file changed, 6 insertions(+), 4 deletions(-)
>>
>> diff --git a/arch/powerpc/kexec/file_load_64.c b/arch/powerpc/kexec/file_load_64.c
>> index af8854f9eae3..3caee570e79b 100644
>> --- a/arch/powerpc/kexec/file_load_64.c
>> +++ b/arch/powerpc/kexec/file_load_64.c
>> @@ -989,10 +989,12 @@ unsigned int kexec_extra_fdt_size_ppc64(struct kimage *image)
>>   	 * linux,drconf-usable-memory properties. Get an approximate on the
>>   	 * number of usable memory entries and use for FDT size estimation.
>>   	 */
>> -	usm_entries = ((memblock_end_of_DRAM() / drmem_lmb_size()) +
>> -		       (2 * (resource_size(&crashk_res) / drmem_lmb_size())));
>> -
>> -	extra_size = (unsigned int)(usm_entries * sizeof(u64));
>> +	if (drmem_lmb_size()) {
>> +		usm_entries = ((memblock_end_of_DRAM() / drmem_lmb_size()) +
>> +			       (2 * (resource_size(&crashk_res) / drmem_lmb_size())));
>> +		extra_size = (unsigned int)(usm_entries * sizeof(u64));
>> +	} else
>> +		extra_size = 0;
>>   
>>   	/*
>>   	 * Get the number of CPU nodes in the current DT. This allows to
>
> I failed to replicate this issue.
>
> Qemu command used:
> $ qemu-system-ppc64 -enable-kvm -smp 4,cores=2 -drive 
> file=my-image.qcow2 -nographic -m 2G
>
>
> lsmem (inside guest):
> RANGE                                 SIZE  STATE REMOVABLE BLOCK
> 0x0000000000000000-0x000000007fffffff   2G online       yes 0-127
>
> Memory block size:        16M
> Total online memory:       2G
> Total offline memory:      0B
>
> Not sure what I am missing, but changes looks good to me.

Hmm, interesting.

Do you have /proc/device-tree/ibm,dynamic-reconfiguration-memory in the VM?

What version of qemu are you using? I think I tested with 6.2 and 7.1.

cheers
Sourabh Jain Feb. 1, 2023, 4:23 a.m. UTC | #3
On 01/02/23 07:25, Michael Ellerman wrote:
> Sourabh Jain <sourabhjain@linux.ibm.com> writes:
>> On 30/01/23 07:17, Michael Ellerman wrote:
>>> In kexec_extra_fdt_size_ppc64() there's logic to estimate how much
>>> extra space will be needed in the device tree for some memory related
>>> properties.
>>>
>>> That logic uses the size of RAM divided by drmem_lmb_size() to do the
>>> estimation. However drmem_lmb_size() can be zero if the machine has no
>>> hotpluggable memory configured, which is the case when booting with qemu
>>> and no maxmem=x parameter is passed (the default).
>>>
>>> The division by zero is reported by UBSAN, and can also lead to an
>>> overflow and a warning from kvmalloc, and kdump kernel loading fails:
>>>
>>>     WARNING: CPU: 0 PID: 133 at mm/util.c:596 kvmalloc_node+0x15c/0x160
>>>     Modules linked in:
>>>     CPU: 0 PID: 133 Comm: kexec Not tainted 6.2.0-rc5-03455-g07358bd97810 #223
>>>     Hardware name: IBM pSeries (emulated by qemu) POWER9 (raw) 0x4e1200 0xf000005 of:SLOF,git-dd0dca pSeries
>>>     NIP:  c00000000041ff4c LR: c00000000041fe58 CTR: 0000000000000000
>>>     REGS: c0000000096ef750 TRAP: 0700   Not tainted  (6.2.0-rc5-03455-g07358bd97810)
>>>     MSR:  800000000282b033 <SF,VEC,VSX,EE,FP,ME,IR,DR,RI,LE>  CR: 24248242  XER: 2004011e
>>>     CFAR: c00000000041fed0 IRQMASK: 0
>>>     ...
>>>     NIP kvmalloc_node+0x15c/0x160
>>>     LR  kvmalloc_node+0x68/0x160
>>>     Call Trace:
>>>       kvmalloc_node+0x68/0x160 (unreliable)
>>>       of_kexec_alloc_and_setup_fdt+0xb8/0x7d0
>>>       elf64_load+0x25c/0x4a0
>>>       kexec_image_load_default+0x58/0x80
>>>       sys_kexec_file_load+0x5c0/0x920
>>>       system_call_exception+0x128/0x330
>>>       system_call_vectored_common+0x15c/0x2ec
>>>
>>> To fix it, skip the calculation if drmem_lmb_size() is zero.
>>>
>>> Fixes: 2377c92e37fe ("powerpc/kexec_file: fix FDT size estimation for kdump kernel")
>>> Cc: stable@vger.kernel.org # v5.12+
>>> Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
>>> ---
>>>    arch/powerpc/kexec/file_load_64.c | 10 ++++++----
>>>    1 file changed, 6 insertions(+), 4 deletions(-)
>>>
>>> diff --git a/arch/powerpc/kexec/file_load_64.c b/arch/powerpc/kexec/file_load_64.c
>>> index af8854f9eae3..3caee570e79b 100644
>>> --- a/arch/powerpc/kexec/file_load_64.c
>>> +++ b/arch/powerpc/kexec/file_load_64.c
>>> @@ -989,10 +989,12 @@ unsigned int kexec_extra_fdt_size_ppc64(struct kimage *image)
>>>    	 * linux,drconf-usable-memory properties. Get an approximate on the
>>>    	 * number of usable memory entries and use for FDT size estimation.
>>>    	 */
>>> -	usm_entries = ((memblock_end_of_DRAM() / drmem_lmb_size()) +
>>> -		       (2 * (resource_size(&crashk_res) / drmem_lmb_size())));
>>> -
>>> -	extra_size = (unsigned int)(usm_entries * sizeof(u64));
>>> +	if (drmem_lmb_size()) {
>>> +		usm_entries = ((memblock_end_of_DRAM() / drmem_lmb_size()) +
>>> +			       (2 * (resource_size(&crashk_res) / drmem_lmb_size())));
>>> +		extra_size = (unsigned int)(usm_entries * sizeof(u64));
>>> +	} else
>>> +		extra_size = 0;
>>>    
>>>    	/*
>>>    	 * Get the number of CPU nodes in the current DT. This allows to
>> I failed to replicate this issue.
>>
>> Qemu command used:
>> $ qemu-system-ppc64 -enable-kvm -smp 4,cores=2 -drive
>> file=my-image.qcow2 -nographic -m 2G
>>
>>
>> lsmem (inside guest):
>> RANGE                                 SIZE  STATE REMOVABLE BLOCK
>> 0x0000000000000000-0x000000007fffffff   2G online       yes 0-127
>>
>> Memory block size:        16M
>> Total online memory:       2G
>> Total offline memory:      0B
>>
>> Not sure what I am missing, but changes looks good to me.
> Hmm, interesting.
>
> Do you have /proc/device-tree/ibm,dynamic-reconfiguration-memory in the VM?

No I don't.

But if I add maxmem=x to qemu command line then ibm,dynamic-reconfiguration
DT node comes up.

> What version of qemu are you using? I think I tested with 6.2 and 7.1.

QEMU emulator version 7.2.50 (v7.2.0-904-g13356edb87)

I tried with QEMU emulator version 7.1.0 (v7.1.0), but not able to 
replicate it.

- Sourabh
Hari Bathini Feb. 2, 2023, 7 a.m. UTC | #4
Thanks for the fix, Michael.

On 30/01/23 7:17 am, Michael Ellerman wrote:
> In kexec_extra_fdt_size_ppc64() there's logic to estimate how much
> extra space will be needed in the device tree for some memory related
> properties.
> 
> That logic uses the size of RAM divided by drmem_lmb_size() to do the
> estimation. However drmem_lmb_size() can be zero if the machine has no
> hotpluggable memory configured, which is the case when booting with qemu
> and no maxmem=x parameter is passed (the default).
> 
> The division by zero is reported by UBSAN, and can also lead to an
> overflow and a warning from kvmalloc, and kdump kernel loading fails:
> 
>    WARNING: CPU: 0 PID: 133 at mm/util.c:596 kvmalloc_node+0x15c/0x160
>    Modules linked in:
>    CPU: 0 PID: 133 Comm: kexec Not tainted 6.2.0-rc5-03455-g07358bd97810 #223
>    Hardware name: IBM pSeries (emulated by qemu) POWER9 (raw) 0x4e1200 0xf000005 of:SLOF,git-dd0dca pSeries
>    NIP:  c00000000041ff4c LR: c00000000041fe58 CTR: 0000000000000000
>    REGS: c0000000096ef750 TRAP: 0700   Not tainted  (6.2.0-rc5-03455-g07358bd97810)
>    MSR:  800000000282b033 <SF,VEC,VSX,EE,FP,ME,IR,DR,RI,LE>  CR: 24248242  XER: 2004011e
>    CFAR: c00000000041fed0 IRQMASK: 0
>    ...
>    NIP kvmalloc_node+0x15c/0x160
>    LR  kvmalloc_node+0x68/0x160
>    Call Trace:
>      kvmalloc_node+0x68/0x160 (unreliable)
>      of_kexec_alloc_and_setup_fdt+0xb8/0x7d0
>      elf64_load+0x25c/0x4a0
>      kexec_image_load_default+0x58/0x80
>      sys_kexec_file_load+0x5c0/0x920
>      system_call_exception+0x128/0x330
>      system_call_vectored_common+0x15c/0x2ec
> 
> To fix it, skip the calculation if drmem_lmb_size() is zero.
> 
> Fixes: 2377c92e37fe ("powerpc/kexec_file: fix FDT size estimation for kdump kernel")
> Cc: stable@vger.kernel.org # v5.12+
> Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>

Acked-by: Hari Bathini <hbathini@in.ibm.com>

> ---
>   arch/powerpc/kexec/file_load_64.c | 10 ++++++----
>   1 file changed, 6 insertions(+), 4 deletions(-)
> 
> diff --git a/arch/powerpc/kexec/file_load_64.c b/arch/powerpc/kexec/file_load_64.c
> index af8854f9eae3..3caee570e79b 100644
> --- a/arch/powerpc/kexec/file_load_64.c
> +++ b/arch/powerpc/kexec/file_load_64.c
> @@ -989,10 +989,12 @@ unsigned int kexec_extra_fdt_size_ppc64(struct kimage *image)
>   	 * linux,drconf-usable-memory properties. Get an approximate on the
>   	 * number of usable memory entries and use for FDT size estimation.
>   	 */
> -	usm_entries = ((memblock_end_of_DRAM() / drmem_lmb_size()) +
> -		       (2 * (resource_size(&crashk_res) / drmem_lmb_size())));
> -
> -	extra_size = (unsigned int)(usm_entries * sizeof(u64));
> +	if (drmem_lmb_size()) {
> +		usm_entries = ((memblock_end_of_DRAM() / drmem_lmb_size()) +
> +			       (2 * (resource_size(&crashk_res) / drmem_lmb_size())));
> +		extra_size = (unsigned int)(usm_entries * sizeof(u64));
> +	} else
> +		extra_size = 0;
>   
>   	/*
>   	 * Get the number of CPU nodes in the current DT. This allows to
Michael Ellerman Feb. 5, 2023, 9:41 a.m. UTC | #5
On Mon, 30 Jan 2023 12:47:07 +1100, Michael Ellerman wrote:
> In kexec_extra_fdt_size_ppc64() there's logic to estimate how much
> extra space will be needed in the device tree for some memory related
> properties.
> 
> That logic uses the size of RAM divided by drmem_lmb_size() to do the
> estimation. However drmem_lmb_size() can be zero if the machine has no
> hotpluggable memory configured, which is the case when booting with qemu
> and no maxmem=x parameter is passed (the default).
> 
> [...]

Applied to powerpc/fixes.

[1/1] powerpc/kexec_file: Fix division by zero in extra size estimation
      https://git.kernel.org/powerpc/c/7294194b47e994753a86eee8cf1c61f3f36458a3

cheers
diff mbox series

Patch

diff --git a/arch/powerpc/kexec/file_load_64.c b/arch/powerpc/kexec/file_load_64.c
index af8854f9eae3..3caee570e79b 100644
--- a/arch/powerpc/kexec/file_load_64.c
+++ b/arch/powerpc/kexec/file_load_64.c
@@ -989,10 +989,12 @@  unsigned int kexec_extra_fdt_size_ppc64(struct kimage *image)
 	 * linux,drconf-usable-memory properties. Get an approximate on the
 	 * number of usable memory entries and use for FDT size estimation.
 	 */
-	usm_entries = ((memblock_end_of_DRAM() / drmem_lmb_size()) +
-		       (2 * (resource_size(&crashk_res) / drmem_lmb_size())));
-
-	extra_size = (unsigned int)(usm_entries * sizeof(u64));
+	if (drmem_lmb_size()) {
+		usm_entries = ((memblock_end_of_DRAM() / drmem_lmb_size()) +
+			       (2 * (resource_size(&crashk_res) / drmem_lmb_size())));
+		extra_size = (unsigned int)(usm_entries * sizeof(u64));
+	} else
+		extra_size = 0;
 
 	/*
 	 * Get the number of CPU nodes in the current DT. This allows to