Skip to contentSkip to conversation

Use feature bit names in clearcpuid=

RFC PATCH In review Borislav Petkov 3 participants 7 messages Sep 20, 2020 – Sep 22, 2020

Subsystems (2)
lore
Share & tools
lore

Borislav Petkov

author maintainer
#0 lore raw

RFC PATCH 1/1 Use feature bit names in clearcpuid=

Hi,

so tglx hates this clearcpuid= interface where you have to give the X86_FEATURE array indices in order to disable a feature bit for testing. Below is a first attempt (lightly tested in a VM only) to accept the bit names from /proc/cpuinfo too.

I say "too" because not all feature bits have names and we would still have to support the numbers. Yeah, yuck.

An exemplary cmdline would then be something like:

clearcpuid=de,440,smca,succory,bmi1,3dnow ("succory" is wrong on purpose).

and it says:

[    0.000000] Clearing CPUID bits: de 13:24 smca bmi1 3dnow

Also, I'm thinking we should taint the kernel when this option is used.

Thoughts?

---

arch/x86/include/asm/cpufeature.h +5 -2
index 59bf91c57aa8..10b65045fc37 100644
@@ -32,14 +32,17 @@ enum cpuid_leafs
 	CPUID_7_EDX,
 };
 
+#define X86_CAP_FMT_BARE "%d:%d"
+#define x86_cap_flag_bare(flag) ((flag) >> 5), ((flag) & 31)
+
 #ifdef CONFIG_X86_FEATURE_NAMES
 extern const char * const x86_cap_flags[NCAPINTS*32];
 extern const char * const x86_power_flags[32];
 #define X86_CAP_FMT "%s"
 #define x86_cap_flag(flag) x86_cap_flags[flag]
 #else
-#define X86_CAP_FMT "%d:%d"
-#define x86_cap_flag(flag) ((flag) >> 5), ((flag) & 31)
+#define X86_CAP_FMT X86_CAP_FMT_BARE
+#define x86_cap_flag(flag) x86_cap_flag_bare((flag))
 #endif
 
 /*
arch/x86/kernel/fpu/init.c +39 -15

68 lines not on this page · raw message

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#1 lore raw
Quoted 15 lines · Borislav Petkov

Hi,

so tglx hates this clearcpuid= interface where you have to give the X86_FEATURE array indices in order to disable a feature bit for testing. Below is a first attempt (lightly tested in a VM only) to accept the bit names from /proc/cpuinfo too.

I say "too" because not all feature bits have names and we would still have to support the numbers. Yeah, yuck.

An exemplary cmdline would then be something like:

clearcpuid=de,440,smca,succory,bmi1,3dnow ("succory" is wrong on purpose).

Quoted 7 lines

and it says:

[    0.000000] Clearing CPUID bits: de 13:24 smca bmi1 3dnow

Also, I'm thinking we should taint the kernel when this option is used.

Thoughts?

I like it. Allowing 13:24 as input would be icing on the cake :)

Small comments below.

Quoted 13 lines
arch/x86/kernel/fpu/init.c +0 -9
@@ -273,21 +273,45 @@ static void __init fpu__init_parse_early_param(void)
 		return;
 
 	pr_info("Clearing CPUID bits:");
-	do {
-		res = get_option(&argptr, &bit);
-		if (res == 0 || res == 3)
-			break;
-
-		/* If the argument was too long, the last bit may be cut off */
-		if (res == 1 && arglen >= sizeof(arg))
-			break;
-

arch/x86/kernel/fpu/init.c static void __init fpu__init_parse_early_param(void) · line 282

