Skip to content

rust: task_struct layout mismatch with CONFIG_SCHEDSTATS=n breaks Rust binder (wrong current->mm, mmap EINVAL, mmgrab on a bogus pointer) #1269

Open

Description

@JulesJux

rust: task_struct layout mismatch with CONFIG_SCHEDSTATS=n breaks Rust binder (wrong current->mm, mmap EINVAL, mmgrab on a bogus pointer)

Summary

With CONFIG_SCHEDSTATS=n, the Rust bindings place every task_struct field after stats 48 bytes too early on my arm64 build. Task::mm() (rust/kernel/task.rs) therefore reads the wrong field. In the Rust binder driver:

  • ShrinkablePageRange::new() stores a bogus mm, and calls mmgrab() on it at every open(), which increments a counter in unrelated kernel memory;
  • every mmap() of a binder device then fails with EINVAL in register_with_vma(), because the bogus mm doesn't match vma->vm_mm.

Userspace sees mmap failing with Invalid argument both for Android's own ProcessState (hw-ProcessState: Mmapping /dev/hwbinder failed: Invalid argument) and for libgbinder. Waydroid cannot start.

Root cause

struct sched_statistics (include/linux/sched.h) has no members when CONFIG_SCHEDSTATS=n, but is declared ____cacheline_aligned. bindgen drops the alignment of empty structs: it emits #[repr(C)] struct sched_statistics {} without #[repr(align(64))]. Its layout tests would catch this, but rust/Makefile passes --no-layout-tests, so it compiles silently.

Upstream bindgen report with a stand-alone reproducer: rust-lang/rust-bindgen#3549

Evidence

C layout from the running kernel's BTF (pahole -F btf -C task_struct /sys/kernel/btf/vmlinux), Rust layout from offset_of! on the bindings (temporary debug print in the binder driver):

field C (BTF) Rust bindings
se, rt, dl, dl_server, sched_class 192, 512, 560, 832, 840 192, 512, 560, 832, 840
stats (empty, __aligned__(64)) 960 908
preempt_notifiers 960 912
policy 968 920
cpus_ptr 992 944
sched_info 1136 1088
tasks 1200 1152
pushable_tasks 1216 1168
pushable_dl_tasks 1256 1208
mm 1280 1232
sizeof(task_struct) 4288 4224

struct vm_area_struct matches on both sides (vm_mm at 16, size 192).

Debug output at mmap() time, with raw pointers. range mm is what binder stored at open(), and current mm is what Task::mm() returns now: both come from the same wrong field. The real mm is vma mm:

binder: binder mmap debug: invalid vma->vm_mm: range mm 0xffff0000e654d888, vma mm 0xffff00010f965dc0, current mm 0xffff0000e654d888, vma 0xff49c492e000-0xff49c4a2c000
binder: binder mmap debug: rust offsets: vm_area_struct.vm_mm=16 vm_area_struct size=192 task_struct.mm=1232
binder: binder mmap debug: rust task_struct size=4224 align=64 se=192 rt=512 dl=560 dl_server=832 sched_class=840 stats=908 preempt_notifiers=912 policy=920 cpus_ptr=944 sched_info=1088 tasks=1152 pushable_tasks=1168 pushable_dl_tasks=1208 mm=1232

The "range mm" value is not even 64-byte aligned, although struct mm_struct is.

Workaround

CONFIG_SCHEDSTATS=y makes sched_statistics non-empty, so bindgen emits the alignment. With it, binder mmap() succeeds and Waydroid runs.

Environment

  • Kernel: sdm845-mainline sdm845-next-20261102-1 (7.4.0-rc1, based on next-20261102), arm64, LLVM=1, cross-compiled by postmarketOS (pmbootstrap)
  • rustc 1.99.1 (Alpine 1.99.1-r0), bindgen 0.73.2, clang/libclang 23.1.5
  • Relevant config: CONFIG_SCHEDSTATS=n, CONFIG_RUST=y, CONFIG_ANDROID_BINDER_IPC_RUST=y, CONFIG_RANDSTRUCT_NONE=y, 4K pages
  • Device: OnePlus 6T (fajita)

Scope and suggestions

Any Rust code reading a task_struct field located after stats is affected on CONFIG_SCHEDSTATS=n builds. I have only verified mm and the fields listed above; other accessors in rust/kernel/task.rs may be affected too.

Possible directions, for whoever knows the area better:

  • fix bindgen (empty structs with an explicit alignment), and/or require a fixed version;
  • give sched_statistics a member when CONFIG_SCHEDSTATS=n, or move the alignment to the task_struct field;
  • add compile-time checks that the Rust offsets of fields used through the bindings match C (offsetof), at least for task_struct.

Activity

  1. ojeda commented on Nov 13, 2026

    @ojeda
    Member

    One more option is to use C (e.g. a Rust helper) to access the fields that suffer from the bug.

    In addition, the "give sched_statistics a member when CONFIG_SCHEDSTATS=n" could also be gated under CONFIG_RUST=y.

    And, yeah, we should fix bindgen anyway. It is a good time to get it fixed, because there may be time to get it into the next minimum version bump next year (i.e. the version shipped in the next Debian Stable).

    Regarding the layout tests, yeah, we should enable them under a Kconfig option if possible (or maybe do it unconditionally if the compile-time does not suffer). And if it not possible because we would need to filter something, then we should add that filtering so that at least we do not regress the cases we care about.

    Cc'ing Binder: @Darksonn @thebigcako

  2. added
    • toolchainRelated to `rustc`, `bindgen`, `rustdoc`, LLVM, Clippy...
    • driversRelated to the example drivers in `drivers/`.
    • bugRelated to runtime bugs, panics, warnings...
    on Nov 13, 2026
  3. JulesJux commented on Nov 14, 2026

    @JulesJux
    Author

    Thanks for the reply.

    I can reproduce this reliably on a OnePlus 6T (arm64, CONFIG_SCHEDSTATS=n), so I'm happy to test a C helper for the affected fields or the CONFIG_RUST-gated member in sched_statistics.

    I'd also like to try fixing bindgen itself. To be transparent: I'm not very familiar with its codebase, so I would work on it with the help of an AI assistant, test it against the reproducer from the bindgen issue and on the device, and say so in the PR.

    If it helps with the layout-test question, I can also build this arm64 config with bindgen layout tests enabled and report which types fail.

  4. pederbe commented on Nov 19, 2026

    @pederbe

    I've submitted a patch to the mailing list.

    Mailing-list thread

  5. ojeda commented on Nov 20, 2026

    @ojeda
    Member

    @JulesJux Would you like us to Cc you there and/or add the email to the Reported-by?

  6. JulesJux commented on Nov 20, 2026

    @JulesJux
    Author

    Thanks, yes please Cc me and add my email : jules@les-mercier.fr to the Reported-by.
    I'll test the patch on the OnePlus 6T with CONFIG_SCHEDSTATS=n and reply on the list with the result.

  7. ojeda commented on Nov 20, 2026

    @ojeda
    Member

    Thanks Jules, appreciated!

  8. JulesJux commented on Nov 20, 2026

    @JulesJux
    Author

    Tested the patch on the device with CONFIG_SCHEDSTATS=n: binder works and Waydroid boots. I replied on the list with a Tested-by.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    • bugRelated to runtime bugs, panics, warnings...• driversRelated to the example drivers in `drivers/`.• toolchainRelated to `rustc`, `bindgen`, `rustdoc`, LLVM, Clippy...

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions