On 6/6/25 02:17, Jiri Slaby wrote:
> Given this is the second time I hit a bug with this, perhaps introduce
> an EXPERIMENTAL CONFIG option, so that random users can simply disable
> it if an issue occurs? Without the need of patching random userspace and
> changing random kernel headers?

What about something like the attached (untested) patch? That should at
least get folks back to the old, universal working behavior even when
using new compilers.
From 08d98b4fa08ba76be96e406b53ae69946842f6a9 Mon Sep 17 00:00:00 2001
From: Dave Hansen <[email protected]>
Date: Fri, 6 Jun 2025 08:33:29 -0700
Subject: [PATCH] compiler.h: Enable config choice for using unqualified casts

TYPEOF_UNQUAL() has a noble goal of letting normal compilers do more
of the job normally reserved for sparse. But it has caused (or
exposed) a number of nasty bugs and is not quite ready for prime time.
Even nastier, some of these issues need separate userspace fixes.

Right now, __typeof_unqual__ will be whenever the compiler supports
it. Restrict it to cases where users have opted in with a new Kconfig
option. This option can either be removed or have its default polarity
flipped when userspace is widely fixed up.

Signed-off-by: Dave Hansen <[email protected]>
---
 include/linux/compiler.h |  2 +-
 init/Kconfig             | 11 +++++++++++
 2 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/include/linux/compiler.h b/include/linux/compiler.h
index 27725f1ab5abc..3efa93f8eca66 100644
--- a/include/linux/compiler.h
+++ b/include/linux/compiler.h
@@ -232,7 +232,7 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,
  * XXX: Remove test for __CHECKER__ once
  * sparse learns about __typeof_unqual__().
  */
-#if CC_HAS_TYPEOF_UNQUAL && !defined(__CHECKER__)
+#if defined(CONFIG_CC_USE_TYPEOF_UNQUAL) && CC_HAS_TYPEOF_UNQUAL && !defined(__CHECKER__)
 # define USE_TYPEOF_UNQUAL 1
 #endif
 
diff --git a/init/Kconfig b/init/Kconfig
index 63f5974b9fa6e..74e5e8d640750 100644
--- a/init/Kconfig
+++ b/init/Kconfig
@@ -1489,6 +1489,17 @@ config CC_OPTIMIZE_FOR_SIZE
 
 endchoice
 
+config CC_USE_TYPEOF_UNQUAL
+	bool "Use compiler-provided unqualified casts (EXPERIMENTAL)"
+	depends on EXPERT
+	help
+	  Newer compilers have the ability to do "unqualified" casts which
+	  strip out type qualifiers like 'const'. Kernel builds can
+	  leverage these to do more strict type checking with normal
+	  compilers instead of resorting to using sparse.
+
+	  If unsure, say N here.
+
 config HAVE_LD_DEAD_CODE_DATA_ELIMINATION
 	bool
 	help
-- 
2.34.1

Reply via email to