mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] rust: bindgen: Preserve sched_statistics alignment
@ 2026-11-19 19:33 Peder Bergan via B4 Relay
  2026-11-20 12:31 ` Miguel Ojeda
  2026-11-20 12:57 ` Jules Mercier
  0 siblings, 2 replies; 6+ messages in thread
From: Peder Bergan via B4 Relay @ 2026-11-19 19:33 UTC (permalink / raw)
  To: Miguel Ojeda
  Cc: Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
	Andreas Hindborg, Alice Ryhl, Trevor Gross, Danilo Krummrich,
	Daniel Almeida, Tamir Duberstein, Alexandre Courbot,
	Onur Özkan, Carlos Llamas, rust-for-linux, linux-kernel,
	stable

From: Peder Bergan <peder.bergan@gmail.com>

With CONFIG_SCHEDSTATS disabled, struct sched_statistics is empty but
still cache-line aligned. Bindgen emits the empty definition without
that alignment. On affected builds this misplaces fields following
stats in the generated task_struct. In the reported arm64 build, Rust
Binder reads the wrong pointer via CurrentTask::mm() and rejects mmap()
with -EINVAL.

Make sched_statistics opaque so bindgen uses the C size and alignment.
Rust has no callers that access the fields of this type.

Fixes: 313c4281bc9d ("rust: add basic `Task`")
Closes: https://github.com/Rust-for-Linux/linux/issues/1269
Cc: stable@vger.kernel.org
Signed-off-by: Peder Bergan <peder.bergan@gmail.com>
---
Jules Mercier reported the layout mismatch on arm64.

On arm64 with bindgen 0.73.2 and CONFIG_SCHEDSTATS=n, the generated Rust
binding placed task_struct.mm at offset 1168 while the C layout placed
it at 1208. After the change both report 1208. The layouts also agree
with CONFIG_SCHEDSTATS=y, and the Rust kernel crate and Binder driver
build with Clippy in both configurations.

OpenAI Codex assisted with the bindgen change, validation and drafting.
---
 rust/bindgen_parameters | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/rust/bindgen_parameters b/rust/bindgen_parameters
index 5bab3cf063360..2686687555af3 100644
--- a/rust/bindgen_parameters
+++ b/rust/bindgen_parameters
@@ -26,6 +26,10 @@
 # warning. We don't need to peek into it anyway.
 --opaque-type spinlock
 
+# Bindgen drops the alignment of empty structs. Keep the cache-line alignment
+# of sched_statistics when CONFIG_SCHEDSTATS is disabled.
+--opaque-type sched_statistics
+
 # enums that appear in indirect function calls should specify a cfi type
 --newtype-enum fwctl_rpc_scope
 --with-attribute-custom-enum=fwctl_rpc_scope='#[cfi_encoding="15fwctl_rpc_scope"]'

---
base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3
change-id: 20261119-fix-schedstats-bindgen-layout-0935b6434707

Best regards,
--  
Peder Bergan <peder.bergan@gmail.com>



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

* Re: [PATCH] rust: bindgen: Preserve sched_statistics alignment
  2026-11-19 19:33 [PATCH] rust: bindgen: Preserve sched_statistics alignment Peder Bergan via B4 Relay
@ 2026-11-20 12:31 ` Miguel Ojeda
  2026-11-20 12:33   ` Miguel Ojeda
  2026-11-20 15:38   ` peder.bergan
  2026-11-20 12:57 ` Jules Mercier
  1 sibling, 2 replies; 6+ messages in thread
From: Miguel Ojeda @ 2026-11-20 12:31 UTC (permalink / raw)
  To: peder.bergan
  Cc: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
	Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
	Danilo Krummrich, Daniel Almeida, Tamir Duberstein,
	Alexandre Courbot, Onur Özkan, Carlos Llamas,
	rust-for-linux, linux-kernel, stable, Greg Kroah-Hartman,
	Arve Hjønnevåg, Todd Kjos, Christian Brauner

