Skip to contentSkip to conversation

printf: mark errptr() noinline

PATCH In review Tamir Duberstein 6 participants 21 messages Apr 5 – Apr 15

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

PATCH 1/1 printf: mark errptr() noinline

Old GCC can miscompile printf_kunit's errptr() test when branch profiling is enabled. BUILD_BUG_ON(IS_ERR(PTR)) is a constant false expression, but CONFIG_TRACE_BRANCH_PROFILING and CONFIG_PROFILE_ALL_BRANCHES make the IS_ERR() path side-effectful. GCC's IPA splitter can then outline the cold assert arm into errptr.part.* and leave that clone with an unconditional __compiletime_assert_*() call, causing a false build failure.

This started showing up after test_hashed() became a macro and moved its local buffer into errptr(), which changed GCC's inlining and splitting decisions enough to expose the compiler bug.

Mark errptr() noinline to keep it out of that buggy IPA path while preserving the BUILD_BUG_ON(IS_ERR(PTR)) check and the macro-based printf argument checking.

Fixes 9bfa52dac27a ("printf: convert test_hashed into macro")
---
 lib/tests/printf_kunit.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
lib/tests/printf_kunit.c +17 -1
index f6f21b445ece..a8087e8ac826 100644
@@ -749,7 +749,23 @@ static void fourcc_pointer(struct kunit *kunittest)
 	fourcc_pointer_test(kunittest, try_cb, ARRAY_SIZE(try_cb), "%p4cb");
 }
 
-static void
+/*
+ * GCC < 12.1 can miscompile this test when branch profiling is enabled.
+ *
+ * BUILD_BUG_ON(IS_ERR(PTR)) is a constant false expression, but old GCC can
+ * still trip over it after CONFIG_TRACE_BRANCH_PROFILING and
+ * CONFIG_PROFILE_ALL_BRANCHES rewrite the IS_ERR() unlikely() path into
+ * side-effectful branch counter updates. IPA splitting then outlines the cold
+ * assert arm into errptr.part.* and leaves that clone with an unconditional
+ * __compiletime_assert_*() call, so the build fails even though PTR is not an
+ * ERR_PTR.
+ *
+ * Keep this test out of that buggy IPA path so the BUILD_BUG_ON() can stay in
+ * place without open-coding IS_ERR(). This can be removed once the minimum GCC
+ * includes commit 76fe49423047 ("Fix tree-optimization/101941: IPA splitting
+ * out function with error attribute"), which first shipped in GCC 12.1.
+ */
+static noinline void
 errptr(struct kunit *kunittest)
 {
 	test("-1234", "%pe", ERR_PTR(-1234));

---
base-commit: d8a9a4b11a137909e306e50346148fc5c3b63f9d
change-id: 20260405-printf-test-old-gcc-f13fecda6524

Best regards,

Signature · 1 line
Tamir Duberstein <[email protected]>
#1 lore raw
Quoted 58 lines · Tamir Duberstein

Old GCC can miscompile printf_kunit's errptr() test when branch profiling is enabled. BUILD_BUG_ON(IS_ERR(PTR)) is a constant false expression, but CONFIG_TRACE_BRANCH_PROFILING and CONFIG_PROFILE_ALL_BRANCHES make the IS_ERR() path side-effectful. GCC's IPA splitter can then outline the cold assert arm into errptr.part.* and leave that clone with an unconditional __compiletime_assert_*() call, causing a false build failure.

This started showing up after test_hashed() became a macro and moved its local buffer into errptr(), which changed GCC's inlining and splitting decisions enough to expose the compiler bug.

Mark errptr() noinline to keep it out of that buggy IPA path while preserving the BUILD_BUG_ON(IS_ERR(PTR)) check and the macro-based printf argument checking.

Fixes 9bfa52dac27a ("printf: convert test_hashed into macro")
---
 lib/tests/printf_kunit.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
lib/tests/printf_kunit.c +17 -2
index f6f21b445ece..a8087e8ac826 100644
@@ -749,7 +749,23 @@ static void fourcc_pointer(struct kunit *kunittest)
 	fourcc_pointer_test(kunittest, try_cb, ARRAY_SIZE(try_cb), "%p4cb");
 }
 
