linux-stable/arch/x86/lib
Colin Ian King 0be645792c x86/lib: Fix overflow when counting digits
[ Upstream commit a24d61c609 ]

tl;dr: The num_digits() function has a theoretical overflow issue.
But it doesn't affect any actual in-tree users.  Fix it by using
a larger type for one of the local variables.

Long version:

There is an overflow in variable m in function num_digits when val
is >= 1410065408 which leads to the digit calculation loop to
iterate more times than required. This results in either more
digits being counted or in some cases (for example where val is
1932683193) the value of m eventually overflows to zero and the
while loop spins forever).

Currently the function num_digits is currently only being used for
small values of val in the SMP boot stage for digit counting on the
number of cpus and NUMA nodes, so the overflow is never encountered.
However it is useful to fix the overflow issue in case the function
is used for other purposes in the future. (The issue was discovered
while investigating the digit counting performance in various
kernel helper functions rather than any real-world use-case).

The simplest fix is to make m a long long, the overhead in
multiplication speed for a long long is very minor for small values
of val less than 10000 on modern processors. The alternative
fix is to replace the multiplication with a constant division
by 10 loop (this compiles down to an multiplication and shift)
without needing to make m a long long, but this is slightly slower
than the fix in this commit when measured on a range of x86
processors).

[ dhansen: subject and changelog tweaks ]

Fixes: 646e29a178 ("x86: Improve the printout of the SMP bootup CPU table")
Signed-off-by: Colin Ian King <colin.i.king@gmail.com>
Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com>
Link: https://lore.kernel.org/all/20231102174901.2590325-1-colin.i.king%40gmail.com
Signed-off-by: Sasha Levin <sashal@kernel.org>
2024-01-25 15:27:18 -08:00
..
.gitignore
atomic64_32.c
atomic64_386_32.S x86: Prepare asm files for straight-line-speculation 2021-12-08 12:25:37 +01:00
atomic64_cx8_32.S x86: Prepare asm files for straight-line-speculation 2021-12-08 12:25:37 +01:00
cache-smp.c
checksum_32.S x86/checksum_32: Remove .fixup usage 2021-12-11 09:09:49 +01:00
clear_page_64.S x86: fix clear_user_rep_good() exception handling annotation 2023-05-17 11:53:59 +02:00
cmdline.c
cmpxchg8b_emu.S x86: Prepare asm files for straight-line-speculation 2021-12-08 12:25:37 +01:00
cmpxchg16b_emu.S x86: Prepare asm files for straight-line-speculation 2021-12-08 12:25:37 +01:00
copy_mc.c iov_iter, x86: Be consistent about the __user tag on copy_mc_to_user() 2023-11-20 11:51:49 +01:00
copy_mc_64.S x86/copy_mc_64: Remove .fixup usage 2021-12-11 09:09:46 +01:00
copy_page_64.S x86: Prepare asm files for straight-line-speculation 2021-12-08 12:25:37 +01:00
copy_user_64.S x86/uaccess: Don't jump between functions 2022-04-19 21:58:53 +02:00
cpu.c
csum-copy_64.S x86: Prepare asm files for straight-line-speculation 2021-12-08 12:25:37 +01:00
csum-partial_64.c uml/x86: use x86 load_unaligned_zeropad() 2022-01-30 21:26:39 -05:00
csum-wrappers_64.c net: unexport csum_and_copy_{from,to}_user 2022-04-29 14:37:59 -07:00
delay.c x86/delay: Fix the wrong asm constraint in delay_loop() 2022-04-05 21:21:57 +02:00
error-inject.c x86/ibt: Annotate text references 2022-03-15 10:32:40 +01:00
getuser.S x86: Prepare asm files for straight-line-speculation 2021-12-08 12:25:37 +01:00
hweight.S x86: Prepare asm files for straight-line-speculation 2021-12-08 12:25:37 +01:00
inat.c
insn-eval.c x86/32: Remove lazy GS macros 2022-04-14 14:09:43 +02:00
insn.c x86/insn: Use get_unaligned() instead of memcpy() 2021-10-06 11:56:37 +02:00
iomap_copy_64.S x86/asm: Fix an assembler warning with current binutils 2023-01-24 07:24:31 +01:00
iomem.c x86: kmsan: handle open-coded assembly in lib/iomem.c 2022-10-03 14:03:24 -07:00
kaslr.c x86/kaslr: Fix build warning in KASLR code in boot stub 2022-04-11 09:41:12 +02:00
Makefile x86: kasan: kmsan: support CONFIG_GENERIC_CSUM on x86, enable it for KASAN/KMSAN 2022-10-03 14:03:24 -07:00
memcpy_32.c fortify: Detect struct member overflows in memmove() at compile-time 2022-02-13 16:50:06 -08:00
memcpy_64.S x86: Add types to indirectly called assembly functions 2022-09-26 10:13:15 -07:00
memmove_64.S x86: Use return-thunk in asm code 2022-06-27 10:33:58 +02:00
memset_64.S x86: clean up symbol aliasing 2022-02-22 16:21:34 +00:00
misc.c x86/lib: Fix overflow when counting digits 2024-01-25 15:27:18 -08:00
msr-reg-export.c
msr-reg.S x86: Prepare asm files for straight-line-speculation 2021-12-08 12:25:37 +01:00
msr-smp.c
msr.c
pc-conf-reg.c
putuser.S x86/uaccess: Add ENDBR to __put_user_nocheck*() 2022-04-19 21:58:49 +02:00
retpoline.S x86/retpoline,kprobes: Fix position of thunk sections with CONFIG_LTO_CLANG 2023-08-26 13:27:00 +02:00
string_32.c lib/string: Move helper functions out of string.c 2021-09-25 08:20:49 -07:00
strstr_32.c
usercopy.c x86/uaccess: instrument copy_from_user_nmi() 2022-11-08 15:57:24 -08:00
usercopy_32.c x86/usercopy: Remove .fixup usage 2021-12-11 09:09:50 +01:00
usercopy_64.c x86/clear_user: Make it faster 2022-08-18 12:36:42 +02:00
x86-opcode-map.txt x86/insn: Add AVX512-FP16 instructions to the x86 instruction decoder 2022-01-23 20:38:01 +01:00