linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] arm64: kvm: avoid overflow in integer division
@ 2023-05-17 20:23 Arnd Bergmann
  2023-05-18  7:30 ` Marc Zyngier
  2023-05-18 17:45 ` Oliver Upton
  0 siblings, 2 replies; 4+ messages in thread
From: Arnd Bergmann @ 2023-05-17 20:23 UTC (permalink / raw)
  To: Marc Zyngier, Oliver Upton, Catalin Marinas, Will Deacon,
	Shaoqin Huang, Ricardo Koller, Gavin Shan
  Cc: Arnd Bergmann, James Morse, Suzuki K Poulose, Zenghui Yu,
	Cornelia Huck, linux-arm-kernel, kvmarm, linux-kernel

From: Arnd Bergmann <arnd@arndb.de>

The newly added kvm_mmu_split_nr_page_tables() function uses DIV_ROUND_DOWN_ULL()
to divide 64-bit addresses, but this requires a 32-bit divisior, and PUD_SIZE
may exceed that when 64KB pages are used:

arch/arm64/kvm/mmu.c: In function 'kvm_mmu_split_nr_page_tables':
include/linux/math.h:42:64: error: conversion from 'long unsigned int' to 'u32' {aka 'unsigned int'} changes value from '68719476736' to '0' [-Werror=overflow]
   42 |         DIV_ROUND_DOWN_ULL((unsigned long long)(ll) + (d) - 1, (d))
      |                                                                ^~~
include/linux/math.h:39:47: note: in definition of macro 'DIV_ROUND_DOWN_ULL'
   39 | #define DIV_ROUND_DOWN_ULL(ll, d) div_u64(ll, d)
      |                                               ^
arch/arm64/kvm/mmu.c:95:22: note: in expansion of macro 'DIV_ROUND_UP_ULL'
   95 |                 n += DIV_ROUND_UP_ULL(range, PUD_SIZE);
      |                      ^~~~~~~~~~~~~~~~

Since this code is only used on 64-bit targets, DIV_ROUND_UP() can deal with this
more easily, as it already takes 64-bit arguments.

Fixes: e7bf7a490c68 ("KVM: arm64: Split huge pages when dirty logging is enabled")
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
---
 arch/arm64/kvm/mmu.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c
index 3386bd28d267..6db9ef288ec3 100644
--- a/arch/arm64/kvm/mmu.c
+++ b/arch/arm64/kvm/mmu.c
@@ -92,8 +92,8 @@ static int kvm_mmu_split_nr_page_tables(u64 range)
 	int n = 0;
 
 	if (KVM_PGTABLE_MIN_BLOCK_LEVEL < 2)
-		n += DIV_ROUND_UP_ULL(range, PUD_SIZE);
-	n += DIV_ROUND_UP_ULL(range, PMD_SIZE);
+		n += DIV_ROUND_UP(range, PUD_SIZE);
+	n += DIV_ROUND_UP(range, PMD_SIZE);
 	return n;
 }
 
-- 
2.39.2


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] arm64: kvm: avoid overflow in integer division
  2023-05-17 20:23 [PATCH] arm64: kvm: avoid overflow in integer division Arnd Bergmann
@ 2023-05-18  7:30 ` Marc Zyngier
  2023-05-18 12:14   ` Arnd Bergmann
  2023-05-18 17:45 ` Oliver Upton
  1 sibling, 1 reply; 4+ messages in thread
From: Marc Zyngier @ 2023-05-18  7:30 UTC (permalink / raw)
  To: Arnd Bergmann
  Cc: Oliver Upton, Catalin Marinas, Will Deacon, Shaoqin Huang,
	Ricardo Koller, Gavin Shan, Arnd Bergmann, James Morse,
	Suzuki K Poulose, Zenghui Yu, Cornelia Huck, linux-arm-kernel,
	kvmarm, linux-kernel

On Wed, 17 May 2023 21:23:39 +0100,
Arnd Bergmann <arnd@kernel.org> wrote:
> 
> From: Arnd Bergmann <arnd@arndb.de>
> 
> The newly added kvm_mmu_split_nr_page_tables() function uses DIV_ROUND_DOWN_ULL()
> to divide 64-bit addresses, but this requires a 32-bit divisior, and PUD_SIZE
> may exceed that when 64KB pages are used:
> 
> arch/arm64/kvm/mmu.c: In function 'kvm_mmu_split_nr_page_tables':
> include/linux/math.h:42:64: error: conversion from 'long unsigned int' to 'u32' {aka 'unsigned int'} changes value from '68719476736' to '0' [-Werror=overflow]
>    42 |         DIV_ROUND_DOWN_ULL((unsigned long long)(ll) + (d) - 1, (d))
>       |                                                                ^~~
> include/linux/math.h:39:47: note: in definition of macro 'DIV_ROUND_DOWN_ULL'
>    39 | #define DIV_ROUND_DOWN_ULL(ll, d) div_u64(ll, d)
>       |                                               ^
> arch/arm64/kvm/mmu.c:95:22: note: in expansion of macro 'DIV_ROUND_UP_ULL'
>    95 |                 n += DIV_ROUND_UP_ULL(range, PUD_SIZE);
>       |                      ^~~~~~~~~~~~~~~~
> 
> Since this code is only used on 64-bit targets, DIV_ROUND_UP() can deal with this
> more easily, as it already takes 64-bit arguments.
> 
> Fixes: e7bf7a490c68 ("KVM: arm64: Split huge pages when dirty logging is enabled")
> Signed-off-by: Arnd Bergmann <arnd@arndb.de>
> ---
>  arch/arm64/kvm/mmu.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c
> index 3386bd28d267..6db9ef288ec3 100644
> --- a/arch/arm64/kvm/mmu.c
> +++ b/arch/arm64/kvm/mmu.c
> @@ -92,8 +92,8 @@ static int kvm_mmu_split_nr_page_tables(u64 range)
>  	int n = 0;
>  
>  	if (KVM_PGTABLE_MIN_BLOCK_LEVEL < 2)
> -		n += DIV_ROUND_UP_ULL(range, PUD_SIZE);
> -	n += DIV_ROUND_UP_ULL(range, PMD_SIZE);
> +		n += DIV_ROUND_UP(range, PUD_SIZE);
> +	n += DIV_ROUND_UP(range, PMD_SIZE);
>  	return n;
>  }

This is against -next, right? Oliver, I assume you'll take this as a
fix for Ricardo's series?

Thanks,

	M.

-- 
Without deviation from the norm, progress is not possible.

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] arm64: kvm: avoid overflow in integer division
  2023-05-18  7:30 ` Marc Zyngier
