Skip to contentSkip to conversation

rust: bindgen: Preserve sched_statistics alignment

PATCH Superseded Peder Bergan 2 participants 2 messages Nov 20

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 rust: bindgen: Preserve sched_statistics alignment

With CONFIG_SCHEDSTATS disabled, struct sched_statistics is empty but still cache-line aligned. Bindgen omits the alignment of the empty type. Bindgen 0.73.2 also stopped forcing padding before over-aligned fields, so fields following stats can be misplaced in the generated task_struct. On 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.

---
Changes in v2

- Explain how the bindgen 0.73.2 padding change exposes the missing alignment.
- Link the bindgen bug and request stable backports starting with 6.12.x.
- Credit Jules Mercier's report and OnePlus 6T test, and expand the Binder Cc list.

I kept the opaque rule unconditional as discussed. On bindgen 0.73.1, I found an offset mismatch at task_struct.se before sched_statistics.

I also checked an arm64 6.12.y build with bindgen 0.73.2 and schedstats disabled. With the rule applied in the 6.12.y context, the Rust task_struct.pid offset moved from 1208 to 1216, matching C.

v1 on lore.kernel.org
https://lore.kernel.org/rust-for-linux/20261119-fix-schedstats-bindgen-layout-v1-1-0599dd230ffd@gmail.com/

OpenAI Codex assisted with the bindgen change, validation and drafting.

---
 rust/bindgen_parameters | 6 ++++++
 1 file changed, 6 insertions(+)
rust/bindgen_parameters +6 -0
index 5bab3cf063360..65c308d4b603b 100644
@@ -26,6 +26,12 @@
 # warning. We don't need to peek into it anyway.
 --opaque-type spinlock
 
+# Bindgen omits the alignment of empty structs. Bindgen 0.73.2 also stopped
+# forcing padding before over-aligned fields, so following fields can be
+# misplaced. Making sched_statistics opaque preserves its C alignment.
+# https://github.com/rust-lang/rust-bindgen/issues/3549
+--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 2 lines · On Fri, Nov 20, 2026 at 6:19 PM Peder Bergan via B4 Relay…

+# misplaced. Making sched_statistics opaque preserves its C alignment.

To clarify, in the comment I suggested, I didn't mention sched_statistics because it could be that we need to add more cases into the list, i.e. I was trying to make the comment generic.

(Of course, hopefully the bug in bindgen will be gone soon)

Cheers,
Miguel