All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] x86/cpu: fix undefined behavior in bit shift for intel_detect_tlb
@ 2022-10-31 11:43 Gaosheng Cui
  2022-10-31 12:04 ` Alex Shi
  2022-10-31 12:30 ` Peter Zijlstra
  0 siblings, 2 replies; 5+ messages in thread
From: Gaosheng Cui @ 2022-10-31 11:43 UTC (permalink / raw)
  To: tglx, mingo, bp, dave.hansen, x86, hpa, tony.luck,
	pawan.kumar.gupta, pbonzini, chenyi.qiang, jithu.joseph, alexs,
	cuigaosheng1
  Cc: linux-kernel

Shifting signed 32-bit value by 31 bits is undefined, so changing
significant bit to unsigned. The UBSAN warning calltrace like below:

UBSAN: shift-out-of-bounds in arch/x86/kernel/cpu/intel.c:948:21
left shift of 1 by 31 places cannot be represented in type 'int'
Call Trace:
 <TASK>
 dump_stack_lvl+0x7d/0xa5
 dump_stack+0x15/0x1b
 ubsan_epilogue+0xe/0x4e
 __ubsan_handle_shift_out_of_bounds+0x1e7/0x20c
 intel_detect_tlb+0x114/0xbd0
 identify_boot_cpu+0x29/0x9e
 check_bugs+0x2f/0x15a5
 start_kernel+0xc3f/0xc78
 x86_64_start_reservations+0x24/0x2a
 x86_64_start_kernel+0xed/0xf8
 secondary_startup_64_no_verify+0xe5/0xeb
 </TASK>

Fixes: e0ba94f14f74 ("x86/tlb_info: get last level TLB entry number of CPU")
Signed-off-by: Gaosheng Cui <cuigaosheng1@huawei.com>
---
 arch/x86/kernel/cpu/intel.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/x86/kernel/cpu/intel.c b/arch/x86/kernel/cpu/intel.c
index 2d7ea5480ec3..121c1c38162a 100644
--- a/arch/x86/kernel/cpu/intel.c
+++ b/arch/x86/kernel/cpu/intel.c
@@ -945,7 +945,7 @@ static void intel_detect_tlb(struct cpuinfo_x86 *c)
 
 		/* If bit 31 is set, this is an unknown format */
 		for (j = 0 ; j < 3 ; j++)
-			if (regs[j] & (1 << 31))
+			if (regs[j] & (1U << 31))
 				regs[j] = 0;
 
 		/* Byte 0 is level count, not a descriptor */
-- 
2.25.1


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

* Re: [PATCH] x86/cpu: fix undefined behavior in bit shift for intel_detect_tlb
  2022-10-31 11:43 [PATCH] x86/cpu: fix undefined behavior in bit shift for intel_detect_tlb Gaosheng Cui
@ 2022-10-31 12:04 ` Alex Shi
  2022-10-31 12:30 ` Peter Zijlstra
  1 sibling, 0 replies; 5+ messages in thread
From: Alex Shi @ 2022-10-31 12:04 UTC (permalink / raw)
  To: Gaosheng Cui
  Cc: tglx, mingo, bp, dave.hansen, x86, hpa, tony.luck,
	pawan.kumar.gupta, pbonzini, chenyi.qiang, jithu.joseph, alexs,
	linux-kernel

On Mon, Oct 31, 2022 at 7:43 PM Gaosheng Cui <cuigaosheng1@huawei.com> wrote:
>
> Shifting signed 32-bit value by 31 bits is undefined, so changing
> significant bit to unsigned. The UBSAN warning calltrace like below:
>
> UBSAN: shift-out-of-bounds in arch/x86/kernel/cpu/intel.c:948:21
> left shift of 1 by 31 places cannot be represented in type 'int'
> Call Trace:
>  <TASK>
>  dump_stack_lvl+0x7d/0xa5
>  dump_stack+0x15/0x1b
>  ubsan_epilogue+0xe/0x4e
>  __ubsan_handle_shift_out_of_bounds+0x1e7/0x20c
>  intel_detect_tlb+0x114/0xbd0
>  identify_boot_cpu+0x29/0x9e
>  check_bugs+0x2f/0x15a5
>  start_kernel+0xc3f/0xc78
>  x86_64_start_reservations+0x24/0x2a
>  x86_64_start_kernel+0xed/0xf8
>  secondary_startup_64_no_verify+0xe5/0xeb
>  </TASK>
>
> Fixes: e0ba94f14f74 ("x86/tlb_info: get last level TLB entry number of CPU")
> Signed-off-by: Gaosheng Cui <cuigaosheng1@huawei.com>

Reviewed-by: Alex Shi <alexs@kernel.org>

> ---
>  arch/x86/kernel/cpu/intel.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/arch/x86/kernel/cpu/intel.c b/arch/x86/kernel/cpu/intel.c
> index 2d7ea5480ec3..121c1c38162a 100644
> --- a/arch/x86/kernel/cpu/intel.c
> +++ b/arch/x86/kernel/cpu/intel.c
> @@ -945,7 +945,7 @@ static void intel_detect_tlb(struct cpuinfo_x86 *c)
>
>                 /* If bit 31 is set, this is an unknown format */
>                 for (j = 0 ; j < 3 ; j++)
> -                       if (regs[j] & (1 << 31))
> +                       if (regs[j] & (1U << 31))
>                                 regs[j] = 0;
>
>                 /* Byte 0 is level count, not a descriptor */
> --
> 2.25.1
>

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

