All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] seq_buf: avoid clang section mismatch from seq_buf_terminate optimization
@ 2026-12-01  9:06 Arnd Bergmann
  2026-12-01 15:13 ` Steven Rostedt
  0 siblings, 1 reply; 6+ messages in thread
From: Arnd Bergmann @ 2026-12-01  9:06 UTC (permalink / raw)
  To: Kees Cook, Nathan Chancellor, Steven Rostedt
  Cc: Arnd Bergmann, Nick Desaulniers, Bill Wendling, Justin Stitt,
	Andy Shevchenko, Randy Dunlap, Alexei Starovoitov, linux-kernel,
	llvm

From: Arnd Bergmann <arnd@arndb.de>

I observed clang-21 create a specialized version of seq_buf_terminate()
that hardcodes the 'bootup_event_seq' argument since all callers pass
the same one. Since seq_buf_terminate is not marked __init, it does
not get inlined into the caller if that is __init, but the __initdata
argument does get referenced from the specialized version, causing a
section mismatch warning:

WARNING: modpost: vmlinux: section mismatch in reference: __seq_buf_terminate+0x13 (section: .text) -> bootup_event_seq (section: .init.data)
WARNING: modpost: vmlinux: section mismatch in reference: seq_buf_terminate+0xda (section: .text) -> bootup_event_seq (section: .init.data)
ERROR: modpost: Section mismatches detected.
Set CONFIG_SECTION_MISMATCH_WARN_ONLY=y to allow them.

I'm unsure whether this should be considered a bug in clang or the
kernel, or what the best workaround is. The two options I considered
are:

 - mark bootup_event_seq as non-__initdata to avoid this one instance,
   slightly pessimizing this the runtime memory usage and possibly having
   the same situation elsewhere

 - mark all three functions in the call chain as __always_inline, forcing
   an inlining decision that the compiler would perhaps not make, but
   preventing other instances of this issue as well.

This is the second option.

Fixes: 7b8cd26b6aab ("seq_buf: Mark a seq_buf overflowed when terminating it loses a byte")
Link: https://lore.kernel.org/all/20261118171346.FH6VoALE-lkp@intel.com/
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
---
 include/linux/seq_buf.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/include/linux/seq_buf.h b/include/linux/seq_buf.h
index 6b915c67d235..43bc067c82cf 100644
--- a/include/linux/seq_buf.h
+++ b/include/linux/seq_buf.h
@@ -74,7 +74,7 @@ seq_buf_has_overflowed(struct seq_buf *s)
  * seq_buf_str() terminates it, so clear whatever was not written: a writer
  * sets len to how much it filled, and anything past that was never adopted.
  */
-static inline void
+static __always_inline void
 seq_buf_set_overflow(struct seq_buf *s)
 {
 	if (s->len < s->size)
@@ -134,7 +134,7 @@ static inline unsigned int seq_buf_used(struct seq_buf *s)
  *
  * Returns: the offset of the NUL.
  */
-static inline size_t __seq_buf_terminate(struct seq_buf *s)
+static __always_inline size_t __seq_buf_terminate(struct seq_buf *s)
 {
 	size_t end;
 
@@ -240,7 +240,7 @@ static inline size_t seq_buf_strlen(struct seq_buf *s)
  * After this function is called, s->buffer is safe to use
  * in string operations.
  */
-static inline void seq_buf_terminate(struct seq_buf *s)
+static __always_inline void seq_buf_terminate(struct seq_buf *s)
 {
 	if (s->size == 0)
 		return;
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] seq_buf: avoid clang section mismatch from seq_buf_terminate optimization
  2026-12-01  9:06 [PATCH] seq_buf: avoid clang section mismatch from seq_buf_terminate optimization Arnd Bergmann
@ 2026-12-01 15:13 ` Steven Rostedt
  2026-12-01 21:53   ` Arnd Bergmann
  0 siblings, 1 reply; 6+ messages in thread
From: Steven Rostedt @ 2026-12-01 15:13 UTC (permalink / raw)
  To: Arnd Bergmann
  Cc: Kees Cook, Nathan Chancellor, Arnd Bergmann, Nick Desaulniers,
	Bill Wendling, Justin Stitt, Andy Shevchenko, Randy Dunlap,
	Alexei Starovoitov, linux-kernel, llvm

On Tue,  1 Dec 2026 10:06:16 +0100
Arnd Bergmann <arnd@kernel.org> wrote:

> From: Arnd Bergmann <arnd@arndb.de>
> 
> I observed clang-21 create a specialized version of seq_buf_terminate()

The above line denotes what kind of bug this is. Clang is creating a
"specialized version"!

> that hardcodes the 'bootup_event_seq' argument since all callers pass
> the same one. Since seq_buf_terminate is not marked __init, it does
> not get inlined into the caller if that is __init, but the __initdata
> argument does get referenced from the specialized version, causing a
> section mismatch warning:
> 
> WARNING: modpost: vmlinux: section mismatch in reference: __seq_buf_terminate+0x13 (section: .text) -> bootup_event_seq (section: .init.data)
> WARNING: modpost: vmlinux: section mismatch in reference: seq_buf_terminate+0xda (section: .text) -> bootup_event_seq (section: .init.data)
> ERROR: modpost: Section mismatches detected.
> Set CONFIG_SECTION_MISMATCH_WARN_ONLY=y to allow them.
> 
> I'm unsure whether this should be considered a bug in clang or the
> kernel, or what the best workaround is. The two options I considered
> are:

It's definitely a bug in clang. If it created a specialized version of a
function and that version is now causing section mismatches, that's a bug
in the compiler. The kernel did nothing wrong.

> 
>  - mark bootup_event_seq as non-__initdata to avoid this one instance,
>    slightly pessimizing this the runtime memory usage and possibly having
>    the same situation elsewhere
> 
>  - mark all three functions in the call chain as __always_inline, forcing
>    an inlining decision that the compiler would perhaps not make, but
>    preventing other instances of this issue as well.
> 
> This is the second option.
> 
> Fixes: 7b8cd26b6aab ("seq_buf: Mark a seq_buf overflowed when terminating it loses a byte")

This doesn't fix that commit. It is a workaround to a clang bug.

I really don't think this should be fixed in the kernel.

-- Steve


> Link: https://lore.kernel.org/all/20261118171346.FH6VoALE-lkp@intel.com/
> Signed-off-by: Arnd Bergmann <arnd@arndb.de>
> ---
>  include/linux/seq_buf.h | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/include/linux/seq_buf.h b/include/linux/seq_buf.h
> index 6b915c67d235..43bc067c82cf 100644
> --- a/include/linux/seq_buf.h
> +++ b/include/linux/seq_buf.h
> @@ -74,7 +74,7 @@ seq_buf_has_overflowed(struct seq_buf *s)
>   * seq_buf_str() terminates it, so clear whatever was not written: a writer
>   * sets len to how much it filled, and anything past that was never adopted.
>   */
> -static inline void
> +static __always_inline void
>  seq_buf_set_overflow(struct seq_buf *s)
>  {
>  	if (s->len < s->size)
> @@ -134,7 +134,7 @@ static inline unsigned int seq_buf_used(struct seq_buf *s)
>   *
>   * Returns: the offset of the NUL.
>   */
> -static inline size_t __seq_buf_terminate(struct seq_buf *s)
> +static __always_inline size_t __seq_buf_terminate(struct seq_buf *s)
>  {
>  	size_t end;
>  
> @@ -240,7 +240,7 @@ static inline size_t seq_buf_strlen(struct seq_buf *s)
>   * After this function is called, s->buffer is safe to use
>   * in string operations.
>   */
> -static inline void seq_buf_terminate(struct seq_buf *s)
> +static __always_inline void seq_buf_terminate(struct seq_buf *s)
>  {
>  	if (s->size == 0)
>  		return;


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] seq_buf: avoid clang section mismatch from seq_buf_terminate optimization
  2026-12-01 15:13 ` Steven Rostedt
