Skip to contentSkip to conversation

x86/cfi: mark cfi_debug as __maybe_unused

PATCH In review Ali Nasrollahi 2 participants 4 messages Nov 16 – Nov 19 x86

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/cfi: mark cfi_debug as __maybe_unused

When CONFIG_FINEIBT is disabled, cfi_debug is never read, but alternative_instructions() still resets it after calling __apply_fineibt(). Clang 23 diagnoses this as -Wunused-but-set-global with W=1, causing a build failure when CONFIG_WERROR=y. Mark cfi_debug as __maybe_unused to account for configurations without FineIBT.

---
 arch/x86/kernel/alternative.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/alternative.c +1 -1
index efbc11f8c336..cc2bdca06f81 100644
@@ -1195,7 +1195,7 @@ void __init_or_module apply_seal_endbr(s32 *start, s32 *end) { }
 #endif
 
 enum cfi_mode cfi_mode __ro_after_init = __CFI_DEFAULT;
-static bool cfi_debug __ro_after_init;
+static bool cfi_debug __ro_after_init __maybe_unused;
 
 #ifdef CONFIG_FINEIBT_BHI
 bool cfi_bhi __ro_after_init = false;
Signature · 1 line
2.56.1
#1 lore raw
Quoted 7 lines · Ali Nasrollahi

When CONFIG_FINEIBT is disabled, cfi_debug is never read, but alternative_instructions() still resets it after calling __apply_fineibt(). Clang 23 diagnoses this as -Wunused-but-set-global with W=1, causing a build failure when CONFIG_WERROR=y. Mark cfi_debug as __maybe_unused to account for configurations without FineIBT.

Does something like so also work?

---

arch/x86/kernel/alternative.c +2 -2
index efbc11f8c336..9bfc36566e17 100644
@@ -1195,9 +1195,9 @@ void __init_or_module apply_seal_endbr(s32 *start, s32 *end) { }
 #endif
 
 enum cfi_mode cfi_mode __ro_after_init = __CFI_DEFAULT;
-static bool cfi_debug __ro_after_init;
 
 #ifdef CONFIG_FINEIBT_BHI
+static bool cfi_debug __ro_after_init;
 bool cfi_bhi __ro_after_init = false;
 #endif
 
@@ -1894,6 +1894,7 @@ static void __init_or_module __apply_fineibt(s32 *start_retpoline, s32 *end_retp
 		break;
 	}
 
+	cfi_debug = false;
 err:
 	pr_err("Something went horribly wrong trying to rewrite the CFI implementation.\n");
 }
@@ -2281,7 +2282,6 @@ void __init alternative_instructions(void)
 
 	__apply_fineibt(__retpoline_sites, __retpoline_sites_end,
 			__cfi_sites, __cfi_sites_end, true);