@ 2023-05-18 12:14   ` Arnd Bergmann
  0 siblings, 0 replies; 4+ messages in thread
From: Arnd Bergmann @ 2023-05-18 12:14 UTC (permalink / raw)
  To: Marc Zyngier, Arnd Bergmann
  Cc: Oliver Upton, Catalin Marinas, Will Deacon, Shaoqin Huang,
	Ricardo Koller, Gavin Shan, James Morse, Suzuki K Poulose,
	Zenghui Yu, Cornelia Huck, linux-arm-kernel, kvmarm, linux-kernel

On Thu, May 18, 2023, at 09:30, Marc Zyngier wrote:
> On Wed, 17 May 2023 21:23:39 +0100,
> Arnd Bergmann <arnd@kernel.org> wrote:
>> diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c
>> index 3386bd28d267..6db9ef288ec3 100644
>> --- a/arch/arm64/kvm/mmu.c
>> +++ b/arch/arm64/kvm/mmu.c
>> @@ -92,8 +92,8 @@ static int kvm_mmu_split_nr_page_tables(u64 range)
>>  	int n = 0;
>>  
>>  	if (KVM_PGTABLE_MIN_BLOCK_LEVEL < 2)
>> -		n += DIV_ROUND_UP_ULL(range, PUD_SIZE);
>> -	n += DIV_ROUND_UP_ULL(range, PMD_SIZE);
>> +		n += DIV_ROUND_UP(range, PUD_SIZE);
>> +	n += DIV_ROUND_UP(range, PMD_SIZE);
>>  	return n;
>>  }
>
> This is against -next, right? Oliver, I assume you'll take this as a
> fix for Ricardo's series?

Yes, correct, I saw it after rebasing my tree from 6.4-rc2 to the
latest -next.

     Arnd

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] arm64: kvm: avoid overflow in integer division
  2023-05-17 20:23 [PATCH] arm64: kvm: avoid overflow in integer division Arnd Bergmann
  2023-05-18  7:30 ` Marc Zyngier
@ 2023-05-18 17:45 ` Oliver Upton
  1 sibling, 0 replies; 4+ messages in thread
From: Oliver Upton @ 2023-05-18 17:45 UTC (permalink / raw)
  To: Gavin Shan, Marc Zyngier, Catalin Marinas, Shaoqin Huang,
	Ricardo Koller, Will Deacon, Arnd Bergmann
  Cc: Oliver Upton, Arnd Bergmann, kvmarm, Cornelia Huck, linux-kernel,
	Zenghui Yu, Suzuki K Poulose, James Morse, linux-arm-kernel

On Wed, 17 May 2023 22:23:39 +0200, Arnd Bergmann wrote:
> From: Arnd Bergmann <arnd@arndb.de>
> 
> The newly added kvm_mmu_split_nr_page_tables() function uses DIV_ROUND_DOWN_ULL()
> to divide 64-bit addresses, but this requires a 32-bit divisior, and PUD_SIZE
> may exceed that when 64KB pages are used:
> 
> arch/arm64/kvm/mmu.c: In function 'kvm_mmu_split_nr_page_tables':
> include/linux/math.h:42:64: error: conversion from 'long unsigned int' to 'u32' {aka 'unsigned int'} changes value from '68719476736' to '0' [-Werror=overflow]
>    42 |         DIV_ROUND_DOWN_ULL((unsigned long long)(ll) + (d) - 1, (d))
>       |                                                                ^~~
> include/linux/math.h:39:47: note: in definition of macro 'DIV_ROUND_DOWN_ULL'
>    39 | #define DIV_ROUND_DOWN_ULL(ll, d) div_u64(ll, d)
>       |                                               ^
> arch/arm64/kvm/mmu.c:95:22: note: in expansion of macro 'DIV_ROUND_UP_ULL'
>    95 |                 n += DIV_ROUND_UP_ULL(range, PUD_SIZE);
>       |                      ^~~~~~~~~~~~~~~~
> 
> [...]

Applied to kvmarm/next, thanks!

[1/1] arm64: kvm: avoid overflow in integer division
      https://git.kernel.org/kvmarm/kvmarm/c/14c3555f055d

--
Best,
Oliver

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-05-18 17:46 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-05-17 20:23 [PATCH] arm64: kvm: avoid overflow in integer division Arnd Bergmann
2023-05-18  7:30 ` Marc Zyngier
2023-05-18 12:14   ` Arnd Bergmann
2023-05-18 17:45 ` Oliver Upton

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).