@ 2026-12-01 21:53   ` Arnd Bergmann
  2026-12-01 22:05     ` Nick Desaulniers
  0 siblings, 1 reply; 6+ messages in thread
From: Arnd Bergmann @ 2026-12-01 21:53 UTC (permalink / raw)
  To: Steven Rostedt, Arnd Bergmann
  Cc: Kees Cook, Nathan Chancellor, Nick Desaulniers, Bill Wendling,
	Justin Stitt, Andy Shevchenko, Randy Dunlap, Alexei Starovoitov,
	linux-kernel, llvm

On Tue, Dec 1, 2026, at 16:13, Steven Rostedt wrote:
> On Tue,  1 Dec 2026 10:06:16 +0100 Arnd Bergmann <arnd@kernel.org> wrote:
>>
>> I'm unsure whether this should be considered a bug in clang or the
>> kernel, or what the best workaround is. The two options I considered
>> are:
>
> It's definitely a bug in clang. If it created a specialized version of a
> function and that version is now causing section mismatches, that's a bug
> in the compiler. The kernel did nothing wrong.

Not sure I understand, do you mean clang should skip the FunctionSpecialization
in interprocedural constant propagation altogether when building the kernel,
or do you think we should teach clang about the kernel's concept of what
functions are allowed to reference .initdata?