-	cfi_debug = false;
 
 	/*
 	 * Rewrite the retpolines, must be done before alternatives since
#2 lore raw
Quoted 36 lines · Peter Zijlstra
Quoted 7 lines · Ali Nasrollahi

When CONFIG_FINEIBT is disabled, cfi_debug is never read, but alternative_instructions() still resets it after calling __apply_fineibt(). Clang 23 diagnoses this as -Wunused-but-set-global with W=1, causing a build failure when CONFIG_WERROR=y. Mark cfi_debug as __maybe_unused to account for configurations without FineIBT.

Does something like so also work?

---

arch/x86/kernel/alternative.c +2 -1
index efbc11f8c336..9bfc36566e17 100644
@@ -1195,9 +1195,9 @@ void __init_or_module apply_seal_endbr(s32 *start, s32 *end) { }
 #endif
 
 enum cfi_mode cfi_mode __ro_after_init = __CFI_DEFAULT;
-static bool cfi_debug __ro_after_init;
 
 #ifdef CONFIG_FINEIBT_BHI
+static bool cfi_debug __ro_after_init;
 bool cfi_bhi __ro_after_init = false;
 #endif
 
@@ -1894,6 +1894,7 @@ static void __init_or_module __apply_fineibt(s32 *start_retpoline, s32 *end_retp
 		break;
 	}
 
+	cfi_debug = false;
 err:
 	pr_err("Something went horribly wrong trying to rewrite the CFI implementation.\n");
 }

arch/x86/kernel/alternative.c void __init alternative_instructions(void) · line 2287

Quoted 7 lines
arch/x86/kernel/alternative.c +0 -1
@@ -2281,7 +2282,6 @@ void __init alternative_instructions(void)
 
 	__apply_fineibt(__retpoline_sites, __retpoline_sites_end,
 			__cfi_sites, __cfi_sites_end, true);
-	cfi_debug = false;
 
 	/*
 	 * Rewrite the retpolines, must be done before alternatives since

Thank you for your review.

Regarding this:

Quoted 4 lines

#ifdef CONFIG_FINEIBT_BHI
+static bool cfi_debug __ro_after_init;
bool cfi_bhi __ro_after_init = false;
#endif

FINEIBT can be enabled while FINEIBT_BHI is disabled, for example when rust is enabled. In that case, the declaration is compiled out while the uses remain, which naturally caused my build to fail with an undeclared cfi_debug. Maybe you meant to put it under under CONFIG_FINEIBT instead?

Additionally, I kept my original patch limited to the warning fix because I wasn't sure about the effects of changing the reset. With your change the CFI_OFF, CFI_KCFI and CFI_FINEIBT cases return before reaching the new assignment. Wouldn't this change the behviour? or perhaps that is expected.

Cheers,

    -- Ali
#3 lore raw
Quoted 47 lines · Ali Nasrollahi
Quoted 36 lines · Peter Zijlstra
Quoted 7 lines · Ali Nasrollahi

When CONFIG_FINEIBT is disabled, cfi_debug is never read, but alternative_instructions() still resets it after calling __apply_fineibt(). Clang 23 diagnoses this as -Wunused-but-set-global with W=1, causing a build failure when CONFIG_WERROR=y. Mark cfi_debug as __maybe_unused to account for configurations without FineIBT.

Does something like so also work?

---

arch/x86/kernel/alternative.c +2 -1
index efbc11f8c336..9bfc36566e17 100644
@@ -1195,9 +1195,9 @@ void __init_or_module apply_seal_endbr(s32 *start, s32 *end) { }
 #endif
 
 enum cfi_mode cfi_mode __ro_after_init = __CFI_DEFAULT;
-static bool cfi_debug __ro_after_init;
 
 #ifdef CONFIG_FINEIBT_BHI
+static bool cfi_debug __ro_after_init;
 bool cfi_bhi __ro_after_init = false;
 #endif
 
@@ -1894,6 +1894,7 @@ static void __init_or_module __apply_fineibt(s32 *start_retpoline, s32 *end_retp
 		break;
 	}
 
+	cfi_debug = false;
 err:
 	pr_err("Something went horribly wrong trying to rewrite the CFI implementation.\n");
 }

arch/x86/kernel/alternative.c void __init alternative_instructions(void) · line 2287

Quoted 7 lines
arch/x86/kernel/alternative.c +0 -1
@@ -2281,7 +2282,6 @@ void __init alternative_instructions(void)
 
 	__apply_fineibt(__retpoline_sites, __retpoline_sites_end,
 			__cfi_sites, __cfi_sites_end, true);
-	cfi_debug = false;
 
 	/*
 	 * Rewrite the retpolines, must be done before alternatives since

Thank you for your review.

Quoted 8 lines

Regarding this:

Quoted 4 lines

#ifdef CONFIG_FINEIBT_BHI
+static bool cfi_debug __ro_after_init;
bool cfi_bhi __ro_after_init = false;
#endif

FINEIBT can be enabled while FINEIBT_BHI is disabled, for example when

Yeah, no, that's me not being able to read. I mean to stick that under CONFIG_FINEIBT.