-static void
+/*
+ * GCC < 12.1 can miscompile this test when branch profiling is enabled.
+ *
+ * BUILD_BUG_ON(IS_ERR(PTR)) is a constant false expression, but old GCC can
+ * still trip over it after CONFIG_TRACE_BRANCH_PROFILING and
+ * CONFIG_PROFILE_ALL_BRANCHES rewrite the IS_ERR() unlikely() path into
+ * side-effectful branch counter updates. IPA splitting then outlines the cold
+ * assert arm into errptr.part.* and leaves that clone with an unconditional
+ * __compiletime_assert_*() call, so the build fails even though PTR is not an
+ * ERR_PTR.
+ *
+ * Keep this test out of that buggy IPA path so the BUILD_BUG_ON() can stay in
+ * place without open-coding IS_ERR(). This can be removed once the minimum GCC
+ * includes commit 76fe49423047 ("Fix tree-optimization/101941: IPA splitting
+ * out function with error attribute"), which first shipped in GCC 12.1.
+ */
+static noinline void
 errptr(struct kunit *kunittest)
 {
 	test("-1234", "%pe", ERR_PTR(-1234));
 
---

base-commit: d8a9a4b11a137909e306e50346148fc5c3b63f9d
change-id: 20260405-printf-test-old-gcc-f13fecda6524

Quoted 5 lines

Best regards,

Signature · 1 line
Tamir Duberstein <[email protected]>

<formletter>

This is not the correct way to submit patches for inclusion in the stable kernel tree. Please read:
https://www.kernel.org/doc/html/latest/process/stable-kernel-rules.html
for how to do this properly.

</formletter>

maintainer
#2 lore raw
In reply to Tamir Duberstein
Quoted 12 lines · On Sun, 05 Apr 2026 13:31:50 -0400 Tamir Duberstein <[email protected]> wrote:

Old GCC can miscompile printf_kunit's errptr() test when branch profiling is enabled. BUILD_BUG_ON(IS_ERR(PTR)) is a constant false expression, but CONFIG_TRACE_BRANCH_PROFILING and CONFIG_PROFILE_ALL_BRANCHES make the IS_ERR() path side-effectful. GCC's IPA splitter can then outline the cold assert arm into errptr.part.* and leave that clone with an unconditional __compiletime_assert_*() call, causing a false build failure.

This started showing up after test_hashed() became a macro and moved its local buffer into errptr(), which changed GCC's inlining and splitting decisions enough to expose the compiler bug.

Quoted 8 lines

Mark errptr() noinline to keep it out of that buggy IPA path while preserving the BUILD_BUG_ON(IS_ERR(PTR)) check and the macro-based printf argument checking.

Fixes 9bfa52dac27a ("printf: convert test_hashed into macro")

Another solution which I would be fine with is:

kernel/trace/Kconfig +1 -0
index e130da35808f..c07e8eadfdd0 100644
@@ -692,6 +692,7 @@ config PROFILE_ANNOTATED_BRANCHES
 config PROFILE_ALL_BRANCHES
 	bool "Profile all if conditionals" if !FORTIFY_SOURCE
 	select TRACE_BRANCH_PROFILING
+	depends on !KUNIT
 	help
 	  This tracer profiles all branch conditions. Every if ()
 	  taken in the kernel is recorded whether it hit or miss.

-- Steve

#3 lore raw
Quoted 32 lines · Steven Rostedt
Quoted 12 lines · On Sun, 05 Apr 2026 13:31:50 -0400 Tamir Duberstein <[email protected]> wrote:

Old GCC can miscompile printf_kunit's errptr() test when branch profiling is enabled. BUILD_BUG_ON(IS_ERR(PTR)) is a constant false expression, but CONFIG_TRACE_BRANCH_PROFILING and CONFIG_PROFILE_ALL_BRANCHES make the IS_ERR() path side-effectful. GCC's IPA splitter can then outline the cold assert arm into errptr.part.* and leave that clone with an unconditional __compiletime_assert_*() call, causing a false build failure.

This started showing up after test_hashed() became a macro and moved its local buffer into errptr(), which changed GCC's inlining and splitting decisions enough to expose the compiler bug.

Quoted 8 lines

Mark errptr() noinline to keep it out of that buggy IPA path while preserving the BUILD_BUG_ON(IS_ERR(PTR)) check and the macro-based printf argument checking.

Fixes 9bfa52dac27a ("printf: convert test_hashed into macro")

Another solution which I would be fine with is:

kernel/trace/Kconfig +0 -0
index e130da35808f..c07e8eadfdd0 100644

kernel/trace/Kconfig config PROFILE_ANNOTATED_BRANCHES

Quoted 10 lines
kernel/trace/Kconfig +1 -1
@@ -692,6 +692,7 @@ config PROFILE_ANNOTATED_BRANCHES
 config PROFILE_ALL_BRANCHES
        bool "Profile all if conditionals" if !FORTIFY_SOURCE
        select TRACE_BRANCH_PROFILING
+       depends on !KUNIT
        help
          This tracer profiles all branch conditions. Every if ()
          taken in the kernel is recorded whether it hit or miss.
 
 
-- Steve

Thanks Steve. IMO that is a very big hammer and not warranted in this case. There's been talk of encouraging distros to enable CONFIG_KUNIT by default [0], which would probably interact poorly with the change you propose.

maintainer
#4 lore raw
Quoted 5 lines · On Mon, 6 Apr 2026 11:21:39 -0400 Tamir Duberstein <[email protected]> wrote:

Thanks Steve. IMO that is a very big hammer and not warranted in this case. There's been talk of encouraging distros to enable CONFIG_KUNIT by default [0], which would probably interact poorly with the change you propose.

Branch profiling is really just a niche that is enabled specifically for seeing all branches taken in the kernel. It hooks to all "if" statements! As you can imagine, it causes a rather large overhead in performance.

This option is only used by developers doing special analysis of their code (namely me ;-).

The only real concern I would have is if the kunit test developers would want to use the branch profiling on their code, in which case my suggestion would prevent that.

-- Steve

maintainer
#5 lore raw
Quoted 13 lines · Steven Rostedt
Quoted 5 lines · On Mon, 6 Apr 2026 11:21:39 -0400 Tamir Duberstein <[email protected]> wrote:

Thanks Steve. IMO that is a very big hammer and not warranted in this case. There's been talk of encouraging distros to enable CONFIG_KUNIT by default [0], which would probably interact poorly with the change you propose.

Branch profiling is really just a niche that is enabled specifically for seeing all branches taken in the kernel. It hooks to all "if" statements! As you can imagine, it causes a rather large overhead in performance.

Quoted 6 lines

This option is only used by developers doing special analysis of their code (namely me ;-).

The only real concern I would have is if the kunit test developers would want to use the branch profiling on their code, in which case my suggestion would prevent that.

I wonder if it might be possible to disable the branch profiling just for the printf_kunit.c as a compromise.

Would "#undef if" in printf_kunit.c help?

Or I see that DISABLE_BRANCH_PROFILING is an official way to disable the feature.

I wonder if the following change would solve the problem. I am sorry, I could not test it easily.

lib/tests/Makefile +1 -0
index 05f74edbc62b..45d69769ccdf 100644
@@ -41,6 +41,7 @@ obj-$(CONFIG_MIN_HEAP_KUNIT_TEST) += min_heap_kunit.o
 CFLAGS_overflow_kunit.o = $(call cc-disable-warning, tautological-constant-out-of-range-compare)
 obj-$(CONFIG_OVERFLOW_KUNIT_TEST) += overflow_kunit.o
 obj-$(CONFIG_PRINTF_KUNIT_TEST) += printf_kunit.o
+CFLAGS_printf_kunit.o += -DDISABLE_BRANCH_PROFILING
 obj-$(CONFIG_RANDSTRUCT_KUNIT_TEST) += randstruct_kunit.o
 obj-$(CONFIG_SCANF_KUNIT_TEST) += scanf_kunit.o
 obj-$(CONFIG_SEQ_BUF_KUNIT_TEST) += seq_buf_kunit.o

Best Regards,
Petr

#6 lore raw
Quoted 25 lines · Petr Mladek
Quoted 13 lines · Steven Rostedt
Quoted 5 lines · On Mon, 6 Apr 2026 11:21:39 -0400 Tamir Duberstein <[email protected]> wrote:

Thanks Steve. IMO that is a very big hammer and not warranted in this case. There's been talk of encouraging distros to enable CONFIG_KUNIT by default [0], which would probably interact poorly with the change you propose.

Branch profiling is really just a niche that is enabled specifically for seeing all branches taken in the kernel. It hooks to all "if" statements! As you can imagine, it causes a rather large overhead in performance.

Quoted 6 lines

This option is only used by developers doing special analysis of their code (namely me ;-).

The only real concern I would have is if the kunit test developers would want to use the branch profiling on their code, in which case my suggestion would prevent that.

I wonder if it might be possible to disable the branch profiling just for the printf_kunit.c as a compromise.

Quoted 7 lines

Would "#undef if" in printf_kunit.c help?

Or I see that DISABLE_BRANCH_PROFILING is an official way to disable the feature.

I wonder if the following change would solve the problem. I am sorry, I could not test it easily.

Yes, we can disable it for the whole file. I decided against that because narrow workarounds are better than broad ones IMO, but it is ultimately up to your preference.

FWIW I did test that this patch fixes the problem in GCC 8.5.0.

maintainer
#7 lore raw
Quoted 31 lines · Tamir Duberstein
Quoted 30 lines · Petr Mladek
Quoted 13 lines · Steven Rostedt
Quoted 5 lines · On Mon, 6 Apr 2026 11:21:39 -0400 Tamir Duberstein <[email protected]> wrote:

Thanks Steve. IMO that is a very big hammer and not warranted in this case. There's been talk of encouraging distros to enable CONFIG_KUNIT by default [0], which would probably interact poorly with the change you propose.

Branch profiling is really just a niche that is enabled specifically for seeing all branches taken in the kernel. It hooks to all "if" statements! As you can imagine, it causes a rather large overhead in performance.

Quoted 6 lines

This option is only used by developers doing special analysis of their code (namely me ;-).

The only real concern I would have is if the kunit test developers would want to use the branch profiling on their code, in which case my suggestion would prevent that.

I wonder if it might be possible to disable the branch profiling just for the printf_kunit.c as a compromise.

Would "#undef if" in printf_kunit.c help?

Or I see that DISABLE_BRANCH_PROFILING is an official way to disable the feature.

Quoted 6 lines
Quoted 2 lines

I wonder if the following change would solve the problem. I am sorry, I could not test it easily.

Yes, we can disable it for the whole file. I decided against that because narrow workarounds are better than broad ones IMO, but it is ultimately up to your preference.

I might be wrong but I think that nobody would want to profile/optimize this kunit test. So, this looks like the best solution because it is straightforward. The variant adding "noinline" looks too hacky to me.

Quoted 1 line

FWIW I did test that this patch fixes the problem in GCC 8.5.0.

Thanks for testing.

Would you like to prepare a proper patch or should I do so?

Best Regards,
Petr

#8 lore raw
Quoted 45 lines · Petr Mladek
Quoted 31 lines · Tamir Duberstein
Quoted 30 lines · Petr Mladek
Quoted 13 lines · Steven Rostedt
Quoted 5 lines · On Mon, 6 Apr 2026 11:21:39 -0400 Tamir Duberstein <[email protected]> wrote:

Thanks Steve. IMO that is a very big hammer and not warranted in this case. There's been talk of encouraging distros to enable CONFIG_KUNIT by default [0], which would probably interact poorly with the change you propose.

Branch profiling is really just a niche that is enabled specifically for seeing all branches taken in the kernel. It hooks to all "if" statements! As you can imagine, it causes a rather large overhead in performance.

Quoted 6 lines

This option is only used by developers doing special analysis of their code (namely me ;-).

The only real concern I would have is if the kunit test developers would want to use the branch profiling on their code, in which case my suggestion would prevent that.

I wonder if it might be possible to disable the branch profiling just for the printf_kunit.c as a compromise.

Would "#undef if" in printf_kunit.c help?

Or I see that DISABLE_BRANCH_PROFILING is an official way to disable the feature.

Quoted 6 lines
Quoted 2 lines

I wonder if the following change would solve the problem. I am sorry, I could not test it easily.

Yes, we can disable it for the whole file. I decided against that because narrow workarounds are better than broad ones IMO, but it is ultimately up to your preference.

I might be wrong but I think that nobody would want to profile/optimize this kunit test. So, this looks like the best solution because it is straightforward. The variant adding "noinline" looks too hacky to me.

Quoted 5 lines
Quoted 1 line

FWIW I did test that this patch fixes the problem in GCC 8.5.0.

Thanks for testing.

Would you like to prepare a proper patch or should I do so?

Please go ahead with your preferred approach.

maintainer
#9 lore raw

PATCH v2 1/1 printf: Compile the kunit test with DISABLE_BRANCH_PROFILING

Old GCC can miscompile printf_kunit's errptr() test when branch profiling is enabled. BUILD_BUG_ON(IS_ERR(PTR)) is a constant false expression, but CONFIG_TRACE_BRANCH_PROFILING and CONFIG_PROFILE_ALL_BRANCHES make the IS_ERR() path side-effectful. GCC's IPA splitter can then outline the cold assert arm into errptr.part.* and leave that clone with an unconditional __compiletime_assert_*() call, causing a false build failure.

This started showing up after test_hashed() became a macro and moved its local buffer into errptr(), which changed GCC's inlining and splitting decisions enough to expose the compiler bug.

Workaround the problem by disabling the branch profiling for printf_kunit.o. It is a straightforward and acceptable solution.

---
Changes against v1:

+ Disable the branch profiling for the whole printf_kunit.o instead of using "noinline".

 lib/tests/Makefile | 1 +
 1 file changed, 1 insertion(+)
lib/tests/Makefile +1 -0
index 05f74edbc62b..fbb2aad26994 100644
@@ -40,6 +40,7 @@ obj-$(CONFIG_MEMCPY_KUNIT_TEST) += memcpy_kunit.o
 obj-$(CONFIG_MIN_HEAP_KUNIT_TEST) += min_heap_kunit.o
 CFLAGS_overflow_kunit.o = $(call cc-disable-warning, tautological-constant-out-of-range-compare)
 obj-$(CONFIG_OVERFLOW_KUNIT_TEST) += overflow_kunit.o
+CFLAGS_printf_kunit.o += -DDISABLE_BRANCH_PROFILING
 obj-$(CONFIG_PRINTF_KUNIT_TEST) += printf_kunit.o
 obj-$(CONFIG_RANDSTRUCT_KUNIT_TEST) += randstruct_kunit.o
 obj-$(CONFIG_SCANF_KUNIT_TEST) += scanf_kunit.o
Signature · 1 line
2.53.0
#10 lore raw

Re: [PATCH v2] printf: Compile the kunit test with DISABLE_BRANCH_PROFILING

Quoted 43 lines · Petr Mladek

Old GCC can miscompile printf_kunit's errptr() test when branch profiling is enabled. BUILD_BUG_ON(IS_ERR(PTR)) is a constant false expression, but CONFIG_TRACE_BRANCH_PROFILING and CONFIG_PROFILE_ALL_BRANCHES make the IS_ERR() path side-effectful. GCC's IPA splitter can then outline the cold assert arm into errptr.part.* and leave that clone with an unconditional __compiletime_assert_*() call, causing a false build failure.

This started showing up after test_hashed() became a macro and moved its local buffer into errptr(), which changed GCC's inlining and splitting decisions enough to expose the compiler bug.

Workaround the problem by disabling the branch profiling for printf_kunit.o. It is a straightforward and acceptable solution.

---
Changes against v1:

+ Disable the branch profiling for the whole printf_kunit.o instead of using "noinline".

 lib/tests/Makefile | 1 +
 1 file changed, 1 insertion(+)
lib/tests/Makefile +1 -1
index 05f74edbc62b..fbb2aad26994 100644
@@ -40,6 +40,7 @@ obj-$(CONFIG_MEMCPY_KUNIT_TEST) += memcpy_kunit.o
 obj-$(CONFIG_MIN_HEAP_KUNIT_TEST) += min_heap_kunit.o
 CFLAGS_overflow_kunit.o = $(call cc-disable-warning, tautological-constant-out-of-range-compare)
 obj-$(CONFIG_OVERFLOW_KUNIT_TEST) += overflow_kunit.o
+CFLAGS_printf_kunit.o += -DDISABLE_BRANCH_PROFILING
 obj-$(CONFIG_PRINTF_KUNIT_TEST) += printf_kunit.o
 obj-$(CONFIG_RANDSTRUCT_KUNIT_TEST) += randstruct_kunit.o
 obj-$(CONFIG_SCANF_KUNIT_TEST) += scanf_kunit.o
--
Quoted 2 lines

2.53.0

It would be good to add a comment explaining when this workaround can be removed.

maintainer
#11 lore raw

PATCH v3 1/1 printf: Compile the kunit test with DISABLE_BRANCH_PROFILING DISABLE_BRANCH_PROFILING

GCC < 12.1 can miscompile printf_kunit's errptr() test when branch profiling is enabled. BUILD_BUG_ON(IS_ERR(PTR)) is a constant false expression, but CONFIG_TRACE_BRANCH_PROFILING and CONFIG_PROFILE_ALL_BRANCHES make the IS_ERR() path side-effectful. GCC's IPA splitter can then outline the cold assert arm into errptr.part.* and leave that clone with an unconditional __compiletime_assert_*() call, causing a false build failure.

This started showing up after test_hashed() became a macro and moved its local buffer into errptr(), which changed GCC's inlining and splitting decisions enough to expose the compiler bug.

Workaround the problem by disabling the branch profiling for printf_kunit.o. It is a straightforward and acceptable solution.

The workaround can be removed once the minimum GCC includes commit 76fe49423047 ("Fix tree-optimization/101941: IPA splitting out function with error attribute"), which first shipped in GCC 12.1.

---
Changes against v2:

+ Added info about gcc version and commit where the miscompilation was fixed. (Tamir)

Changes against v1:

+ Disable the branch profiling for the whole printf_kunit.o instead of using "noinline".

lib/tests/Makefile | 2 ++

 1 file changed, 2 insertions(+)
lib/tests/Makefile +2 -0
index 05f74edbc62b..7e9c2fa52e35 100644
@@ -40,6 +40,8 @@ obj-$(CONFIG_MEMCPY_KUNIT_TEST) += memcpy_kunit.o
 obj-$(CONFIG_MIN_HEAP_KUNIT_TEST) += min_heap_kunit.o
 CFLAGS_overflow_kunit.o = $(call cc-disable-warning, tautological-constant-out-of-range-compare)
 obj-$(CONFIG_OVERFLOW_KUNIT_TEST) += overflow_kunit.o
+# GCC < 12.1 can miscompile errptr() test when branch profiling is enabled.
+CFLAGS_printf_kunit.o += -DDISABLE_BRANCH_PROFILING
 obj-$(CONFIG_PRINTF_KUNIT_TEST) += printf_kunit.o
 obj-$(CONFIG_RANDSTRUCT_KUNIT_TEST) += randstruct_kunit.o
 obj-$(CONFIG_SCANF_KUNIT_TEST) += scanf_kunit.o
Signature · 1 line
2.53.0
author Acked-by on v3 1/1
#12 lore raw

Re: [PATCH v3] printf: Compile the kunit test with DISABLE_BRANCH_PROFILING DISABLE_BRANCH_PROFILING

Quoted 20 lines · Petr Mladek

GCC < 12.1 can miscompile printf_kunit's errptr() test when branch profiling is enabled. BUILD_BUG_ON(IS_ERR(PTR)) is a constant false expression, but CONFIG_TRACE_BRANCH_PROFILING and CONFIG_PROFILE_ALL_BRANCHES make the IS_ERR() path side-effectful. GCC's IPA splitter can then outline the cold assert arm into errptr.part.* and leave that clone with an unconditional __compiletime_assert_*() call, causing a false build failure.

This started showing up after test_hashed() became a macro and moved its local buffer into errptr(), which changed GCC's inlining and splitting decisions enough to expose the compiler bug.

Workaround the problem by disabling the branch profiling for printf_kunit.o. It is a straightforward and acceptable solution.

The workaround can be removed once the minimum GCC includes commit 76fe49423047 ("Fix tree-optimization/101941: IPA splitting out function with error attribute"), which first shipped in GCC 12.1.

Quoted 5 lines
maintainer
#13 lore raw

Re: [PATCH v3] printf: Compile the kunit test with DISABLE_BRANCH_PROFILING DISABLE_BRANCH_PROFILING

Quoted 21 lines · Tamir Duberstein
Quoted 20 lines · Petr Mladek

GCC < 12.1 can miscompile printf_kunit's errptr() test when branch profiling is enabled. BUILD_BUG_ON(IS_ERR(PTR)) is a constant false expression, but CONFIG_TRACE_BRANCH_PROFILING and CONFIG_PROFILE_ALL_BRANCHES make the IS_ERR() path side-effectful. GCC's IPA splitter can then outline the cold assert arm into errptr.part.* and leave that clone with an unconditional __compiletime_assert_*() call, causing a false build failure.

This started showing up after test_hashed() became a macro and moved its local buffer into errptr(), which changed GCC's inlining and splitting decisions enough to expose the compiler bug.

Workaround the problem by disabling the branch profiling for printf_kunit.o. It is a straightforward and acceptable solution.

The workaround can be removed once the minimum GCC includes commit 76fe49423047 ("Fix tree-optimization/101941: IPA splitting out function with error attribute"), which first shipped in GCC 12.1.

Quoted 7 lines
Quoted 5 lines

Thanks a lot for checking.

JFYI, I have committed the patch into printk/linux.git, branch for-7.1-printf-kunit-build.

Best Regards,
Petr

#14 lore raw
In reply to Steven Rostedt
Quoted 9 lines · On Mon, 6 Apr 2026 12:32:32 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 5 lines · On Mon, 6 Apr 2026 11:21:39 -0400 Tamir Duberstein <[email protected]> wrote:

Thanks Steve. IMO that is a very big hammer and not warranted in this case. There's been talk of encouraging distros to enable CONFIG_KUNIT by default [0], which would probably interact poorly with the change you propose.

Quoted 6 lines

Branch profiling is really just a niche that is enabled specifically for seeing all branches taken in the kernel. It hooks to all "if" statements! As you can imagine, it causes a rather large overhead in performance.

This option is only used by developers doing special analysis of their code (namely me ;-).

Is there any way to stop randconfig picking up options like these? It is rather a waste of brain-cycles trying to fix them. If you want the option to test a specific bit of code it is easy to hack/disable any problematic parts.

Even having the KASAN/KMSAN code compiled into allmodconfig is a PITA when you are trying to check that code compiles to something sensible.

David

maintainer
#15 lore raw
Quoted 19 lines · David Laight
Quoted 9 lines · On Mon, 6 Apr 2026 12:32:32 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 5 lines · On Mon, 6 Apr 2026 11:21:39 -0400 Tamir Duberstein <[email protected]> wrote:

Thanks Steve. IMO that is a very big hammer and not warranted in this case. There's been talk of encouraging distros to enable CONFIG_KUNIT by default [0], which would probably interact poorly with the change you propose.

Quoted 6 lines

Branch profiling is really just a niche that is enabled specifically for seeing all branches taken in the kernel. It hooks to all "if" statements! As you can imagine, it causes a rather large overhead in performance.

This option is only used by developers doing special analysis of their code (namely me ;-).

Quoted 7 lines

Is there any way to stop randconfig picking up options like these? It is rather a waste of brain-cycles trying to fix them. If you want the option to test a specific bit of code it is easy to hack/disable any problematic parts.

Even having the KASAN/KMSAN code compiled into allmodconfig is a PITA when you are trying to check that code compiles to something sensible.

This does not look like a good idea. KASAN/KMSAN are very useful features. People will want to keep them working. Removing them from randconfig would just postpone detection of the problem. We would need to deal with it sooner or later anyway.

Best Regards,
Petr

#16 lore raw
Quoted 25 lines · On Wed, 8 Apr 2026 09:24:58 +0200 Petr Mladek <[email protected]> wrote:
Quoted 24 lines · David Laight
Quoted 9 lines · On Mon, 6 Apr 2026 12:32:32 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 5 lines · On Mon, 6 Apr 2026 11:21:39 -0400 Tamir Duberstein <[email protected]> wrote:

Thanks Steve. IMO that is a very big hammer and not warranted in this case. There's been talk of encouraging distros to enable CONFIG_KUNIT by default [0], which would probably interact poorly with the change you propose.

Quoted 6 lines

Branch profiling is really just a niche that is enabled specifically for seeing all branches taken in the kernel. It hooks to all "if" statements! As you can imagine, it causes a rather large overhead in performance.

This option is only used by developers doing special analysis of their code (namely me ;-).

Is there any way to stop randconfig picking up options like these? It is rather a waste of brain-cycles trying to fix them. If you want the option to test a specific bit of code it is easy to hack/disable any problematic parts.

Quoted 7 lines
Quoted 2 lines

Even having the KASAN/KMSAN code compiled into allmodconfig is a PITA when you are trying to check that code compiles to something sensible.

This does not look like a good idea. KASAN/KMSAN are very useful features. People will want to keep them working. Removing them from randconfig would just postpone detection of the problem. We would need to deal with it sooner or later anyway.

True, but when I build an allmodconfig build to check how the asm looks I really don't want them.
For the 'bot' builds you also want to know whether they are defined. Changes to how things are built rather than what is built can throw up unexpected warnings that are very hard to pin down.

It is bad enough finding things that affect one obscure architecture with a specific compiler version when the compiler just makes slightly different decisions, without having unusual compilation/config options is the mix to muddy the waters further.

David

Quoted 3 lines

Best Regards,
Petr

#17 lore raw
Quoted 7 lines · David Laight
Quoted 5 lines · On Wed, 8 Apr 2026 09:24:58 +0200 Petr Mladek <[email protected]> wrote:
Quoted 4 lines · David Laight
Quoted 2 lines · On Mon, 6 Apr 2026 12:32:32 -0400 Steven Rostedt <[email protected]> wrote:

On Mon, 6 Apr 2026 11:21:39 -0400
Tamir Duberstein <[email protected]> wrote:

...

Quoted 10 lines
Quoted 7 lines
Quoted 2 lines

Even having the KASAN/KMSAN code compiled into allmodconfig is a PITA when you are trying to check that code compiles to something sensible.

This does not look like a good idea. KASAN/KMSAN are very useful features. People will want to keep them working. Removing them from randconfig would just postpone detection of the problem. We would need to deal with it sooner or later anyway.

True, but when I build an allmodconfig build to check how the asm looks I really don't want them.

Isn't easy to disable that in the command line to make?

Quoted 8 lines

For the 'bot' builds you also want to know whether they are defined. Changes to how things are built rather than what is built can throw up unexpected warnings that are very hard to pin down.

It is bad enough finding things that affect one obscure architecture with a specific compiler version when the compiler just makes slightly different decisions, without having unusual compilation/config options is the mix to muddy the waters further.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#18 lore raw
Quoted 19 lines · On Wed, 8 Apr 2026 14:29:59 +0300 Andy Shevchenko…
Quoted 7 lines · David Laight
Quoted 5 lines · On Wed, 8 Apr 2026 09:24:58 +0200 Petr Mladek <[email protected]> wrote:
Quoted 4 lines · David Laight
Quoted 2 lines · On Mon, 6 Apr 2026 12:32:32 -0400 Steven Rostedt <[email protected]> wrote:

On Mon, 6 Apr 2026 11:21:39 -0400
Tamir Duberstein <[email protected]> wrote:

...

Quoted 8 lines
Quoted 7 lines
Quoted 2 lines

Even having the KASAN/KMSAN code compiled into allmodconfig is a PITA when you are trying to check that code compiles to something sensible.

This does not look like a good idea. KASAN/KMSAN are very useful features. People will want to keep them working. Removing them from randconfig would just postpone detection of the problem. We would need to deal with it sooner or later anyway.

Quoted 4 lines
Quoted 2 lines

True, but when I build an allmodconfig build to check how the asm looks I really don't want them.

Isn't easy to disable that in the command line to make?

It is a config option, and I think you need to reset it every time you rerun 'make allmodconfig' to pick up config changes.

You can enable -Werror with W=e, but not disable it if you want to set W=1.
I did have a patch that let you use W=1-e which is useful.

David

Quoted 10 lines
Quoted 9 lines

For the 'bot' builds you also want to know whether they are defined. Changes to how things are built rather than what is built can throw up unexpected warnings that are very hard to pin down.

It is bad enough finding things that affect one obscure architecture with a specific compiler version when the compiler just makes slightly different decisions, without having unusual compilation/config options is the mix to muddy the waters further.

#19 lore raw
In reply to Tamir Duberstein
Quoted 13 lines · Tamir Duberstein

Old GCC can miscompile printf_kunit's errptr() test when branch profiling is enabled. BUILD_BUG_ON(IS_ERR(PTR)) is a constant false expression, but CONFIG_TRACE_BRANCH_PROFILING and CONFIG_PROFILE_ALL_BRANCHES make the IS_ERR() path side-effectful. GCC's IPA splitter can then outline the cold assert arm into errptr.part.* and leave that clone with an unconditional __compiletime_assert_*() call, causing a false build failure.

This started showing up after test_hashed() became a macro and moved its local buffer into errptr(), which changed GCC's inlining and splitting decisions enough to expose the compiler bug.

Quoted 8 lines

Mark errptr() noinline to keep it out of that buggy IPA path while preserving the BUILD_BUG_ON(IS_ERR(PTR)) check and the macro-based printf argument checking.

Fixes 9bfa52dac27a ("printf: convert test_hashed into macro")

Adding tag per stable instructions:

#20 lore raw
In reply to Tamir Duberstein
Quoted 8 lines · On Sun, 05 Apr 2026 13:31:50 -0400 Tamir Duberstein <[email protected]> wrote:

Old GCC can miscompile printf_kunit's errptr() test when branch profiling is enabled. BUILD_BUG_ON(IS_ERR(PTR)) is a constant false expression, but CONFIG_TRACE_BRANCH_PROFILING and CONFIG_PROFILE_ALL_BRANCHES make the IS_ERR() path side-effectful. GCC's IPA splitter can then outline the cold assert arm into errptr.part.* and leave that clone with an unconditional __compiletime_assert_*() call, causing a false build failure.

Quoted 7 lines

This started showing up after test_hashed() became a macro and moved its local buffer into errptr(), which changed GCC's inlining and splitting decisions enough to expose the compiler bug.

Mark errptr() noinline to keep it out of that buggy IPA path while preserving the BUILD_BUG_ON(IS_ERR(PTR)) check and the macro-based printf argument checking.

Why not convert that check to a run-time one?
It isn't as though IS_ERR() is usually used with constants.

(There are a lot of other tests which would be better as compile-time ones; since the run-time code just checks the compiler generated the correct constant. So a compile failure saves you having to run the tests; but that isn't true here.)

I'd also test -4095 and -4096...

Quoted 25 lines
Fixes 9bfa52dac27a ("printf: convert test_hashed into macro")
---
 lib/tests/printf_kunit.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
lib/tests/printf_kunit.c +7 -1
index f6f21b445ece..a8087e8ac826 100644
@@ -749,7 +749,23 @@ static void fourcc_pointer(struct kunit *kunittest)
 	fourcc_pointer_test(kunittest, try_cb, ARRAY_SIZE(try_cb), "%p4cb");
 }
 
-static void
+/*
+ * GCC < 12.1 can miscompile this test when branch profiling is enabled.
+ *
+ * BUILD_BUG_ON(IS_ERR(PTR)) is a constant false expression, but old GCC can
+ * still trip over it after CONFIG_TRACE_BRANCH_PROFILING and
+ * CONFIG_PROFILE_ALL_BRANCHES rewrite the IS_ERR() unlikely() path into
+ * side-effectful branch counter updates. IPA splitting then outlines the cold

lib/tests/printf_kunit.c static void fourcc_pointer(struct kunit *kunittest) · line 768

Quoted 10 lines
lib/tests/printf_kunit.c +10 -0
+ * assert arm into errptr.part.* and leaves that clone with an unconditional
+ * __compiletime_assert_*() call, so the build fails even though PTR is not an
+ * ERR_PTR.
+ *
+ * Keep this test out of that buggy IPA path so the BUILD_BUG_ON() can stay in
+ * place without open-coding IS_ERR(). This can be removed once the minimum GCC
+ * includes commit 76fe49423047 ("Fix tree-optimization/101941: IPA splitting
+ * out function with error attribute"), which first shipped in GCC 12.1.
+ */
+static noinline void

While that might make a difference, I'm not sure that it has to. You aren't changing anything directly related to the failing expansion.

David

Quoted 13 lines
lib/tests/printf_kunit.c +0 -1
 errptr(struct kunit *kunittest)
 {
 	test("-1234", "%pe", ERR_PTR(-1234));
 
---

base-commit: d8a9a4b11a137909e306e50346148fc5c3b63f9d
change-id: 20260405-printf-test-old-gcc-f13fecda6524

Best regards,

Signature · 1 line
Tamir Duberstein <[email protected]>