On Thu, Nov 19, 2026 at 8:33 PM Peder Bergan via B4 Relay
<devnull+peder.bergan.gmail.com@kernel.org> wrote:
>
> With CONFIG_SCHEDSTATS disabled, struct sched_statistics is empty but
> still cache-line aligned. Bindgen emits the empty definition without
> that alignment.

Link: https://github.com/rust-lang/rust-bindgen/issues/3549

Most importantly, this is about a bug that happens to be exposed in
bindgen 0.73.2 only, as far as I can see -- could you please confirm?

> In the reported arm64 build, Rust
> Binder reads the wrong pointer via CurrentTask::mm() and rejects mmap()
> with -EINVAL.

Cc'ing the Binder folks.

> Fixes: 313c4281bc9d ("rust: add basic `Task`")

Given this is a `bindgen` bug, this tag should instead be:

Cc: stable@vger.kernel.org # 6.12.x (Rust is pinned in older LTSs)

> Jules Mercier reported the layout mismatch on arm64.

Reported-by: Jules Mercier

(I asked Jules about an email in the issue)

> +# Bindgen drops the alignment of empty structs. Keep the cache-line alignment
> +# of sched_statistics when CONFIG_SCHEDSTATS is disabled.
> +--opaque-type sched_statistics

This seems OK as a temporary workaround, but I would probably write
something like:

    # Bindgen drops the alignment of empty structs and bindgen 0.73.2
does not add
    # explicit padding before over-aligned fields either, thus the
fields that follow
    # get misplaced: https://github.com/rust-lang/rust-bindgen/issues/3549
    # By making these structs opaque, we recover their alignment.

(In addition, it would still be nice to have a debug Kconfig symbol to
enable the layout tests as discussed in the issue, but that is an
independent patch, of course -- I am thinking of gating it under a new
`BINDGEN_VERSION`, plus particular architectures where we can confirm
it is clean, and maybe `!COMPILE_TEST` in the beginning too...).

Thanks!

Cheers,
Miguel

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

* Re: [PATCH] rust: bindgen: Preserve sched_statistics alignment
  2026-11-20 12:31 ` Miguel Ojeda
@ 2026-11-20 12:33   ` Miguel Ojeda
  2026-11-20 15:38   ` peder.bergan
  1 sibling, 0 replies; 6+ messages in thread
From: Miguel Ojeda @ 2026-11-20 12:33 UTC (permalink / raw)
  To: peder.bergan, Jules Mercier
  Cc: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
	Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
	Danilo Krummrich, Daniel Almeida, Tamir Duberstein,
	Alexandre Courbot, Onur Özkan, Carlos Llamas,
	rust-for-linux, linux-kernel, stable, Greg Kroah-Hartman,
	Arve Hjønnevåg, Todd Kjos, Christian Brauner

On Fri, Nov 20, 2026 at 1:31 PM Miguel Ojeda
<miguel.ojeda.sandonis@gmail.com> wrote:
>
> Reported-by: Jules Mercier

Reported-by: Jules Mercier <jules@les-mercier.fr>

Cheers,
Miguel

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

* Re: [PATCH] rust: bindgen: Preserve sched_statistics alignment
  2026-11-19 19:33 [PATCH] rust: bindgen: Preserve sched_statistics alignment Peder Bergan via B4 Relay
  2026-11-20 12:31 ` Miguel Ojeda