* Re: [PATCH] x86/cpu: fix undefined behavior in bit shift for intel_detect_tlb
  2022-10-31 11:43 [PATCH] x86/cpu: fix undefined behavior in bit shift for intel_detect_tlb Gaosheng Cui
  2022-10-31 12:04 ` Alex Shi
@ 2022-10-31 12:30 ` Peter Zijlstra
  2022-10-31 12:32   ` Peter Zijlstra
  1 sibling, 1 reply; 5+ messages in thread
From: Peter Zijlstra @ 2022-10-31 12:30 UTC (permalink / raw)
  To: Gaosheng Cui
  Cc: tglx, mingo, bp, dave.hansen, x86, hpa, tony.luck,
	pawan.kumar.gupta, pbonzini, chenyi.qiang, jithu.joseph, alexs,
	linux-kernel

On Mon, Oct 31, 2022 at 07:43:40PM +0800, Gaosheng Cui wrote:
> Shifting signed 32-bit value by 31 bits is undefined, so changing
> significant bit to unsigned. The UBSAN warning calltrace like below:
> 
> UBSAN: shift-out-of-bounds in arch/x86/kernel/cpu/intel.c:948:21
> left shift of 1 by 31 places cannot be represented in type 'int'

Is it really? Shouldn't -fstrict-overflow define this case?

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

* Re: [PATCH] x86/cpu: fix undefined behavior in bit shift for intel_detect_tlb
  2022-10-31 12:30 ` Peter Zijlstra
@ 2022-10-31 12:32   ` Peter Zijlstra
  2022-11-01 11:48     ` cuigaosheng
  0 siblings, 1 reply; 5+ messages in thread
From: Peter Zijlstra @ 2022-10-31 12:32 UTC (permalink / raw)
  To: Gaosheng Cui
  Cc: tglx, mingo, bp, dave.hansen, x86, hpa, tony.luck,
	pawan.kumar.gupta, pbonzini, chenyi.qiang, jithu.joseph, alexs,
	linux-kernel

On Mon, Oct 31, 2022 at 01:30:57PM +0100, Peter Zijlstra wrote:
> On Mon, Oct 31, 2022 at 07:43:40PM +0800, Gaosheng Cui wrote:
> > Shifting signed 32-bit value by 31 bits is undefined, so changing
> > significant bit to unsigned. The UBSAN warning calltrace like below:
> > 
> > UBSAN: shift-out-of-bounds in arch/x86/kernel/cpu/intel.c:948:21
> > left shift of 1 by 31 places cannot be represented in type 'int'
> 
> Is it really? Shouldn't -fstrict-overflow define this case?

-fno-strict-overflow that is, which implies -fwrapv which ensures 2s
complement, at which point shifting signed numbers is fully defined.

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

* Re: [PATCH] x86/cpu: fix undefined behavior in bit shift for intel_detect_tlb
  2022-10-31 12:32   ` Peter Zijlstra
@ 2022-11-01 11:48     ` cuigaosheng
  0 siblings, 0 replies; 5+ messages in thread
From: cuigaosheng @ 2022-11-01 11:48 UTC (permalink / raw)
  To: Peter Zijlstra
  Cc: tglx, mingo, bp, dave.hansen, x86, hpa, tony.luck,
	pawan.kumar.gupta, pbonzini, chenyi.qiang, jithu.joseph, alexs,
	linux-kernel

We don't need to add UBSAN calltrace, so I have merged the patch with another
patch("x86/cpu: replacing the open-coded shift with BIT(x)"), please ignore
this patch,thanks for review this patch!

On 2022/10/31 20:32, Peter Zijlstra wrote:
> On Mon, Oct 31, 2022 at 01:30:57PM +0100, Peter Zijlstra wrote:
>> On Mon, Oct 31, 2022 at 07:43:40PM +0800, Gaosheng Cui wrote:
>>> Shifting signed 32-bit value by 31 bits is undefined, so changing
>>> significant bit to unsigned. The UBSAN warning calltrace like below:
>>>
>>> UBSAN: shift-out-of-bounds in arch/x86/kernel/cpu/intel.c:948:21
>>> left shift of 1 by 31 places cannot be represented in type 'int'
>> Is it really? Shouldn't -fstrict-overflow define this case?
> -fno-strict-overflow that is, which implies -fwrapv which ensures 2s
> complement, at which point shifting signed numbers is fully defined.
>
> .

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

end of thread, other threads:[~2022-11-01 11:51 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-31 11:43 [PATCH] x86/cpu: fix undefined behavior in bit shift for intel_detect_tlb Gaosheng Cui
2022-10-31 12:04 ` Alex Shi
2022-10-31 12:30 ` Peter Zijlstra
2022-10-31 12:32   ` Peter Zijlstra
2022-11-01 11:48     ` cuigaosheng

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.