Skip to contentSkip to conversation

x86/microcode/intel: Use 64-bit arithmetic instead of 32-bit

PATCH In review Gustavo A. R. Silva 3 participants 5 messages Feb 13, 2018 – Feb 14, 2018

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/microcode/intel: Use 64-bit arithmetic instead of 32-bit

Add suffix ULL to constant 1024 in order to give the compiler complete information about the proper arithmetic to use. Notice that this constant is used in a context that expects an expression of type u64 (64 bits, unsigned).

The expression c->x86_cache_size * 1024 is currently being evaluated using 32-bit arithmetic.

Addresses-Coverity-ID: 1464429

---
 arch/x86/kernel/cpu/microcode/intel.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/cpu/microcode/intel.c +1 -1
index f7c55b0..e5edb92 100644
@@ -982,7 +982,7 @@ static struct microcode_ops microcode_intel_ops = {
 
 static int __init calc_llc_size_per_core(struct cpuinfo_x86 *c)
 {
-	u64 llc_size = c->x86_cache_size * 1024;
+	u64 llc_size = c->x86_cache_size * 1024ULL;
 
 	do_div(llc_size, c->x86_max_cores);
 
Signature · 1 line
2.7.4
#1 lore raw
Quoted 19 lines · Gustavo A. R. Silva

Add suffix ULL to constant 1024 in order to give the compiler complete information about the proper arithmetic to use. Notice that this constant is used in a context that expects an expression of type u64 (64 bits, unsigned).

The expression c->x86_cache_size * 1024 is currently being evaluated using 32-bit arithmetic.

Addresses-Coverity-ID: 1464429

---
 arch/x86/kernel/cpu/microcode/intel.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/cpu/microcode/intel.c +0 -0
index f7c55b0..e5edb92 100644

arch/x86/kernel/cpu/microcode/intel.c static struct microcode_ops microcode_intel_ops = { · line 985

Quoted 5 lines
arch/x86/kernel/cpu/microcode/intel.c +1 -1
@@ -982,7 +982,7 @@ static struct microcode_ops microcode_intel_ops = {
 
 static int __init calc_llc_size_per_core(struct cpuinfo_x86 *c)
 {
-	u64 llc_size = c->x86_cache_size * 1024;
+	u64 llc_size = c->x86_cache_size * 1024ULL;

x86_cache_size is 'int', so you really want to cast c->x86_cache_size to (u64) for correctness sake.

Aside of that the patch is really purely cosmetic at the moment because the largest LLC sizes are still below the 3 digit MB range which fits into 32bit quite well. You'd need to have a CPU with >= 2G LLC to create a problem.

But looking at c->x86_cache_size again. It's int because it's set to -1 initially which is then changed if CPUid or general CPU info gives real information about the cache size. The only place where that matters is the /proc/cpuinfo output:

	if (c->x86_cache_size >= 0)
		seq_printf(m, "cache size\t: %d KB\n", c->x86_cache_size);

which is silly, because that really can be done with:

	if (c->x86_cache_size)

as there is no point in printing 'cache size 0KB', which means x86_cache_size can be made unsigned int, which makes sense because cache size < 0 does not at all.

So instead of doing this purely mechanical cosmetic change to make a static checker shut up, I'd like to see a proper cleanup of that thing.

Thanks,

tglx

#2 lore raw

Hi Thomas,

Quoting Thomas Gleixner <[email protected]>:

Quoted 45 lines · Gustavo A. R. Silva
Quoted 20 lines

Add suffix ULL to constant 1024 in order to give the compiler complete information about the proper arithmetic to use. Notice that this constant is used in a context that expects an expression of type u64 (64 bits, unsigned).

The expression c->x86_cache_size * 1024 is currently being evaluated using 32-bit arithmetic.

Addresses-Coverity-ID: 1464429

---
 arch/x86/kernel/cpu/microcode/intel.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/cpu/microcode/intel.c +0 -0
index f7c55b0..e5edb92 100644

arch/x86/kernel/cpu/microcode/intel.c static struct microcode_ops microcode_intel_ops = { · line 985

Quoted 5 lines
arch/x86/kernel/cpu/microcode/intel.c +1 -1
@@ -982,7 +982,7 @@ static struct microcode_ops microcode_intel_ops = {
 
 static int __init calc_llc_size_per_core(struct cpuinfo_x86 *c)
 {
-	u64 llc_size = c->x86_cache_size * 1024;
+	u64 llc_size = c->x86_cache_size * 1024ULL;

x86_cache_size is 'int', so you really want to cast c->x86_cache_size to (u64) for correctness sake.

Aside of that the patch is really purely cosmetic at the moment because the largest LLC sizes are still below the 3 digit MB range which fits into 32bit quite well. You'd need to have a CPU with >= 2G LLC to create a problem.

But looking at c->x86_cache_size again. It's int because it's set to -1 initially which is then changed if CPUid or general CPU info gives real information about the cache size. The only place where that matters is the /proc/cpuinfo output:

	if (c->x86_cache_size >= 0)
		seq_printf(m, "cache size\t: %d KB\n", c->x86_cache_size);

which is silly, because that really can be done with:

Quoted 9 lines
	if (c->x86_cache_size)

as there is no point in printing 'cache size 0KB', which means x86_cache_size can be made unsigned int, which makes sense because cache size < 0 does not at all.

So instead of doing this purely mechanical cosmetic change to make a static checker shut up, I'd like to see a proper cleanup of that thing.

Yeah, actually I was curious about why x86_cache_size is signed instead of unsigned. You've made it clear now.

I will change it to be of type unsigned int and make the proper changes to the rest of code in which x86_cache_size is being used.

Also, I'm curious about the types of the rest of the related variables:

/* Cache QoS architectural values: */
int x86_cache_max_rmid; /* max index */
int x86_cache_occ_scale; /* scale to bytes */
int x86_power;

Maybe they need some cleanup too.

Thanks for the feedback.
--
Gustavo

#3 lore raw
In reply to Thomas Gleixner
Quoted 10 lines
	if (c->x86_cache_size >= 0)
		seq_printf(m, "cache size\t: %d KB\n", c->x86_cache_size);

which is silly, because that really can be done with:

	if (c->x86_cache_size)

as there is no point in printing 'cache size 0KB', which means x86_cache_size can be made unsigned int, which makes sense because cache size < 0 does not at all.

Currently 0MB means "I know you have no cache" (early slot 1 celeron), while not printing it means 'I have no clue'

Alan

#4 lore raw
Quoted 5 lines · Alan Cox
Quoted 5 lines
	if (c->x86_cache_size >= 0)
		seq_printf(m, "cache size\t: %d KB\n", c->x86_cache_size);

which is silly, because that really can be done with:

Quoted 8 lines
Quoted 5 lines
	if (c->x86_cache_size)

as there is no point in printing 'cache size 0KB', which means x86_cache_size can be made unsigned int, which makes sense because cache size < 0 does not at all.

Currently 0MB means "I know you have no cache" (early slot 1 celeron), while not printing it means 'I have no clue'

Cute. I hope the 3 slot1 celeron users will not go wild if their /proc/cpuinfo now claims to have no clue about caches.

Thanks,

tglx