@ 2026-11-20 12:57 ` Jules Mercier
  1 sibling, 0 replies; 6+ messages in thread
From: Jules Mercier @ 2026-11-20 12:57 UTC (permalink / raw)
  To: Peder Bergan
  Cc: Miguel Ojeda, rust-for-linux, linux-kernel, Alice Ryhl, Carlos Llamas

On Thu, Nov 19, 2026 at 08:33:07PM +0100, Peder Bergan wrote:
> With CONFIG_SCHEDSTATS disabled, struct sched_statistics is empty but
> still cache-line aligned. Bindgen emits the empty definition without
> that alignment. On affected builds this misplaces fields following
> stats in the generated task_struct. In the reported arm64 build, Rust
> Binder reads the wrong pointer via CurrentTask::mm() and rejects mmap()
> with -EINVAL.
>
> Make sched_statistics opaque so bindgen uses the C size and alignment.
> Rust has no callers that access the fields of this type.

Tested on a OnePlus 6T (sdm845, arm64), kernel based on next-20261102,
LLVM=1, bindgen 0.73.2, with CONFIG_SCHEDSTATS=n.

Without this patch, mmap() of the binder devices fails with -EINVAL as
described in the issue, and Waydroid cannot start. With it, binder
works and Waydroid boots.

Tested-by: Jules Mercier <jules@les-mercier.fr>

Thanks!

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

* Re: [PATCH] rust: bindgen: Preserve sched_statistics alignment
  2026-11-20 12:31 ` Miguel Ojeda
  2026-11-20 12:33   ` Miguel Ojeda
@ 2026-11-20 15:38   ` peder.bergan
  2026-11-20 16:34     ` Miguel Ojeda
  1 sibling, 1 reply; 6+ messages in thread
From: peder.bergan @ 2026-11-20 15:38 UTC (permalink / raw)
  To: Miguel Ojeda
  Cc: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
	Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
	Danilo Krummrich, Daniel Almeida, Tamir Duberstein,
	Alexandre Courbot, Onur Özkan, Carlos Llamas,
	rust-for-linux, linux-kernel, stable, Greg Kroah-Hartman,
	Arve Hjønnevåg, Todd Kjos, Christian Brauner,
	Jules Mercier

Thanks, Miguel.

I looked into bindgen 0.73.1. The layout diverges earlier at
task_struct.se, and my v1 leaves that offset unchanged.

I ran a reduced test and found that making the type opaque can worsen
field placement on 0.73.0 and 0.73.1. Would you prefer limiting the
workaround to 0.73.2 with scheduler statistics disabled?

I'll add the bindgen issue link and update the comment and stable tag.

Thanks for testing, Jules. I'll add your Reported-by and copy you on v2.

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

* Re: [PATCH] rust: bindgen: Preserve sched_statistics alignment
  2026-11-20 15:38   ` peder.bergan
@ 2026-11-20 16:34     ` Miguel Ojeda
  0 siblings, 0 replies; 6+ messages in thread
From: Miguel Ojeda @ 2026-11-20 16:34 UTC (permalink / raw)
  To: peder.bergan
  Cc: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
	Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
	Danilo Krummrich, Daniel Almeida, Tamir Duberstein,
	Alexandre Courbot, Onur Özkan, Carlos Llamas,
	rust-for-linux, linux-kernel, stable, Greg Kroah-Hartman,
	Arve Hjønnevåg, Todd Kjos, Christian Brauner,
	Jules Mercier

On Fri, Nov 20, 2026 at 4:38 PM <peder.bergan@gmail.com> wrote:
>
> I looked into bindgen 0.73.1. The layout diverges earlier at
> task_struct.se, and my v1 leaves that offset unchanged.
>
> I ran a reduced test and found that making the type opaque can worsen
> field placement on 0.73.0 and 0.73.1. Would you prefer limiting the
> workaround to 0.73.2 with scheduler statistics disabled?

I don't think we need to do that: `bindgen` 0.73.0 and 0.73.1 had
other layout bugs and, anyway, it doesn't really matter fields are off
by 1 byte or many, i.e. they are completely wrong either way... :)

Cheers,
Miguel

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

end of thread, other threads:[~2026-11-20 16:34 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-19 19:33 [PATCH] rust: bindgen: Preserve sched_statistics alignment Peder Bergan via B4 Relay
2026-11-20 12:31 ` Miguel Ojeda
2026-11-20 12:33   ` Miguel Ojeda
2026-11-20 15:38   ` peder.bergan
2026-11-20 16:34     ` Miguel Ojeda
2026-11-20 12:57 ` Jules Mercier

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®