[PATCH v2] x86/math64: handle #DE in mul_u64_u64_div_u64()

Oleg Nesterov posted 1 patch 2 months, 1 week ago
There is a newer version of this series
arch/x86/include/asm/div64.h | 13 ++++++++-----
1 file changed, 8 insertions(+), 5 deletions(-)
[PATCH v2] x86/math64: handle #DE in mul_u64_u64_div_u64()
Posted by Oleg Nesterov 2 months, 1 week ago
Change mul_u64_u64_div_u64() to return ULONG_MAX if the result doesn't
fit into u64 or div == 0. The former matches the generic implementation
in lib/math/div64.c, the latter doesn't. Perhaps we will add a WARN()
into the fixup_exception() paths later.

No need to use _ASM_EXTABLE_TYPE_REG(), we know that the target register
is pt_regs->ax with offset == 0, so a simple EX_DATA_REG(0) should work
just fine.

Reported-by: Li RongQing <lirongqing@baidu.com>
Link: https://lore.kernel.org/all/78a0d7bb20504c0884d474868eccd858@baidu.com/
Signed-off-by: Oleg Nesterov <oleg@redhat.com>
---
 arch/x86/include/asm/div64.h | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/arch/x86/include/asm/div64.h b/arch/x86/include/asm/div64.h
index 9931e4c7d73f..0bf2c6afe66e 100644
--- a/arch/x86/include/asm/div64.h
+++ b/arch/x86/include/asm/div64.h
@@ -79,18 +79,21 @@ static inline u64 mul_u32_u32(u32 a, u32 b)
 
 #else
 # include <asm-generic/div64.h>
+# include <asm/asm.h>
 
 /*
- * Will generate an #DE when the result doesn't fit u64, could fix with an
- * __ex_table[] entry when it becomes an issue.
+ * Returns ULONG_MAX if the result doesn't fit u64 or div == 0.
  */
 static inline u64 mul_u64_u64_div_u64(u64 a, u64 mul, u64 div)
 {
 	u64 q;
 
-	asm ("mulq %2; divq %3" : "=a" (q)
-				: "a" (a), "rm" (mul), "rm" (div)
-				: "rdx");
+	asm ("mulq %2; 1: divq %3; 2:\n"
+		_ASM_EXTABLE_TYPE(1b, 2b,
+			EX_TYPE_IMM_REG | EX_DATA_REG(0) | EX_DATA_IMM(-1))
+		: "=a" (q)
+		: "a" (a), "rm" (mul), "rm" (div)
+		: "rdx");
 
 	return q;
 }
-- 
2.25.1.362.g51ebf55
RE: [????] [PATCH v2] x86/math64: handle #DE in mul_u64_u64_div_u64()
Posted by Li,Rongqing 2 months, 1 week ago

> Change mul_u64_u64_div_u64() to return ULONG_MAX if the result doesn't fit
> into u64 or div == 0. The former matches the generic implementation in
> lib/math/div64.c, the latter doesn't. Perhaps we will add a WARN() into the
> fixup_exception() paths later.
> 
> No need to use _ASM_EXTABLE_TYPE_REG(), we know that the target register
> is pt_regs->ax with offset == 0, so a simple EX_DATA_REG(0) should work just
> fine.
> 
> Reported-by: Li RongQing <lirongqing@baidu.com>
> Link:
> https://lore.kernel.org/all/78a0d7bb20504c0884d474868eccd858@baidu.com/
> Signed-off-by: Oleg Nesterov <oleg@redhat.com>
> ---

Tested-by: Li RongQing <lirongqing@baidu.com>

Thanks for fixing

Br
Re: [PATCH v2] x86/math64: handle #DE in mul_u64_u64_div_u64()
Posted by David Laight 2 months, 1 week ago
On Sun, 27 Jul 2025 14:34:58 +0200
Oleg Nesterov <oleg@redhat.com> wrote:

> Change mul_u64_u64_div_u64() to return ULONG_MAX if the result doesn't
> fit into u64 or div == 0. The former matches the generic implementation
> in lib/math/div64.c, the latter doesn't. Perhaps we will add a WARN()
> into the fixup_exception() paths later.
> 
> No need to use _ASM_EXTABLE_TYPE_REG(), we know that the target register
> is pt_regs->ax with offset == 0, so a simple EX_DATA_REG(0) should work
> just fine.
> 
> Reported-by: Li RongQing <lirongqing@baidu.com>
> Link: https://lore.kernel.org/all/78a0d7bb20504c0884d474868eccd858@baidu.com/
> Signed-off-by: Oleg Nesterov <oleg@redhat.com>

Reviewed-by: David Laight <david.laight.linux@gmail.com>

> ---
>  arch/x86/include/asm/div64.h | 13 ++++++++-----
>  1 file changed, 8 insertions(+), 5 deletions(-)
> 
> diff --git a/arch/x86/include/asm/div64.h b/arch/x86/include/asm/div64.h
> index 9931e4c7d73f..0bf2c6afe66e 100644
> --- a/arch/x86/include/asm/div64.h
> +++ b/arch/x86/include/asm/div64.h
> @@ -79,18 +79,21 @@ static inline u64 mul_u32_u32(u32 a, u32 b)
>  
>  #else
>  # include <asm-generic/div64.h>
> +# include <asm/asm.h>
>  
>  /*
> - * Will generate an #DE when the result doesn't fit u64, could fix with an
> - * __ex_table[] entry when it becomes an issue.
> + * Returns ULONG_MAX if the result doesn't fit u64 or div == 0.
>   */
>  static inline u64 mul_u64_u64_div_u64(u64 a, u64 mul, u64 div)
>  {
>  	u64 q;
>  
> -	asm ("mulq %2; divq %3" : "=a" (q)
> -				: "a" (a), "rm" (mul), "rm" (div)
> -				: "rdx");
> +	asm ("mulq %2; 1: divq %3; 2:\n"
> +		_ASM_EXTABLE_TYPE(1b, 2b,
> +			EX_TYPE_IMM_REG | EX_DATA_REG(0) | EX_DATA_IMM(-1))
> +		: "=a" (q)
> +		: "a" (a), "rm" (mul), "rm" (div)
> +		: "rdx");
>  
>  	return q;
>  }