Is the clang behavior fundamentally different from gcc here? It seems to
me that gcc just didn't hit this issue because unlike clang it does
inline across sections.

      Arnd

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] seq_buf: avoid clang section mismatch from seq_buf_terminate optimization
  2026-12-01 21:53   ` Arnd Bergmann
@ 2026-12-01 22:05     ` Nick Desaulniers
  2026-12-01 22:17       ` Steven Rostedt
  0 siblings, 1 reply; 6+ messages in thread
From: Nick Desaulniers @ 2026-12-01 22:05 UTC (permalink / raw)
  To: Arnd Bergmann
  Cc: Steven Rostedt, Arnd Bergmann, Kees Cook, Nathan Chancellor,
	Bill Wendling, Justin Stitt, Andy Shevchenko, Randy Dunlap,
	Alexei Starovoitov, linux-kernel, llvm

On Tue, Dec 1, 2026 at 1:54 PM Arnd Bergmann <arnd@arndb.de> wrote:
>
> On Tue, Dec 1, 2026, at 16:13, Steven Rostedt wrote:
> > On Tue,  1 Dec 2026 10:06:16 +0100 Arnd Bergmann <arnd@kernel.org> wrote:
> >>
> >> I'm unsure whether this should be considered a bug in clang or the
> >> kernel, or what the best workaround is. The two options I considered
> >> are:
> >
> > It's definitely a bug in clang. If it created a specialized version of a

I saw this thread earlier and rolled my eyes. This wasn't a compiler
bug back during the countless other modpost warnings we've fixed in
kernel sources.  It's still not.

commit 2780860eddec ("include/linux/list.h: mark list_add and
__list_add as __always_inline")

commit f9d2ee3f51d6 ("riscv: Always inline bitops")

commit d5fd042bf4cf ("x86/resctrl: Annotate get_mem_config() functions
as __init")

commit 1ec2362084a0 ("ARM: imx: Annotate imx7d_enet_init() as __init")

> > function and that version is now causing section mismatches, that's a bug
> > in the compiler. The kernel did nothing wrong.
>
> Not sure I understand, do you mean clang should skip the FunctionSpecialization
> in interprocedural constant propagation altogether when building the kernel,
> or do you think we should teach clang about the kernel's concept of what
> functions are allowed to reference .initdata?
>
> Is the clang behavior fundamentally different from gcc here? It seems to
> me that gcc just didn't hit this issue because unlike clang it does
> inline across sections.

Asserting "clang does not inline across sections" is a _false_
assertion: https://godbolt.org/z/7TTnxo9Wb (i.e. observe baz jmp'ing
to bar because foo was inlined, despite being in .woof rather than
.text).
-- 
Thanks,
~Nick Desaulniers

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] seq_buf: avoid clang section mismatch from seq_buf_terminate optimization
  2026-12-01 22:05     ` Nick Desaulniers
@ 2026-12-01 22:17       ` Steven Rostedt
  2026-12-02 22:18         ` Nathan Chancellor
  0 siblings, 1 reply; 6+ messages in thread
From: Steven Rostedt @ 2026-12-01 22:17 UTC (permalink / raw)
  To: Nick Desaulniers
  Cc: Arnd Bergmann, Arnd Bergmann, Kees Cook, Nathan Chancellor,
	Bill Wendling, Justin Stitt, Andy Shevchenko, Randy Dunlap,
	Alexei Starovoitov, linux-kernel, llvm

On Tue, 1 Dec 2026 14:05:00 -0800
Nick Desaulniers <ndesaulniers@google.com> wrote:

> On Tue, Dec 1, 2026 at 1:54 PM Arnd Bergmann <arnd@arndb.de> wrote:
> >
> > On Tue, Dec 1, 2026, at 16:13, Steven Rostedt wrote:  
> > > On Tue,  1 Dec 2026 10:06:16 +0100 Arnd Bergmann <arnd@kernel.org> wrote:  
> > >>
> > >> I'm unsure whether this should be considered a bug in clang or the
> > >> kernel, or what the best workaround is. The two options I considered
> > >> are:  
> > >
> > > It's definitely a bug in clang. If it created a specialized version of a  
> 
> I saw this thread earlier and rolled my eyes. This wasn't a compiler
> bug back during the countless other modpost warnings we've fixed in
> kernel sources.  It's still not.
> 
> commit 2780860eddec ("include/linux/list.h: mark list_add and
> __list_add as __always_inline")
> 
> commit f9d2ee3f51d6 ("riscv: Always inline bitops")
> 
> commit d5fd042bf4cf ("x86/resctrl: Annotate get_mem_config() functions
> as __init")
> 
> commit 1ec2362084a0 ("ARM: imx: Annotate imx7d_enet_init() as __init")

My question is, if this wasn't an inline to begin with and was just a
static function in that file, would it still warn?

What exactly did the kernel do wrong?

If we had:

static void seq_buf_set_overflow(struct seq_buf *s)
{
	if (s->len < s->size)
		memset(s->buffer + s->len, 0, s->size - s->len);

	s->len = s->size + 1;
}

static struct seq_buf bootup_event_seq __initdata = {
	.buffer = bootup_event_buf,
	.size = sizeof(bootup_event_buf),
};

static __init int event_trace_enable(void)
{
[..]
	seq_buf_terminate(&bootup_event_seq);
[..]
}

Is that a legitimate section mismatch?

I'm OK if we take this patch to convert to always_inline, but I do not see
it as a kernel fix but simply a workaround to the section matching issue,
as it appears to be as side effect of optimizations and nothing more.

-- Steve

> 
> > > function and that version is now causing section mismatches, that's a bug
> > > in the compiler. The kernel did nothing wrong.  
> >
> > Not sure I understand, do you mean clang should skip the FunctionSpecialization
> > in interprocedural constant propagation altogether when building the kernel,
> > or do you think we should teach clang about the kernel's concept of what
> > functions are allowed to reference .initdata?
> >
> > Is the clang behavior fundamentally different from gcc here? It seems to
> > me that gcc just didn't hit this issue because unlike clang it does
> > inline across sections.  
> 
> Asserting "clang does not inline across sections" is a _false_
> assertion: https://godbolt.org/z/7TTnxo9Wb (i.e. observe baz jmp'ing
> to bar because foo was inlined, despite being in .woof rather than
> .text).


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] seq_buf: avoid clang section mismatch from seq_buf_terminate optimization
  2026-12-01 22:17       ` Steven Rostedt
@ 2026-12-02 22:18         ` Nathan Chancellor
  0 siblings, 0 replies; 6+ messages in thread
From: Nathan Chancellor @ 2026-12-02 22:18 UTC (permalink / raw)
  To: Steven Rostedt
  Cc: Nick Desaulniers, Arnd Bergmann, Arnd Bergmann, Kees Cook,
	Bill Wendling, Justin Stitt, Andy Shevchenko, Randy Dunlap,
	Alexei Starovoitov, linux-kernel, llvm

On Tue, Dec 01, 2026 at 05:17:53PM -0500, Steven Rostedt wrote:
> My question is, if this wasn't an inline to begin with and was just a
> static function in that file, would it still warn?

Yes, I believe so, at least with the example that you provide.

> What exactly did the kernel do wrong?

I would not say the kernel did anything wrong here but I would not say
the compiler did either. I think this is merely a case where the kernel
has assigned some additional meaning to these sections (".init.* is only
available once then discarded") that the compiler is unaware of (so it
cannot alter its behavior because this transformation is normally safe)
and the kernel's post processing does/can not do CFG analysis to see
that bootup_event_seq is only referred to in an init function.

> If we had:
> 
> static void seq_buf_set_overflow(struct seq_buf *s)

I assume this should be seq_buf_terminate()?

> {
> 	if (s->len < s->size)
> 		memset(s->buffer + s->len, 0, s->size - s->len);
> 
> 	s->len = s->size + 1;
> }
> 
> static struct seq_buf bootup_event_seq __initdata = {
> 	.buffer = bootup_event_buf,
> 	.size = sizeof(bootup_event_buf),
> };
> 
> static __init int event_trace_enable(void)
> {
> [..]
> 	seq_buf_terminate(&bootup_event_seq);
> [..]
> }
> 
> Is that a legitimate section mismatch?

No, definitely not.

> I'm OK if we take this patch to convert to always_inline, but I do not see
> it as a kernel fix but simply a workaround to the section matching issue,
> as it appears to be as side effect of optimizations and nothing more.

Sure, I don't see this any different from inlining certain functions for
noinstr and such. Sometimes you need to be explicit for the compiler to
behave as expected and it is not really anyone's fault.

-- 
Cheers,
Nathan

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-12-02 22:18 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01  9:06 [PATCH] seq_buf: avoid clang section mismatch from seq_buf_terminate optimization Arnd Bergmann
2026-12-01 15:13 ` Steven Rostedt
2026-12-01 21:53   ` Arnd Bergmann
2026-12-01 22:05     ` Nick Desaulniers
2026-12-01 22:17       ` Steven Rostedt
2026-12-02 22:18         ` Nathan Chancellor

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.