Rust netlink support + use in Rust Binder
Subsystems (4)
Alice Ryhl
maintainerv5 cover Rust netlink support + use in Rust Binder
The C Binder driver exposes messages over netlink when transactions fail, so that a userpace daemon can respond to processes with many failing transactions.
This patch series adds netlink support from Rust, then implements an equivalent API in Rust Binder.
As Binder only uses broadcast messages, I did not add support for other kinds of messages.
---
Changes in v5:
- Remove patch 2.
- Squash patches 3 and 4.
- Link to v4: https://lore.kernel.org/r/[email protected]
Changes in v4:
- Fix typo in 'report netlink transactions' commit message.
- Document that some methods are for const context only.
- Rebase and pick up tags.
- Link to v3: https://lore.kernel.org/r/[email protected]
Changes in v3:
- Fix spurious 'return' statements in Rust helpers (Patch 1).
- Sashiko review:
- Fix ynl_gen to handle empty multicast groups and correct multicast indexing (Patch 2).
- Fix transaction failed reply logic to report via Netlink inside reply_inner() (Patch 4).
- Link to v2: https://lore.kernel.org/r/[email protected]
Changes in v2:
- Make inclusion of to_pid conditional too.
- Add note about file name in second patch.
- Make it clear that the sk_buff wrapper is netlink-specific.
- Better handle bitfield in patch 1.
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Alice Ryhl (1):
rust: netlink: add raw netlink abstraction
Carlos Llamas (1):
rust_binder: report netlink transactions
drivers/android/Kconfig | 2 +- drivers/android/binder/netlink.rs | 110 ++++++++++ drivers/android/binder/rust_binder_main.rs | 8 +- drivers/android/binder/thread.rs | 10 + drivers/android/binder/transaction.rs | 40 ++++ rust/bindings/bindings_helper.h | 3 + rust/helpers/genetlink.c | 46 ++++ rust/helpers/helpers.c | 1 + rust/kernel/lib.rs | 1 + rust/kernel/netlink.rs | 336 +++++++++++++++++++++++++++++ rust/uapi/uapi_helper.h | 1 + 11 files changed, 555 insertions(+), 3 deletions(-)
---
base-commit: 5ad28496055858166eb2268344c8fda2c26d3561
change-id: 20260306-binder-netlink-c82110b2fb74
Best regards,
Signature · 1 line
Alice Ryhl <[email protected]>
Alice Ryhl
maintainerPATCH v5 1/2 rust: netlink: add raw netlink abstraction
This implements a safe and relatively simple API over the netlink API, that allows you to add different attributes to a netlink message and broadcast it. As the first user of this API only makes use of broadcast, only broadcast messages are supported here.
This API is intended to be safe and to be easy to use in *generated* code. This is because netlink is generally used with yaml files that describe the underlying API, and the python generator outputs C code (or, soon, Rust code) that lets you use the API more easily. So for example, if there is a string field, the code generator will output a method that internall calls put_string() with the right attr type.
--- rust/bindings/bindings_helper.h | 3 + rust/helpers/genetlink.c | 46 ++++++ rust/helpers/helpers.c | 1 + rust/kernel/lib.rs | 1 + rust/kernel/netlink.rs | 336 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 387 insertions(+)
rust/bindings/bindings_helper.h +3 -0
@@ -92,6 +92,8 @@#include <linux/wait.h> #include <linux/workqueue.h> #include <linux/xarray.h> +#include <net/genetlink.h> +#include <net/netlink.h> #include <trace/events/rust_sample.h> /*
@@ -109,6 +111,7 @@const size_t RUST_CONST_HELPER_ARCH_SLAB_MINALIGN =ARCH_SLAB_MINALIGN ;const size_t RUST_CONST_HELPER_ARCH_KMALLOC_MINALIGN =ARCH_KMALLOC_MINALIGN ;const size_t RUST_CONST_HELPER_PAGE_SIZE =PAGE_SIZE ; +const size_t RUST_CONST_HELPER_GENLMSG_DEFAULT_SIZE =GENLMSG_DEFAULT_SIZE ;const gfp_t RUST_CONST_HELPER_GFP_ATOMIC =GFP_ATOMIC ;const gfp_t RUST_CONST_HELPER_GFP_KERNEL =GFP_KERNEL ;const gfp_t RUST_CONST_HELPER_GFP_KERNEL_ACCOUNT =GFP_KERNEL_ACCOUNT ;
rust/helpers/genetlink.c new file +46 -0
46 lines not on this page · raw message
rust/helpers/helpers.c +1 -0
@@ -62,6 +62,7 @@#include "err.c" #include "irq.c" #include "fs.c" +#include "genetlink.c" #include "gpu.c" #include "io.c" #include "jump_label.c"
rust/kernel/lib.rs +1 -0
@@ -96,6 +96,7 @@pub mod module_param;#[cfg( CONFIG_NET )] pub mod net; +pub mod netlink;pub mod num;pub mod of;#[cfg( CONFIG_PM_OPP )]
rust/kernel/netlink.rs new file +336 -0
336 lines not on this page · raw message
Signature · 1 line
2.54.0.746.g67dd491aae-goog
Re: [PATCH v5 1/2] rust: netlink: add raw netlink abstraction
Quoted 7 lines · Alice Ryhl
This implements a safe and relatively simple API over the netlink API, that allows you to add different attributes to a netlink message and broadcast it. As the first user of this API only makes use of broadcast, only broadcast messages are supported here.
rust/kernel/netlink.rs | 336 ++++++++++++++++++++++++++++++++++++++++
This should be added to an existing or new MAINTAINERS entry, i.e. as usual, either inheriting it if the maintainers want, or delegating it to a sub- or co-maintainer, etc. depending on what people want.
Cheers,
Miguel
Alice Ryhl
maintainerRe: [PATCH v5 1/2] rust: netlink: add raw netlink abstraction
Quoted 7 lines · Miguel Ojeda
Quoted 5 lines · Alice Ryhl
This implements a safe and relatively simple API over the netlink API, that allows you to add different attributes to a netlink message and broadcast it. As the first user of this API only makes use of broadcast, only broadcast messages are supported here.
Quoted 5 lines
Quoted 1 line
rust/kernel/netlink.rs | 336 ++++++++++++++++++++++++++++++++++++++++This should be added to an existing or new
MAINTAINERSentry, i.e. as usual, either inheriting it if the maintainers want, or delegating it to a sub- or co-maintainer, etc. depending on what people want.
Netdev maintainers, what do you prefer that I put for these files? I'm of course happy to be on the hook for this code.
We can either add it under an existing entry, or we could add a new one along these lines:
NETWORKING [RUST]
M: Alice Ryhl <[email protected]>
L: [email protected]
L: [email protected]
S: Supported
F: rust/helpers/genetlink.c
F: rust/kernel/netlink.rs
Of course, it would be useful to have others listed here, as I'm sure I will have questions about how networking works if there are patches in this area. Or perhaps the netdev@ list itself is enough for that to reach the right people?
With regards to actually merging code in this area, then I think for this particular series it is simpler to let Greg take it through char-misc with all the other Binder changes. But for any future changes to netlink.rs, I think it makes sense for them to land through the netdev tree.
Thoughts?
Alice
Jakub Kicinski
maintainerRe: [PATCH v5 1/2] rust: netlink: add raw netlink abstraction
Quoted 8 lines · On Wed, 27 May 2026 10:00:40 +0000 Alice Ryhl wrote:
Quoted 7 lines · Miguel Ojeda
Quoted 5 lines · Alice Ryhl
This implements a safe and relatively simple API over the netlink API, that allows you to add different attributes to a netlink message and broadcast it. As the first user of this API only makes use of broadcast, only broadcast messages are supported here.
Quoted 5 lines
Quoted 5 lines
Quoted 1 line
rust/kernel/netlink.rs | 336 ++++++++++++++++++++++++++++++++++++++++This should be added to an existing or new
MAINTAINERSentry, i.e. as usual, either inheriting it if the maintainers want, or delegating it to a sub- or co-maintainer, etc. depending on what people want.
Good call, missed this isn't under rust/kernel/net
Quoted 2 lines
Netdev maintainers, what do you prefer that I put for these files? I'm of course happy to be on the hook for this code.
Can we put it under rust/kernel/net and rust/helpers/net ? And then add those directories to networking ?
Just so that we don't have to keep wondering with every little piece of code.
Quoted 10 lines
We can either add it under an existing entry, or we could add a new one along these lines:
NETWORKING [RUST]
M: Alice Ryhl <[email protected]>
L: [email protected]
L: [email protected]
S: Supported
F:rust/helpers/genetlink.c
F:rust/kernel/netlink.rs
Probably not at this time.
Quoted 10 lines
Of course, it would be useful to have others listed here, as I'm sure I will have questions about how networking works if there are patches in this area. Or perhaps the netdev@ list itself is enough for that to reach the right people?
With regards to actually merging code in this area, then I think for this particular series it is simpler to let Greg take it through char-misc with all the other Binder changes. But for any future changes to
netlink.rs, I think it makes sense for them to land through the netdev tree.
SG, once acked it can go via any tree. Greg's tree make sense.
Alice Ryhl
maintainerRe: [PATCH v5 1/2] rust: netlink: add raw netlink abstraction
Quoted 17 lines · Jakub Kicinski
Quoted 8 lines · On Wed, 27 May 2026 10:00:40 +0000 Alice Ryhl wrote:
Quoted 7 lines · Miguel Ojeda
Quoted 5 lines · Alice Ryhl
This implements a safe and relatively simple API over the netlink API, that allows you to add different attributes to a netlink message and broadcast it. As the first user of this API only makes use of broadcast, only broadcast messages are supported here.
Quoted 5 lines
Quoted 5 lines
Quoted 1 line
rust/kernel/netlink.rs | 336 ++++++++++++++++++++++++++++++++++++++++This should be added to an existing or new
MAINTAINERSentry, i.e. as usual, either inheriting it if the maintainers want, or delegating it to a sub- or co-maintainer, etc. depending on what people want.Good call, missed this isn't under
rust/kernel/net
Quoted 7 lines
Quoted 2 lines
Netdev maintainers, what do you prefer that I put for these files? I'm of course happy to be on the hook for this code.
Can we put it under
rust/kernel/netandrust/helpers/net? And then add those directories to networking ?
Just so that we don't have to keep wondering with every little piece of code.
Sure, will do.
However, it doesn't look like rust/kernel/net/ is listed under any MAINTAINERS entry either.
Quoted 5 lines
Quoted 5 lines
Of course, it would be useful to have others listed here, as I'm sure I will have questions about how networking works if there are patches in this area. Or perhaps the netdev@ list itself is enough for that to reach the right people?
Quoted 7 lines
Quoted 5 lines
With regards to actually merging code in this area, then I think for this particular series it is simpler to let Greg take it through char-misc with all the other Binder changes. But for any future changes to
netlink.rs, I think it makes sense for them to land through the netdev tree.SG, once acked it can go via any tree. Greg's tree make sense.
SG, thanks!
Alice
Jakub Kicinski
maintainerRe: [PATCH v5 1/2] rust: netlink: add raw netlink abstraction
Quoted 8 lines · On Thu, 28 May 2026 06:25:48 +0000 Alice Ryhl wrote:
Quoted 7 lines
Quoted 2 lines
Netdev maintainers, what do you prefer that I put for these files? I'm of course happy to be on the hook for this code.
Can we put it under
rust/kernel/netandrust/helpers/net? And then add those directories to networking ?
Just so that we don't have to keep wondering with every little piece of code.
Quoted 4 lines
Sure, will do.
However, it doesn't look like
rust/kernel/net/is listed under anyMAINTAINERSentry either.
Which is why I said:
"And then add those directories to networking ?"
Alice Ryhl
maintainerRe: [PATCH v5 1/2] rust: netlink: add raw netlink abstraction
Quoted 9 lines · Jakub Kicinski
Quoted 8 lines · On Thu, 28 May 2026 06:25:48 +0000 Alice Ryhl wrote:
Quoted 7 lines
Quoted 2 lines
Netdev maintainers, what do you prefer that I put for these files? I'm of course happy to be on the hook for this code.
Can we put it under
rust/kernel/netandrust/helpers/net? And then add those directories to networking ?
Just so that we don't have to keep wondering with every little piece of code.
Quoted 8 lines
Quoted 4 lines
Sure, will do.
However, it doesn't look like
rust/kernel/net/is listed under anyMAINTAINERSentry either.Which is why I said:
"And then add those directories to networking ?"
Oh I missed that. Alright, let's do that.
Alice
Alice Ryhl
maintainerPATCH v5 2/2 rust_binder: report netlink transactions
In reply to Alice RyhlFrom: Carlos Llamas <[email protected]>
The Android Binder driver supports a netlink API that reports transaction *failures* to a userspace daemon. This allows devices to monitor processes with many failed transactions so that it can e.g. kill misbehaving apps.
One very important thing that this monitors is when many oneway messages are sent to a frozen process, so there is special handling to ensure this scenario is surfaced over netlink.
--- drivers/android/Kconfig | 2 +- drivers/android/binder/netlink.rs | 110 +++++++++++++++++++++++++++++ drivers/android/binder/rust_binder_main.rs | 8 ++- drivers/android/binder/thread.rs | 10 +++ drivers/android/binder/transaction.rs | 40 +++++++++++ rust/uapi/uapi_helper.h | 1 + 6 files changed, 168 insertions(+), 3 deletions(-)
drivers/android/Kconfig +1 -1
@@ -16,7 +16,7 @@ config ANDROID_BINDER_IPC config ANDROID_BINDER_IPC_RUST bool "Rust version of Android Binder IPC Driver" - depends on RUST && MMU && !ANDROID_BINDER_IPC + depends on RUST && MMU && NET && !ANDROID_BINDER_IPC help This enables the Rust implementation of the Binder driver.
drivers/android/binder/netlink.rs new file +110 -0
110 lines not on this page · raw message
drivers/android/binder/rust_binder_main.rs +6 -2
@@ -38,6 +38,7 @@mod deferred_close;mod defs;mod error; +mod netlink;mod node;mod page_range;mod process;
@@ -288,19 +289,22 @@ fn ptr_align(value: usize) -> Option<usize> {// SAFETY: We call register in `init`. static BINDER_SHRINKER :Shrinker =unsafe {Shrinker ::new () }; -struct BinderModule {} +struct BinderModule { + _netlink: kernel::netlink::Registration , +}impl kernel::Module for BinderModule {fn init (_module: &'static kernel::ThisModule ) ->Result <Self > {// SAFETY: The module initializer never runs twice, so we only call this once. unsafe {crate ::context::CONTEXTS .init () }; +let netlink =crate ::netlink::BINDER_NL_FAMILY .register ()?;BINDER_SHRINKER .register (c"android-binder" )?;// SAFETY: The module is being loaded, so we can initialize binderfs. unsafe { kernel::error::to_result (binderfs::init_rust_binderfs ())? }; -Ok (Self {}) +Ok (Self { _netlink: netlink }) } }
drivers/android/binder/thread.rs +10 -0
@@ -1263,6 +1263,15 @@ fn transaction(self: &Arc<Self>, cmd: u32, reader: &mut UserSliceReader) -> Resu } } +if info.oneway_spam_suspect { +// If this is both a oneway spam suspect and a failure, we report it twice. This is +// useful in case the transaction failed with BR_TRANSACTION_PENDING_FROZEN. + info.report_netlink (BR_ONEWAY_SPAM_SUSPECT , &self.process.ctx); + } +if info.reply !=0 { + info.report_netlink (info.reply, &self.process.ctx); + } +Ok (()) }
@@ -1332,6 +1341,7 @@ fn reply_inner(self: &Arc<Self>, info: &mut TransactionInfo) -> BinderResult { );let reply =Err (BR_FAILED_REPLY ); orig.from.deliver_reply (reply, &orig); + info.reply =BR_FAILED_REPLY ; err.reply =BR_TRANSACTION_COMPLETE ; err });
drivers/android/binder/transaction.rs +40 -0
@@ -3,6 +3,7 @@// Copyright (C) 2025 Google LLC. use kernel::{ + netlink::GENLMSG_DEFAULT_SIZE , prelude::*, seq_file::SeqFile , seq_print,
@@ -17,6 +18,7 @@ allocation::{Allocation, TranslatedFds}, defs::*, error::{BinderError, BinderResult}, + netlink::Report, node::{Node, NodeRef}, process::{Process, ProcessInner}, ptr_align,
@@ -49,6 +51,44 @@ impl TransactionInfo {pub (crate )fn is_oneway (&self) ->bool { self.flags &TF_ONE_WAY !=0 } + +pub (crate )fn report_netlink (&self, reply:u32 , ctx: &crate ::Context ) { +if let Err (err) = self.report_netlink_inner (reply, ctx) { +pr_warn! ( +"{}:{} netlink report failed: {err:?}\n" , + self.from_pid, + self.from_tid + ); + } + } + +fn report_netlink_inner (&self, reply:u32 , ctx: &crate ::Context ) -> kernel::error::Result { +if !Report ::has_listeners () { +return Ok (()); + } +let mut report =Report ::new (GENLMSG_DEFAULT_SIZE ,0 ,0 ,GFP_KERNEL )?; + + report.error (reply)?; + report.context (&ctx.name)?; + report.from_pid (self.from_pidas u32 )?; + report.from_tid (self.from_tidas u32 )?; +if self.to_pid !=0 { + report.to_pid (self.to_pidas u32 )?; + } +if self.to_tid !=0 { + report.to_tid (self.to_tidas u32 )?; + } + +if self.is_reply { + report.is_reply ()?; + } + report.flags (self.flags)?; + report.code (self.code)?; + report.data_size (self.data_sizeas u32 )?; + + report.multicast (0 ,GFP_KERNEL )?; +Ok (()) + } }use core::mem::offset_of;
rust/uapi/uapi_helper.h +1 -0
@@ -11,6 +11,7 @@#include <uapi/drm/nova_drm.h> #include <uapi/drm/panthor_drm.h> #include <uapi/linux/android/binder.h> +#include <uapi/linux/android/binder_netlink.h> #include <uapi/linux/mdio.h> #include <uapi/linux/mii.h> #include <uapi/linux/ethtool.h>
Signature · 1 line
2.54.0.746.g67dd491aae-goog