Quoted 10 lines
arch/x86/kernel/fpu/init.c +7 -3
-		if (bit >= 0 && bit < NCAPINTS * 32) {
-			pr_cont(" " X86_CAP_FMT, x86_cap_flag(bit));
-			setup_clear_cpu_cap(bit);
+
+	while (argptr) {
+		int i;
+
+		opt = (strsep(&argptr, ","));
+		if (!opt)
+			continue;

The !opt check is unnecessary: strsep() returns NULL iff argptr is NULL on entry. The parentheses around strsep() also look odd.

Review static void __init fpu__init_parse_early_param(void) · line 284

Quoted 2 lines
(quoted diff) +2 -0
+
+		if (!kstrtoint(opt, 10, &bit)) {

Could make bit unsigned and use kstrtouint().

arch/x86/kernel/fpu/init.c static void __init fpu__init_parse_early_param(void) · line 294

Quoted 10 lines
arch/x86/kernel/fpu/init.c +10 -0
+			if (bit >= 0 && bit < NCAPINTS * 32) {
+				if (!x86_cap_flag(bit))
+					pr_cont(" " X86_CAP_FMT_BARE, x86_cap_flag_bare(bit));
+				else
+					pr_cont(" " X86_CAP_FMT, x86_cap_flag(bit));
+
+				setup_clear_cpu_cap(bit);
+				taint++;
+				continue;
+			}

Could always continue if it was a number, even if it was invalid, since that shouldn't match a name in any case?

Quoted 4 lines
arch/x86/kernel/fpu/init.c +2 -1
 		}
-	} while (res == 2);
+
+#ifdef CONFIG_X86_FEATURE_NAMES

arch/x86/kernel/fpu/init.c static void __init fpu__init_parse_early_param(void) · line 307

Quoted 10 lines
arch/x86/kernel/fpu/init.c +10 -0
+		for (i = 0; i < 32 * NCAPINTS; i++) {
+			if (!x86_cap_flags[i])
+				continue;
+
+			if (strcmp(x86_cap_flags[i], opt))
+				continue;
+
+			pr_cont(" %s", opt);
+			setup_clear_cpu_cap(i);
+			taint++;

We could break out of the loop here -- we can't have multiple bits with the same name, right?

Quoted 16 lines
arch/x86/kernel/fpu/init.c +6 -0
+		}
+#endif
+	}
 	pr_cont("\n");
