Skip to contentSkip to conversation

rust: bindgen: Preserve sched_statistics alignment

PATCH Superseded Peder Bergan 3 participants 6 messages Nov 19 – Nov 20

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rust: bindgen: Preserve sched_statistics alignment

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`")

---
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(+)
rust/bindgen_parameters +4 -0
index 5bab3cf063360..2686687555af3 100644
@@ -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,

Signature · 1 line
Peder Bergan <[email protected]>
#1 lore raw
Quoted 4 lines · On Thu, Nov 19, 2026 at 8:33 PM Peder Bergan via B4 Relay…

With CONFIG_SCHEDSTATS disabled, struct sched_statistics is empty but still cache-line aligned. Bindgen emits the empty definition without that alignment.

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?

Quoted 3 lines

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

Cc'ing the Binder folks.

Quoted 1 line
Fixes 313c4281bc9d ("rust: add basic `Task`")

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

Cc [email protected] # 6.12.x (Rust is pinned in older LTSs)
Quoted 1 line

Jules Mercier reported the layout mismatch on arm64.

Reported-by Jules Mercier

(I asked Jules about an email in the issue)

rust/bindgen_parameters line 31

Quoted 3 lines
rust/bindgen_parameters +3 -0
+# 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

#3 lore raw
In reply to Miguel Ojeda

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.

#4 lore raw
Quoted 7 lines · On Fri, Nov 20, 2026 at 4:38 PM <[email protected]> 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

Tested-by on v1 1/1
#5 lore raw
In reply to Peder Bergan
Quoted 9 lines · Peder Bergan

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.

Thanks!