Access Rust fields from TP_fast_assign in TRACE_EVENT
Subsystems (4)
Alice Ryhl
maintainercover Access Rust fields from TP_fast_assign in TRACE_EVENT
This patch shows one possible mechanism for accessing Rust fields from the TP_fast_assign() body of a trace event. The idea is to have the driver expose the offsets of the relevant fields, and to have the C code use those offsets to access the data.
I'm currently using this mechanism in the Rust Binder driver on Android's fork of 6.12.
---
Alice Ryhl (2):
rust: sync: add Arc::DATA_OFFSET
rust_binder: add binder_transaction tracepoint
drivers/android/binder/node.rs | 8 +++ drivers/android/binder/process.rs | 7 +++ drivers/android/binder/rust_binder.h | 79 +++++++++++++++++++++++++++++ drivers/android/binder/rust_binder_events.h | 30 +++++++++++ drivers/android/binder/rust_binder_main.rs | 8 +++ drivers/android/binder/thread.rs | 1 + drivers/android/binder/trace.rs | 21 ++++++++ drivers/android/binder/transaction.rs | 14 +++++ rust/kernel/sync/arc.rs | 3 ++ 9 files changed, 171 insertions(+)
---
base-commit: 211ddde0823f1442e4ad052a2f30f050145ccada
change-id: 20251113-binder-trace1-b2d369f63e30
Best regards,
Signature · 1 line
Alice Ryhl <[email protected]>
Alice Ryhl
maintainerPATCH 1/2 rust: sync: add Arc::DATA_OFFSET
This constant will be used to expose some offset constants from the Rust Binder driver to tracepoints which are implemented in C. The constant is usually equal to sizeof(refcount_t), but may be larger if T has a large alignment.
--- rust/kernel/sync/arc.rs | 3 +++ 1 file changed, 3 insertions(+)
rust/kernel/sync/arc.rs +3 -0
@@ -240,6 +240,9 @@ pub fn new(contents: T, flags: Flags) -> Result<Self, AllocError> {// `Arc` object. Ok (unsafe {Self ::from_inner (inner) }) } + +/// The offset that the value is stored at. +pub const DATA_OFFSET :usize = core::mem::offset_of!(ArcInner <T >, data); }impl <T : ?Sized >Arc <T > {
Signature · 1 line
2.52.0.158.g65b55ccf14-goog
Re: [PATCH 1/2] rust: sync: add Arc::DATA_OFFSET
Quoted 33 lines · Alice Ryhl
This constant will be used to expose some offset constants from the Rust Binder driver to tracepoints which are implemented in C. The constant is usually equal to
sizeof(refcount_t), but may be larger if T has a large alignment.Signed-off-by Alice Ryhl <[email protected]>---
rust/kernel/sync/arc.rs| 3 +++1 file changed, 3 insertions(+)rust/kernel/sync/arc.rs +3
-0@@ -240,6 +240,9 @@ pub fn new(contents: T, flags: Flags) -> Result<Self, AllocError> {// `Arc` object. Ok (unsafe {Self ::from_inner (inner) }) } + +/// The offset that the value is stored at. +pub const DATA_OFFSET :usize = core::mem::offset_of!(ArcInner <T >, data); }impl <T : ?Sized >Arc <T > {Signature · 2 lines
-- 2.52.0.158.g65b55ccf14-goog
Alice Ryhl
maintainerPATCH 2/2 rust_binder: add binder_transaction tracepoint
In reply to Alice RyhlThis patch adds the binder_transaction tracepoint to Rust Binder. This was chosen as the next tracepoint to add as it is the most complex tracepoint. (And it's also an important tracepoint known to perfetto.)
--- drivers/android/binder/node.rs | 8 +++ drivers/android/binder/process.rs | 7 +++ drivers/android/binder/rust_binder.h | 79 +++++++++++++++++++++++++++++ drivers/android/binder/rust_binder_events.h | 30 +++++++++++ drivers/android/binder/rust_binder_main.rs | 8 +++ drivers/android/binder/thread.rs | 1 + drivers/android/binder/trace.rs | 21 ++++++++ drivers/android/binder/transaction.rs | 14 +++++ 8 files changed, 168 insertions(+)
drivers/android/binder/node.rs +8 -0
@@ -178,6 +178,14 @@ struct NodeInner { refs: List<NodeRefInfo, { NodeRefInfo::LIST_NODE }>, } +use kernel::bindings::rb_node_layout; +use mem::offset_of; +pub (crate )const NODE_LAYOUT :rb_node_layout =rb_node_layout { + arc_offset:Arc ::<Node >::DATA_OFFSET +offset_of! (DTRWrap <Node >, wrapped), + debug_id:offset_of! (Node , debug_id), + ptr:offset_of! (Node , ptr), +}; +#[pin_data] pub (crate )struct Node {pub (crate ) debug_id:usize ,
drivers/android/binder/process.rs +7 -0
@@ -387,6 +387,13 @@ fn new() -> Self { } } +use core::mem::offset_of; +use kernel::bindings::rb_process_layout; +pub (crate )const PROCESS_LAYOUT :rb_process_layout =rb_process_layout { + arc_offset:Arc ::<Process >::DATA_OFFSET , + task:offset_of! (Process , task), +}; +/// A process using binder. /// /// Strictly speaking, there can be multiple of these per process. There is one for each binder fd
drivers/android/binder/rust_binder.h +79 -0
83 lines not on this page · raw message
drivers/android/binder/rust_binder_events.h +30 -0
@@ -30,6 +30,36 @@ TRACE_EVENT(rust_binder_ioctl,TP_printk ("cmd=0x%x arg=0x%lx" , __entry->cmd, __entry->arg) ); +TRACE_EVENT (rust_binder_transaction, + TP_PROTO(bool reply, rust_binder_transaction t, struct task_struct *thread), +TP_ARGS (reply, t, thread), +TP_STRUCT__entry ( + __field(int, debug_id) + __field(int, target_node) + __field(int, to_proc) + __field(int, to_thread) + __field(int, reply) + __field(unsigned int, code) + __field(unsigned int, flags) + ), +TP_fast_assign ( + rust_binder_process to = rust_binder_transaction_to_proc(t); + rust_binder_node target_node = rust_binder_transaction_target_node(t); + + __entry->debug_id = rust_binder_transaction_debug_id(t); + __entry->target_node = target_node ? rust_binder_node_debug_id(target_node) : 0; + __entry->to_proc = rust_binder_process_task(to)->pid; + __entry->to_thread = thread ? thread->pid : 0; + __entry->reply = reply; + __entry->code = rust_binder_transaction_code(t); + __entry->flags = rust_binder_transaction_flags(t); + ), +TP_printk ("transaction=%d dest_node=%d dest_proc=%d dest_thread=%d reply=%d flags=0x%x code=0x%x" , + __entry->debug_id, __entry->target_node, + __entry->to_proc, __entry->to_thread, + __entry->reply, __entry->flags, __entry->code) +); +#endif /* _RUST_BINDER_TRACE_H */ /* This part must be outside protection */
drivers/android/binder/rust_binder_main.rs +8 -0
@@ -89,6 +89,14 @@ fn default() -> Self { license: "GPL", } +use kernel::bindings::rust_binder_layout; +#[no_mangle] +static RUST_BINDER_LAYOUT :rust_binder_layout =rust_binder_layout { + t: transaction::TRANSACTION_LAYOUT , + p: process::PROCESS_LAYOUT , + n: node::NODE_LAYOUT , +}; +fn next_debug_id () ->usize {static NEXT_DEBUG_ID :AtomicUsize =AtomicUsize ::new (0 );
drivers/android/binder/thread.rs +1 -0
@@ -1117,6 +1117,7 @@ fn deliver_single_reply( transaction: &DArc<Transaction>, ) -> bool {if let Ok (transaction) = &reply { +crate ::trace::trace_transaction (true , transaction,Some (&self.task)); transaction.set_outstanding (&mut self.process.inner.lock ()); }
drivers/android/binder/trace.rs +21 -0
@@ -2,11 +2,21 @@// Copyright (C) 2025 Google LLC. +use crate ::transaction::Transaction ; + +use kernel::bindings::{rust_binder_transaction, task_struct};use kernel::ffi::{c_uint, c_ulong}; +use kernel::task::Task ;use kernel::tracepoint::declare_trace;declare_trace! {unsafe fn rust_binder_ioctl (cmd:c_uint , arg:c_ulong ); +unsafe fn rust_binder_transaction (reply:bool , t:rust_binder_transaction , thread: *mut task_struct ); +} + +#[inline] +fn raw_transaction (t: &Transaction ) ->rust_binder_transaction { + tas *const Transaction as rust_binder_transaction }#[inline]
@@ -14,3 +24,14 @@ pub(crate) fn trace_ioctl(cmd: u32, arg: usize) {// SAFETY: Always safe to call. unsafe {rust_binder_ioctl (cmd, argas c_ulong ) } } + +#[inline] +pub (crate )fn trace_transaction (reply:bool , t: &Transaction , thread:Option <&Task >) { +let thread =match thread { +Some (thread) => thread.as_ptr (), +None => core::ptr::null_mut (), + }; +// SAFETY: The raw transaction is valid for the duration of this call. The thread pointer is +// valid or null. +unsafe {rust_binder_transaction (reply,raw_transaction (t), thread) } +}
drivers/android/binder/transaction.rs +14 -0
@@ -24,6 +24,17 @@ BinderReturnWriter, DArc, DLArc, DTRWrap, DeliverToRead, }; +use core::mem::offset_of; +use kernel::bindings::rb_transaction_layout; +pub (crate )const TRANSACTION_LAYOUT :rb_transaction_layout =rb_transaction_layout { + debug_id:offset_of! (Transaction , debug_id), + code:offset_of! (Transaction , code), + flags:offset_of! (Transaction , flags), + from_thread:offset_of! (Transaction , from), + to_proc:offset_of! (Transaction , to), + target_node:offset_of! (Transaction , target_node), +}; +#[pin_data( PinnedDrop )] pub (crate )struct Transaction {pub (crate ) debug_id:usize ,
@@ -249,6 +260,7 @@ pub(crate) fn submit(self: DLArc<Self>) -> BinderResult {if oneway {if let Some (target_node) = self.target_node.clone () { +crate ::trace::trace_transaction (false , &self,None );if process_inner.is_frozen { process_inner.async_recv =true ;if self.flags &TF_UPDATE_TXN !=0 {
@@ -286,11 +298,13 @@ pub(crate) fn submit(self: DLArc<Self>) -> BinderResult { }let res =if let Some (thread) = self.find_target_thread () { +crate ::trace::trace_transaction (false , &self,Some (&thread.task));match thread.push_work (self) {PushWorkRes ::Ok =>Ok (()),PushWorkRes ::FailedDead (me) =>Err ((BinderError ::new_dead (), me)), } }else { +crate ::trace::trace_transaction (false , &self,None ); process_inner.push_work (self) };drop (process_inner);
Signature · 1 line
2.52.0.158.g65b55ccf14-goog
Carlos Llamas
maintainerRe: [PATCH 2/2] rust_binder: add binder_transaction tracepoint
Quoted 6 lines · Alice Ryhl
This patch adds the
binder_transactiontracepoint to Rust Binder. This was chosen as the next tracepoint to add as it is the most complex tracepoint. (And it's also an important tracepoint known to perfetto.)Signed-off-by Alice Ryhl <[email protected]>---
This approach seems to work well considering the limitations. I wonder though if it would be more practical to simply pass the individual primitive values used by TP_prinkt() from the start? I suppose the downside would be the extra overhead with the tracepoints disabled. However, this might be neglectable and the solution much simpler?
drivers/android/binder/rust_binder.h struct inode; · line 82
Quoted 7 lines
drivers/android/binder/rust_binder.h +7
-0+static inline rust_binder_node rust_binder_transaction_target_node (rust_binder_transaction t) +{ +void *p = *(void **) (t +RUST_BINDER_LAYOUT .t.target_node); + +if (p) + p = p +RUST_BINDER_LAYOUT .n.arc_offset; +return NULL ;
It looks like this should have 'return p' right? Sorry for the late review this has been picked up so we might need a fixes for this.
--
Carlos Llamas
Alice Ryhl
maintainerRe: [PATCH 2/2] rust_binder: add binder_transaction tracepoint
Quoted 5 lines · Carlos Llamas
Quoted 4 lines · Alice Ryhl
This patch adds the
binder_transactiontracepoint to Rust Binder. This was chosen as the next tracepoint to add as it is the most complex tracepoint. (And it's also an important tracepoint known to perfetto.)
Quoted 8 lines
Quoted 2 lines
Signed-off-by Alice Ryhl <[email protected]>---
This approach seems to work well considering the limitations. I wonder though if it would be more practical to simply pass the individual primitive values used by
TP_prinkt()from the start? I suppose the downside would be the extra overhead with the tracepoints disabled. However, this might be neglectable and the solution much simpler?
I decided to go down this road because the same kinds of helpers would be useful for vendor hooks.
Quoted 10 lines
drivers/android/binder/rust_binder.h struct inode; · line 82
Quoted 7 lines
drivers/android/binder/rust_binder.h +7
-0+static inline rust_binder_node rust_binder_transaction_target_node (rust_binder_transaction t) +{ +void *p = *(void **) (t +RUST_BINDER_LAYOUT .t.target_node); + +if (p) + p = p +RUST_BINDER_LAYOUT .n.arc_offset; +return NULL ;It looks like this should have 'return p' right? Sorry for the late review this has been picked up so we might need a fixes for this.
Fix sent:
https://lore.kernel.org/r/20260128-binder-fix-target-node-null-v1-1-78d198ef55a5@google.com
No idea how this happened. Thanks.
Alice