+
+	if (taint)
+		add_taint(TAINT_CPU_OUT_OF_SPEC, LOCKDEP_STILL_OK);
 }
 
 /*
Signature · 5 lines
-- 
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
author maintainer
#2 lore raw
Quoted 1 line · Arvind Sankar

Allowing 13:24 as input would be icing on the cake :)

Well, I'm kinda "meh" on that. Why, you ask?

Well, whether the user multiplies two integers or the kernel does it for her/him, I'd prefer the user.

But that's not even the problem - whether the product of the word number and the bit within that word, or the two supplied as a pair - either is the wrong interface. It is ugly and not even close to even beginning to be user-friendly.

However, we can't make it fully user-friendly yet because not all bits have names. :-\

But you know what, that doesn't matter too because that clearcpuid= thing is mainly for poking at sh*t and testing, not meant for users. Thus the tainting...

So I guess the one who needs it, can go the minute distance and do the multiplication. According to that argument, adding the string parsing is not really needed too, but it is simple enough so WTH.

I've incorporated all your other comments, see below.

Thx!

---

arch/x86/include/asm/cpufeature.h +5 -2
index 59bf91c57aa8..10b65045fc37 100644
@@ -32,14 +32,17 @@ enum cpuid_leafs
 	CPUID_7_EDX,
 };
 
+#define X86_CAP_FMT_BARE "%d:%d"
+#define x86_cap_flag_bare(flag) ((flag) >> 5), ((flag) & 31)
+
 #ifdef CONFIG_X86_FEATURE_NAMES
 extern const char * const x86_cap_flags[NCAPINTS*32];
 extern const char * const x86_power_flags[32];
 #define X86_CAP_FMT "%s"
 #define x86_cap_flag(flag) x86_cap_flags[flag]
 #else
-#define X86_CAP_FMT "%d:%d"
-#define x86_cap_flag(flag) ((flag) >> 5), ((flag) & 31)
+#define X86_CAP_FMT X86_CAP_FMT_BARE
+#define x86_cap_flag(flag) x86_cap_flag_bare((flag))
 #endif
 
 /*
arch/x86/kernel/fpu/init.c +39 -12

68 lines not on this page · raw message

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#3 lore raw
Quoted 20 lines · Borislav Petkov
Quoted 1 line · Arvind Sankar

Allowing 13:24 as input would be icing on the cake :)

Well, I'm kinda "meh" on that. Why, you ask?

Well, whether the user multiplies two integers or the kernel does it for her/him, I'd prefer the user.

But that's not even the problem - whether the product of the word number and the bit within that word, or the two supplied as a pair - either is the wrong interface. It is ugly and not even close to even beginning to be user-friendly.

However, we can't make it fully user-friendly yet because not all bits have names. :-\

But you know what, that doesn't matter too because that clearcpuid= thing is mainly for poking at sh*t and testing, not meant for users. Thus the tainting...

Quoted 8 lines

So I guess the one who needs it, can go the minute distance and do the multiplication. According to that argument, adding the string parsing is not really needed too, but it is simple enough so WTH.

I've incorporated all your other comments, see below.

Thx!

Thanks. Maybe also mention in the documentation that names can now be used.

author maintainer
#4 lore raw
Quoted 2 lines · Arvind Sankar

Thanks. Maybe also mention in the documentation that names can now be used.

Already did it as an incremental ontop. Will merge later:

---

Documentation/admin-guide/kernel-parameters.txt +8 -3
index ffe864390c5a..49988e891d7f 100644
@@ -577,12 +577,17 @@
 			loops can be debugged more effectively on production
 			systems.
 
-	clearcpuid=BITNUM[,BITNUM...] [X86]
+	clearcpuid=X[,X...] [X86]
 			Disable CPUID feature X for the kernel. See
 			arch/x86/include/asm/cpufeatures.h for the valid bit
-			numbers. Note the Linux specific bits are not necessarily
-			stable over kernel options, but the vendor specific
+			numbers X. Note the Linux-specific bits are not necessarily
+			stable over kernel options, but the vendor-specific
 			ones should be.
+			X can also be a string as appearing in the flags: line
+			in /proc/cpuinfo which does not have the above
+			instability issue. However, not all features have names
+			in /proc/cpuinfo.
+			Note that using this option will taint your kernel.
 			Also note that user programs calling CPUID directly
 			or using the feature without checking anything
 			will still see it. This just prevents it from
Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#5 lore raw
In reply to Borislav Petkov
Quoted 15 lines · Borislav Petkov

Hi,

so tglx hates this clearcpuid= interface where you have to give the X86_FEATURE array indices in order to disable a feature bit for testing. Below is a first attempt (lightly tested in a VM only) to accept the bit names from /proc/cpuinfo too.

I say "too" because not all feature bits have names and we would still have to support the numbers. Yeah, yuck.

An exemplary cmdline would then be something like:

clearcpuid=de,440,smca,succory,bmi1,3dnow ("succory" is wrong on purpose).

Quoted 7 lines

and it says:

[    0.000000] Clearing CPUID bits: de 13:24 smca bmi1 3dnow

Also, I'm thinking we should taint the kernel when this option is used.

Thoughts?

Yes, instead of making it differently horrible, can we finally remove that nonsense which should have never been there in the first place?

Thanks,

tglx

author maintainer
#6 lore raw
Quoted 18 lines · Thomas Gleixner
Quoted 17 lines · Borislav Petkov

Hi,

so tglx hates this clearcpuid= interface where you have to give the X86_FEATURE array indices in order to disable a feature bit for testing. Below is a first attempt (lightly tested in a VM only) to accept the bit names from /proc/cpuinfo too.

I say "too" because not all feature bits have names and we would still have to support the numbers. Yeah, yuck.

An exemplary cmdline would then be something like:

clearcpuid=de,440,smca,succory,bmi1,3dnow ("succory" is wrong on purpose).

and it says:

Quoted 8 lines
Quoted 5 lines
[    0.000000] Clearing CPUID bits: de 13:24 smca bmi1 3dnow

Also, I'm thinking we should taint the kernel when this option is used.

Thoughts?

Yes, instead of making it differently horrible, can we finally remove that nonsense which should have never been there in the first place?

Fine with me - I don't need it.

I believe dhansen has a use-case or two, CCed.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette