Setting up Binder for the future
Subsystems (7)
Alice Ryhl
maintainercover Setting up Binder for the future
We're generally not proponents of rewrites (nasty uncomfortable things that make you late for dinner!). So why rewrite Binder?
Binder has been evolving over the past 15+ years to meet the evolving needs of Android. Its responsibilities, expectations, and complexity have grown considerably during that time. While we expect Binder to continue to evolve along with Android, there are a number of factors that currently constrain our ability to develop/maintain it. Briefly those are:
1. Complexity: Binder is at the intersection of everything in Android and fulfills many responsibilities beyond IPC. It has become many things to many people, and due to its many features and their interactions with each other, its complexity is quite high. In just 6kLOC it must deliver transactions to the right threads. It must correctly parse and translate the contents of transactions, which can contain several objects of different types (e.g., pointers, fds) that can interact with each other. It controls the size of thread pools in userspace, and ensures that transactions are assigned to threads in ways that avoid deadlocks where the threadpool has run out of threads. It must track refcounts of objects that are shared by several processes by forwarding refcount changes between the processes correctly. It must handle numerous error scenarios and it combines/nests 13 different locks, 7 reference counters, and atomic variables. Finally, It must do all of this as fast and efficiently as possible. Minor performance regressions can cause a noticeably degraded user experience.
2. Things to improve: Thousand-line functions [1], error-prone error handling [2], and confusing structure can occur as a code base grows organically. After more than a decade of development, this codebase could use an overhaul.
3. Security critical: Binder is a critical part of Android's sandboxing strategy. Even Android's most de-privileged sandboxes (e.g. the Chrome renderer, or SW Codec) have direct access to Binder. More than just about any other component, it's important that Binder provide robust security, and itself be robust against security vulnerabilities.
It's #1 (high complexity) that has made continuing to evolve Binder and resolving #2 (tech debt) exceptionally difficult without causing #3 (security issues). For Binder to continue to meet Android's needs, we need better ways to manage (and reduce!) complexity without increasing the risk.
The biggest change is obviously the choice of programming language. We decided to use Rust because it directly addresses a number of the challenges within Binder that we have faced during the last years. It prevents mistakes with ref counting, locking, bounds checking, and also does a lot to reduce the complexity of error handling. Additionally, we've been able to use the more expressive type system to encode the ownership semantics of the various structs and pointers, which takes the complexity of managing object lifetimes out of the hands of the programmer, reducing the risk of use-after-frees and similar problems.
Rust has many different pointer types that it uses to encode ownership semantics into the type system, and this is probably one of the most important aspects of how it helps in Binder. The Binder driver has a lot of different objects that have complex ownership semantics; some pointers own a refcount, some pointers have exclusive ownership, and some pointers just reference the object and it is kept alive in some other manner. With Rust, we can use a different pointer type for each kind of pointer, which enables the compiler to enforce that the ownership semantics are implemented correctly.
Another useful feature is Rust's error handling. Rust allows for more simplified error handling with features such as destructors, and you get compilation failures if errors are not properly handled. This means that even though Rust requires you to spend more lines of code than C on things such as writing down invariants that are left implicit in C, the Rust driver is still slightly smaller than C binder: Rust is 5.5kLOC and C is 5.8kLOC. (These numbers are excluding blank lines, comments, binderfs, and any debugging facilities in C that are not yet implemented in the Rust driver. The numbers include abstractions in rust/kernel/ that are unlikely to be used by other drivers than Binder.)
Although this rewrite completely rethinks how the code is structured and how assumptions are enforced, we do not fundamentally change *how* the driver does the things it does. A lot of careful thought has gone into the existing design. The rewrite is aimed rather at improving code health, structure, readability, robustness, security, maintainability and extensibility. We also include more inline documentation, and improve how assumptions in the code are enforced. Furthermore, all unsafe code is annotated with a SAFETY comment that explains why it is correct.
We have left the binderfs filesystem component in C. Rewriting it in Rust would be a large amount of work and requires a lot of bindings to the file system interfaces. Binderfs has not historically had the same challenges with security and complexity, so rewriting binderfs seems to have lower value than the rest of Binder.
Correctness and feature parity
------------------------------
Rust binder passes all tests that validate the correctness of Binder in the Android Open Source Project. We can boot a device, and run a variety of apps and functionality without issues. We have performed this both on the Cuttlefish Android emulator device, and on a Pixel 6 Pro.
As for feature parity, Rust binder currently implements all features that C binder supports, with the exception of some debugging facilities. The missing debugging facilities will be added before we submit the Rust implementation upstream.
Performance numbers
-------------------
We have tested the driver using two different benchmarks:
binderThroughputTest [3] and binderRpcBenchmark [4]. These benchmarks show that the Rust implementation has very promising performance characteristics. That said, these are only microbenchmarks with very simple workloads, and there is still a lot of work to be done before we can truly understand how the drivers compare in the real world.
binderThroughputTest:
Some visualizations of the benchmarking results are available at the following links:
Average latency with no payload: https://raw.githubusercontent.com/Darksonn/linux/rust-binder-rfc/img-for-rust-binder-rfc/Average%20latency%20with%20no%20payload.png
Average latency with 4k payload: https://raw.githubusercontent.com/Darksonn/linux/rust-binder-rfc/img-for-rust-binder-rfc/Average%20latency%20with%204k%20payload.png
99 percentile latency with no payload: https://raw.githubusercontent.com/Darksonn/linux/rust-binder-rfc/img-for-rust-binder-rfc/99%20percentile%20latency%20with%20no%20payload.png
99 percentile latency with 4k payload: https://raw.githubusercontent.com/Darksonn/linux/rust-binder-rfc/img-for-rust-binder-rfc/99%20percentile%20latency%20with%204k%20payload.png
Raw data with empty payloads:
+-----------+----------+---------+----------+---------+----------+----------+
| c/s pairs | Rust avg | C avg | Rust 99p | C 99p | Avg frac | 99p frac |
+-----------+----------+---------+----------+---------+----------+----------+
| 1 | 17.517 | 17.278 | 31.169 | 34.464 | +1.38% | -9.56% |
| 2 | 17.405 | 17.425 | 36.051 | 36.825 | -0.11% | -2.10% |
| 4 | 27.623 | 27.524 | 46.305 | 45.776 | +0.36% | +1.16% |
| 8 | 25.152 | 25.461 | 61.442 | 61.279 | -1.21% | +0.27% |
| 16 | 50.251 | 49.987 | 120.158 | 121.297 | +0.53% | -0.94% |
| 32 | 99.439 | 100.537 | 238.891 | 238.404 | -1.09% | +0.20% |
+-----------+----------+---------+----------+---------+----------+----------+
Raw data with 4k payloads:
+-----------+----------+---------+----------+---------+----------+----------+
| c/s pairs | Rust avg | C avg | Rust 99p | C 99p | Avg frac | 99p frac |
+-----------+----------+---------+----------+---------+----------+----------+
| 1 | 19.422 | 19.811 | 30.233 | 31.616 | -1.96% | -4.37% |
| 2 | 18.393 | 18.277 | 34.790 | 35.319 | +0.63% | -1.50% |
| 4 | 29.350 | 29.283 | 48.544 | 47.730 | +0.23% | +1.71% |
| 8 | 25.075 | 25.283 | 66.040 | 65.226 | -0.82% | +1.25% |
| 16 | 58.608 | 58.949 | 156.657 | 159.709 | -0.58% | -1.91% |
| 32 | 127.404 | 129.459 | 321.249 | 326.945 | -1.59% | -1.74% |
+-----------+----------+---------+----------+---------+----------+----------+These tables depict roundtrip latencies of transactions as measured by binderThroughputTest. Each measurement is given in microseconds. Each row has a sample size of 10 million iterations. Negative percentages are better for Rust.
We've found that Rust binder has similar performance to C binder on the binderThroughputTest benchmark. The average latencies fluctuate between -1.96% and +1.38%.
binderRpcBenchmark:
+---------------------+-----------+---------+----------+---------+-----------+----------+
| Benchmark | Time Rust | Time C | CPU Rust | CPU C | Time frac | CPU frac |
+---------------------+-----------+---------+----------+---------+-----------+----------+
| pingTransaction | 21.595 | 22.167 | 9.625 | 9.692 | -2.58% | -0.69% |
| repeatBinder | 33.982 | 34.648 | 16.252 | 16.681 | -1.92% | -2.57% |
| throughput/64 | 26.774 | 26.587 | 11.995 | 11.823 | +0.70% | +1.45% |
| throughput/1024 | 33.679 | 33.867 | 15.140 | 15.137 | -0.56% | +0.02% |
| throughput/2048 | 39.744 | 40.092 | 17.898 | 17.926 | -0.87% | -0.16% |
| throughput/4096 | 52.585 | 53.457 | 23.788 | 24.067 | -1.63% | -1.16% |
| throughput/8182 | 76.352 | 77.148 | 35.135 | 35.228 | -1.03% | -0.26% |
| throughput/16364 | 121.875 | 122.877 | 57.342 | 57.614 | -0.82% | -0.47% |
| throughput/32728 | 212.380 | 212.765 | 101.838 | 101.589 | -0.18% | +0.25% |
| throughput/65535 | 442.983 | 421.935 | 222.642 | 212.494 | +4.99% | +4.78% |
| throughput/65536 | 431.250 | 416.916 | 216.634 | 210.160 | +3.44% | +3.08% |
| throughput/65537 | 512.902 | 492.272 | 242.472 | 232.786 | +4.19% | +4.16% |
| repeatTwoPageString | 456.546 | 445.398 | 222.921 | 219.821 | +2.50% | +1.41% |
+---------------------+-----------+---------+----------+---------+-----------+----------+This table depicts wall clock time and cpu time measurements over various test cases. Each measurement is given in microseconds. The throughput benchmarks correspond to the BM_throughputForTransportAndBytes test case, and the number is the size of the payload. Negative percentages are better for Rust.
From the above, we find that Rust binder is competitive for all test cases except for those with very large transaction sizes. However, this is a very rare case in practice [5] and we've been able to fix all other performance issues that we've run into, so there's no reason to think that we won't also be able to fix this issue. We did not fix it for this RFC because we prioritized getting the RFC out to provide context for the upcoming discussion at Linux Plumbers Conference [6].
We ran all of the benchmarks with cross-language LTO enabled, so that C code can be inlined into Rust code. We get similar results on the Cuttlefish Android emulator (which has an x86 architecture).
The Binder driver is very performance critical, and although our initial numbers are promising, we must gain a better understanding of how it performs in realistic workloads and not just in simple benchmarks. What we ultimately care about is the performance impact that it has on the whole system. Much work remains to be done on this front.
Dependencies
------------
When implementing kernel drivers in Rust, you must write bindings for each subsystem that we need to call into from Rust. Binder requires quite a few of them. We have not included them in this patch series, but you can view them at the following branch:
https://github.com/Darksonn/linux/commits/rust-binder-rfc
The branch is based on top of commit 639409a4ac8e ("Merge tag 'wq-for-6.7-rust-bindings' of git://git.kernel.org/pub/scm/linux/kernel/git/tj/wq"),
which is available in mainline. I did not base it on a tag, since there is not yet any tag that includes the Rust workqueue abstractions.
This RFC uses the kernel's red-black tree for key/value mappings, but we are aware that the red-black tree is deprecated. We did this to make the performance comparison more fair, since C binder also uses rbtree for this. We intend to replace these with XArrays instead. That said, we don't think that XArray is a good fit for the range allocator, and we propose to continue using the red-black tree for the range allocator. (see patch 6)
Thank you
---------
* Wedson Almeida Filho who wrote the first version of the driver and started the project.
* Miguel Ojeda for his support, and leading the Rust-for-Linux effort, and helping us navigate the upstream community.
* Matt Gilbride for his work on the range allocator and oneway spam detection.
* Carlos Llamas for patiently answering all my questions to help me understand the C driver, and co-presenting with me at LPC and Kangrejos.
* Greg KH for reviews and guidance on upstream development.
* Todd Kjos for reviewing the cover letter, answering questions, and pointers on benchmarking the driver.
* Matthew Maurer for his mentorship and help with navigating the build system, including getting LTO working.
* John Stultz for his help with debugging a performance issue.
* Andreas Hindborg for his help with getting LTO working.
* Benno Lossin, Gary Guo, Andreas Hindborg, Miguel Ojeda, Wedson Almeida Filho, Martin Rodriguez Reboredo, Björn Roy Baron, Boqun Feng, Tejun Heo, Nathan Huckleberry for reviewing various bindings needed by Binder.
Thank you,
Alice
[1]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/android/binder.c?h=v6.5#n2896
[2]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/android/binder.c?h=v6.5#n3658
[3]: https://android-review.googlesource.com/c/platform/frameworks/native/+/2680818
[4]: https://cs.android.com/android/platform/superproject/main/+/main:frameworks/native/libs/binder/tests/binderRpcBenchmark.cpp
[5]: https://cs.android.com/android/_/android/platform/frameworks/native/+/b85e7f7dbd0463d2ba78d53d50e64489fcb01ec4:libs/binder/tests/binderRpcBenchmark.cpp;l=206-217;bpv=1;bpt=0
[6]: https://lpc.events/event/17/contributions/1427/
---
Alice Ryhl (15):
rust_binder: add binderfs support to Rust binder
rust_binder: add threading support
rust_binder: add work lists
rust_binder: add nodes and context managers
rust_binder: add oneway transactions
rust_binder: serialize oneway transactions
rust_binder: send nodes in transactions
rust_binder: add BINDER_TYPE_PTR support
rust_binder: add BINDER_TYPE_FD support
rust_binder: add BINDER_TYPE_FDA support
rust_binder: add process freezing
rust_binder: add TF_UPDATE_TXN support
rust_binder: add binder_logs/state
rust_binder: add vma shrinker
binder: delete the C implementation
Matt Gilbride (1):
rust_binder: add oneway spam detection
Wedson Almeida Filho (4):
rust_binder: define a Rust binder driver
rust_binder: add epoll support
rust_binder: add non-oneway transactions
rust_binder: add death notifications
drivers/android/Kconfig | 19 +-
drivers/android/Makefile | 2 +
drivers/android/allocation.rs | 541 ++
drivers/android/binder.c | 6630 -----------------------
drivers/android/binder_alloc.c | 1284 -----
drivers/android/context.rs | 225 +
drivers/android/defs.rs | 171 +
drivers/android/error.rs | 94 +
drivers/android/node.rs | 761 +++
drivers/android/process.rs | 1412 +++++
drivers/android/range_alloc.rs | 442 ++
drivers/android/rust_binder.rs | 389 ++
drivers/android/{binderfs.c => rust_binderfs.c} | 135 +-
drivers/android/thread.rs | 1552 ++++++
drivers/android/transaction.rs | 428 ++
include/linux/rust_binder.h | 16 +
include/uapi/linux/android/binder.h | 30 +-
include/uapi/linux/magic.h | 1 +
rust/bindings/bindings_helper.h | 6 +
rust/helpers.c | 48 +
rust/kernel/file.rs | 2 +-
rust/kernel/lib.rs | 9 +
rust/kernel/page_range.rs | 715 +++
rust/kernel/security.rs | 33 +
rust/kernel/seq_file.rs | 47 +
rust/kernel/sync/condvar.rs | 10 +
rust/kernel/sync/lock.rs | 24 +
rust/kernel/sync/lock/mutex.rs | 10 +
rust/kernel/sync/lock/spinlock.rs | 10 +
rust/kernel/task.rs | 2 +-
scripts/Makefile.build | 2 +-
31 files changed, 7061 insertions(+), 7989 deletions(-)---
base-commit: b4be1bd6c44225bf7276a4666fd30b8da9cba517
change-id: 20231101-rust-binder-464b89651887
Best regards,
Signature · 1 line
Alice Ryhl <[email protected]>
Alice Ryhl
maintainerRFC PATCH 1/20 rust_binder: define a Rust binder driver
From: Wedson Almeida Filho <[email protected]>
Define the Rust binder driver, and set up the helpers for making C types accessible from Rust.
--- drivers/android/Kconfig | 11 +++++++++++ drivers/android/Makefile | 1 + drivers/android/rust_binder.rs | 21 +++++++++++++++++++++ include/uapi/linux/android/binder.h | 30 ++++++++++++++++-------------- rust/bindings/bindings_helper.h | 1 + 5 files changed, 50 insertions(+), 14 deletions(-)
drivers/android/Kconfig +11 -0
@@ -13,6 +13,17 @@ config ANDROID_BINDER_IPC Android process, using Binder to identify, invoke and pass arguments between said processes. +config ANDROID_BINDER_IPC_RUST + bool "Android Binder IPC Driver in Rust" + depends on MMU && RUST + help + Binder is used in Android for both communication between processes, + and remote method invocation. + + This means one Android process can call a method/routine in another + Android process, using Binder to identify, invoke and pass arguments + between said processes. + config ANDROID_BINDERFS bool "Android Binderfs filesystem" depends on ANDROID_BINDER_IPC
drivers/android/Makefile +1 -0
@@ -4,3 +4,4 @@ ccflags-y += -I$(src) # needed for trace events obj-$(CONFIG_ANDROID_BINDERFS) += binderfs.o obj-$(CONFIG_ANDROID_BINDER_IPC) += binder.o binder_alloc.o obj-$(CONFIG_ANDROID_BINDER_IPC_SELFTEST) += binder_alloc_selftest.o +obj-$(CONFIG_ANDROID_BINDER_IPC_RUST) += rust_binder.o
drivers/android/rust_binder.rs new file +21 -0
@@ -0,0 +1,21 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Binder -- the Android IPC mechanism. + +use kernel::prelude::*; + +module! { +type :BinderModule , + name:"rust_binder" , + author:"Wedson Almeida Filho, Alice Ryhl" , + description:"Android Binder" , + license:"GPL" , +} + +struct BinderModule {} + +impl kernel::Module for BinderModule { +fn init (_module: &'static kernel::ThisModule ) ->Result <Self > { +Ok (Self {}) + } +}
include/uapi/linux/android/binder.h +16 -14
@@ -251,20 +251,22 @@ struct binder_extended_error {__s32 param; }; -#define BINDER_WRITE_READ _IOWR('b', 1, struct binder_write_read) -#define BINDER_SET_IDLE_TIMEOUT _IOW('b', 3, __s64) -#define BINDER_SET_MAX_THREADS _IOW('b', 5, __u32) -#define BINDER_SET_IDLE_PRIORITY _IOW('b', 6, __s32) -#define BINDER_SET_CONTEXT_MGR _IOW('b', 7, __s32) -#define BINDER_THREAD_EXIT _IOW('b', 8, __s32) -#define BINDER_VERSION _IOWR('b', 9, struct binder_version) -#define BINDER_GET_NODE_DEBUG_INFO _IOWR('b', 11, struct binder_node_debug_info) -#define BINDER_GET_NODE_INFO_FOR_REF _IOWR('b', 12, struct binder_node_info_for_ref) -#define BINDER_SET_CONTEXT_MGR_EXT _IOW('b', 13, struct flat_binder_object) -#define BINDER_FREEZE _IOW('b', 14, struct binder_freeze_info) -#define BINDER_GET_FROZEN_INFO _IOWR('b', 15, struct binder_frozen_status_info) -#define BINDER_ENABLE_ONEWAY_SPAM_DETECTION _IOW('b', 16, __u32) -#define BINDER_GET_EXTENDED_ERROR _IOWR('b', 17, struct binder_extended_error) +enum { + BINDER_WRITE_READ = _IOWR('b', 1, struct binder_write_read), +BINDER_SET_IDLE_TIMEOUT =_IOW ('b' ,3 , __s64), +BINDER_SET_MAX_THREADS =_IOW ('b' ,5 , __u32), +BINDER_SET_IDLE_PRIORITY =_IOW ('b' ,6 , __s32), +BINDER_SET_CONTEXT_MGR =_IOW ('b' ,7 , __s32), +BINDER_THREAD_EXIT =_IOW ('b' ,8 , __s32), + BINDER_VERSION = _IOWR('b', 9, struct binder_version), + BINDER_GET_NODE_DEBUG_INFO = _IOWR('b', 11, struct binder_node_debug_info), + BINDER_GET_NODE_INFO_FOR_REF = _IOWR('b', 12, struct binder_node_info_for_ref), + BINDER_SET_CONTEXT_MGR_EXT = _IOW('b', 13, struct flat_binder_object), + BINDER_FREEZE = _IOW('b', 14, struct binder_freeze_info), + BINDER_GET_FROZEN_INFO = _IOWR('b', 15, struct binder_frozen_status_info), +BINDER_ENABLE_ONEWAY_SPAM_DETECTION =_IOW ('b' ,16 , __u32), + BINDER_GET_EXTENDED_ERROR = _IOWR('b', 17, struct binder_extended_error), +};/* * NOTE: Two special error codes you should check for when calling
rust/bindings/bindings_helper.h +1 -0
@@ -21,6 +21,7 @@#include <linux/sched.h> #include <linux/task_work.h> #include <linux/workqueue.h> +#include <uapi/linux/android/binder.h> /* `bindgen` gets confused at certain things. */ const size_t BINDINGS_ARCH_SLAB_MINALIGN =ARCH_SLAB_MINALIGN ;
Signature · 1 line
2.42.0.820.g83a721a137-goog
Greg Kroah-Hartman
maintainerRe: [PATCH RFC 01/20] rust_binder: define a Rust binder driver
Quoted 21 lines · Alice Ryhl
From: Wedson Almeida Filho <[email protected]>
Define the Rust binder driver, and set up the helpers for making C types accessible from Rust.
Signed-off-by Wedson Almeida Filho <[email protected]>Co-developed-by Alice Ryhl <[email protected]>Signed-off-by Alice Ryhl <[email protected]>--- drivers/android/Kconfig | 11 +++++++++++ drivers/android/Makefile | 1 + drivers/android/rust_binder.rs | 21 +++++++++++++++++++++ include/uapi/linux/android/binder.h | 30 ++++++++++++++++-------------- rust/bindings/bindings_helper.h | 1 + 5 files changed, 50 insertions(+), 14 deletions(-)drivers/android/Kconfig +0
-0
drivers/android/Kconfig config ANDROID_BINDER_IPC · line 18
Quoted 6 lines
drivers/android/Kconfig +3
-0@@ -13,6 +13,17 @@ config ANDROID_BINDER_IPC Android process, using Binder to identify, invoke and pass arguments between said processes. +config ANDROID_BINDER_IPC_RUST + bool "Android Binder IPC Driver in Rust" + depends on MMU && RUST
Can RUST even build on non-mmu systems?
Alice Ryhl
maintainerRe: [PATCH RFC 01/20] rust_binder: define a Rust binder driver
Quoted 6 lines · Greg Kroah-Hartman
drivers/android/Kconfig config ANDROID_BINDER_IPC · line 18
Quoted 3 lines · Alice Ryhl
drivers/android/Kconfig +3
-0+config ANDROID_BINDER_IPC_RUST + bool "Android Binder IPC Driver in Rust" + depends on MMU && RUSTCan RUST even build on non-mmu systems?
I don't know, but if it could, then the dependencies of this RFC probably broke that. I guess this depends on is in the wrong place.
Alice
Re: [PATCH RFC 01/20] rust_binder: define a Rust binder driver
In reply to Alice RyhlOn Wed, Nov 01, 2023 at 06:01:31PM +0000, Alice Ryhl wrote:
[...]
rust/bindings/bindings_helper.h line 24
Quoted 7 lines
rust/bindings/bindings_helper.h +1
-0@@ -21,6 +21,7 @@#include <linux/sched.h> #include <linux/task_work.h> #include <linux/workqueue.h> +#include <uapi/linux/android/binder.h>
I wonder whether we could (and should) move this into rust/uapi/uapi_helpers.h
Regards,
Boqun
Quoted 7 lines
rust/bindings/bindings_helper.h +0
-0/* `bindgen` gets confused at certain things. */ const size_t BINDINGS_ARCH_SLAB_MINALIGN =ARCH_SLAB_MINALIGN ;Signature · 1 line
2.42.0.820.g83a721a137-goog
Alice Ryhl
maintainerRe: [PATCH RFC 01/20] rust_binder: define a Rust binder driver
Quoted 4 lines · Boqun Feng
Quoted 1 line
+#include <
uapi/linux/android/binder.h>I wonder whether we could (and should) move this into
rust/uapi/uapi_helpers.h
Sure. That was introduced after I wrote this part, but I'm happy to move it if you think it would make sense.
Alice
Alice Ryhl
maintainerRFC PATCH 2/20 rust_binder: add binderfs support to Rust binder
In reply to Alice RyhlAdd support for accessing the Rust binder driver via binderfs. The actual binderfs implementation is done entirely in C, and the rust_binderfs.c file is a modified version of binderfs.c that is adjusted to call into the Rust binder driver rather than the C driver.
We have left the binderfs filesystem component in C. Rewriting it in Rust would be a large amount of work and requires a lot of bindings to the file system interfaces. Binderfs has not historically had the same challenges with security and complexity, so rewriting Binderfs seems to have lower value than the rest of Binder.
We also add code on the Rust side for binderfs to call into. Most of this is left as stub implementation, with the exception of closing the file descriptor and the BINDER_VERSION ioctl.
--- drivers/android/Kconfig | 24 ++ drivers/android/Makefile | 1 + drivers/android/context.rs | 144 +++++++ drivers/android/defs.rs | 39 ++ drivers/android/process.rs | 251 ++++++++++++ drivers/android/rust_binder.rs | 196 ++++++++- drivers/android/rust_binderfs.c | 866 ++++++++++++++++++++++++++++++++++++++++ include/linux/rust_binder.h | 16 + include/uapi/linux/magic.h | 1 + rust/bindings/bindings_helper.h | 2 + rust/kernel/lib.rs | 7 + scripts/Makefile.build | 2 +- 12 files changed, 1547 insertions(+), 2 deletions(-)
drivers/android/Kconfig +24 -0
@@ -36,6 +36,18 @@ config ANDROID_BINDERFS It can be used to dynamically allocate new binder IPC devices via ioctls. +config ANDROID_BINDERFS_RUST + bool "Android Binderfs filesystem in Rust" + depends on ANDROID_BINDER_IPC_RUST + default n + help + Binderfs is a pseudo-filesystem for the Android Binder IPC driver + which can be mounted per-ipc namespace allowing to run multiple + instances of Android. + Each binderfs mount initially only contains a binder-control device. + It can be used to dynamically allocate new binder IPC devices via + ioctls. + config ANDROID_BINDER_DEVICES string "Android Binder devices" depends on ANDROID_BINDER_IPC
@@ -48,6 +60,18 @@ config ANDROID_BINDER_DEVICES created. Each binder device has its own context manager, and is therefore logically separated from the other devices. +config ANDROID_BINDER_DEVICES_RUST + string "Android Binder devices in Rust" + depends on ANDROID_BINDER_IPC_RUST + default "binder,hwbinder,vndbinder" + help + Default value for the binder.devices parameter. + + The binder.devices parameter is a comma-separated list of strings + that specifies the names of the binder device nodes that will be + created. Each binder device has its own context manager, and is + therefore logically separated from the other devices. + config ANDROID_BINDER_IPC_SELFTEST bool "Android Binder IPC Driver Selftest" depends on ANDROID_BINDER_IPC
drivers/android/Makefile +1 -0
@@ -5,3 +5,4 @@ obj-$(CONFIG_ANDROID_BINDERFS) += binderfs.o obj-$(CONFIG_ANDROID_BINDER_IPC) += binder.o binder_alloc.o obj-$(CONFIG_ANDROID_BINDER_IPC_SELFTEST) += binder_alloc_selftest.o obj-$(CONFIG_ANDROID_BINDER_IPC_RUST) += rust_binder.o +obj-$(CONFIG_ANDROID_BINDERFS_RUST) += rust_binderfs.o
drivers/android/context.rs new file +144 -0
144 lines not on this page · raw message
drivers/android/defs.rs new file +39 -0
@@ -0,0 +1,39 @@ +// SPDX-License-Identifier: GPL-2.0 + +use core::ops::{Deref ,DerefMut }; +use kernel::{ + bindings, + io_buffer::{ReadableFromBytes ,WritableToBytes }, +}; + +macro_rules! decl_wrapper { + ($newname:ident, $wrapped:ty) => { +#[derive( Copy , Clone , Default )] +#[repr(transparent)] +pub (crate )struct $newname($wrapped); +// SAFETY: This macro is only used with types where this is ok. +unsafe impl ReadableFromBytes for $newname {} +unsafe impl WritableToBytes for $newname {} +impl Deref for $newname { +type Target = $wrapped; +fn deref (&self) -> &Self ::Target { + &self.0 + } + } +impl DerefMut for $newname { +fn deref_mut (&mut self) -> &mut Self ::Target { + &mut self.0 + } + } + }; +} + +decl_wrapper! (BinderVersion , bindings::binder_version); + +impl BinderVersion { +pub (crate )fn current () ->Self { +Self (bindings::binder_version { + protocol_version: bindings::BINDER_CURRENT_PROTOCOL_VERSION as _ , + }) + } +}
drivers/android/process.rs new file +251 -0
251 lines not on this page · raw message
drivers/android/rust_binder.rs +195 -1
208 lines not on this page · raw message
drivers/android/rust_binderfs.c new file +866 -0
866 lines not on this page · raw message
include/linux/rust_binder.h new file +16 -0
@@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_RUST_BINDER_H +#define _LINUX_RUST_BINDER_H + +#include <uapi/linux/android/binderfs.h> + +/* +* This typedef is used for Rust binder driver instances. The driver object is +* completely opaque from C and can only be accessed via calls into Rust, so we +* use a typedef. +*/ +typedef void *rust_binder_device ; + +int init_rust_binderfs (void ); + +#endif
include/uapi/linux/magic.h +1 -0
@@ -82,6 +82,7 @@#define BINFMTFS_MAGIC 0x42494e4d#define DEVPTS_SUPER_MAGIC 0x1cd1#define BINDERFS_SUPER_MAGIC 0x6c6f6f70 +#define RUST_BINDERFS_SUPER_MAGIC 0x6c6f6f71#define FUTEXFS_SUPER_MAGIC 0xBAD1DEA#define PIPEFS_MAGIC 0x50495045#define PROC_SUPER_MAGIC 0x9fa0
rust/bindings/bindings_helper.h +2 -0
@@ -17,11 +17,13 @@#include <linux/security.h> #include <linux/slab.h> #include <linux/refcount.h> +#include <linux/rust_binder.h> #include <linux/wait.h> #include <linux/sched.h> #include <linux/task_work.h> #include <linux/workqueue.h> #include <uapi/linux/android/binder.h> +#include <uapi/linux/android/binderfs.h> /* `bindgen` gets confused at certain things. */ const size_t BINDINGS_ARCH_SLAB_MINALIGN =ARCH_SLAB_MINALIGN ;
rust/kernel/lib.rs +7 -0
@@ -99,6 +99,13 @@ impl ThisModule {pub const unsafe fn from_ptr (ptr: *mut bindings::module ) ->ThisModule {ThisModule (ptr) } + +/// Access the raw pointer for this module. +/// +/// It is up to the user to use it correctly. +pub const fn as_ptr (&self) -> *mut bindings::module { + self.0 + } }#[cfg(not(any(testlib, test)))]
scripts/Makefile.build +1 -1
@@ -262,7 +262,7 @@ $(obj)/%.lst: $(src)/%.c FORCE # Compile Rust sources (.rs) # --------------------------------------------------------------------------- -rust_allowed_features := new_uninit,offset_of +rust_allowed_features := const_maybe_uninit_zeroed,new_uninit,offset_of # `--out-dir` is required to avoid temporaries being created by `rustc` in the # current working directory, which may be not accessible in the out-of-tree
Signature · 1 line
2.42.0.820.g83a721a137-goog
Greg Kroah-Hartman
maintainerRe: [PATCH RFC 02/20] rust_binder: add binderfs support to Rust binder
Quoted 38 lines · Alice Ryhl
Add support for accessing the Rust binder driver via binderfs. The actual binderfs implementation is done entirely in C, and the
rust_binderfs.cfile is a modified version ofbinderfs.cthat is adjusted to call into the Rust binder driver rather than the C driver.We have left the binderfs filesystem component in C. Rewriting it in Rust would be a large amount of work and requires a lot of bindings to the file system interfaces. Binderfs has not historically had the same challenges with security and complexity, so rewriting Binderfs seems to have lower value than the rest of Binder.
We also add code on the Rust side for binderfs to call into. Most of this is left as stub implementation, with the exception of closing the file descriptor and the
BINDER_VERSIONioctl.Co-developed-by Wedson Almeida Filho <[email protected]>Signed-off-by Wedson Almeida Filho <[email protected]>Signed-off-by Alice Ryhl <[email protected]>--- drivers/android/Kconfig | 24 ++ drivers/android/Makefile | 1 + drivers/android/context.rs | 144 +++++++ drivers/android/defs.rs | 39 ++ drivers/android/process.rs | 251 ++++++++++++ drivers/android/rust_binder.rs | 196 ++++++++- drivers/android/rust_binderfs.c | 866 ++++++++++++++++++++++++++++++++++++++++ include/linux/rust_binder.h | 16 + include/uapi/linux/magic.h | 1 + rust/bindings/bindings_helper.h | 2 + rust/kernel/lib.rs | 7 + scripts/Makefile.build | 2 +- 12 files changed, 1547 insertions(+), 2 deletions(-)drivers/android/Kconfig +0
-0
drivers/android/Kconfig config ANDROID_BINDERFS · line 42
Quoted 7 lines
drivers/android/Kconfig +4
-0@@ -36,6 +36,18 @@ config ANDROID_BINDERFS It can be used to dynamically allocate new binder IPC devices via ioctls. +config ANDROID_BINDERFS_RUST + bool "Android Binderfs filesystem in Rust" + depends on ANDROID_BINDER_IPC_RUST + default n
Nit, the default is always 'n', so no need for this line.
Also, it's the middle of the merge window, many of us are busy with other things and can't review new code until a few weeks from now, sorry.
greg k-h
Alice Ryhl
maintainerRe: [PATCH RFC 02/20] rust_binder: add binderfs support to Rust binder
Quoted 7 lines · Greg Kroah-Hartman
drivers/android/Kconfig config ANDROID_BINDERFS · line 42
Quoted 4 lines · Alice Ryhl
drivers/android/Kconfig +4
-0+config ANDROID_BINDERFS_RUST + bool "Android Binderfs filesystem in Rust" + depends on ANDROID_BINDER_IPC_RUST + default nNit, the default is always 'n', so no need for this line.
Got it. I'll remove it.
Quoted 3 lines
Also, it's the middle of the merge window, many of us are busy with other things and can't review new code until a few weeks from now, sorry.
That's fine. I had hoped to send it earlier to avoid this, but we wanted performance numbers from real hardware instead of just from an emulator, which delayed it.
Alice
Re: [PATCH RFC 02/20] rust_binder: add binderfs support to Rust binder
In reply to Alice RyhlQuoted 56 lines · Alice Ryhl
Add support for accessing the Rust binder driver via binderfs. The actual binderfs implementation is done entirely in C, and the
rust_binderfs.cfile is a modified version ofbinderfs.cthat is adjusted to call into the Rust binder driver rather than the C driver.We have left the binderfs filesystem component in C. Rewriting it in Rust would be a large amount of work and requires a lot of bindings to the file system interfaces. Binderfs has not historically had the same challenges with security and complexity, so rewriting Binderfs seems to have lower value than the rest of Binder.
We also add code on the Rust side for binderfs to call into. Most of this is left as stub implementation, with the exception of closing the file descriptor and the
BINDER_VERSIONioctl.Co-developed-by Wedson Almeida Filho <[email protected]>Signed-off-by Wedson Almeida Filho <[email protected]>Signed-off-by Alice Ryhl <[email protected]>--- drivers/android/Kconfig | 24 ++ drivers/android/Makefile | 1 + drivers/android/context.rs | 144 +++++++ drivers/android/defs.rs | 39 ++ drivers/android/process.rs | 251 ++++++++++++ drivers/android/rust_binder.rs | 196 ++++++++- drivers/android/rust_binderfs.c | 866 ++++++++++++++++++++++++++++++++++++++++ include/linux/rust_binder.h | 16 + include/uapi/linux/magic.h | 1 + rust/bindings/bindings_helper.h | 2 + rust/kernel/lib.rs | 7 + scripts/Makefile.build | 2 +- 12 files changed, 1547 insertions(+), 2 deletions(-)drivers/android/Kconfig +0
-0drivers/android/Makefile +0
-0drivers/android/context.rs new file +0
-0drivers/android/defs.rs new file +4
-0@@ -0,0 +1,39 @@ +// SPDX-License-Identifier: GPL-2.0 + +use core::ops::{Deref ,DerefMut }; +use kernel::{
drivers/android/defs.rs line 14
Quoted 10 lines
drivers/android/defs.rs new file +10
-0+ bindings, + io_buffer::{ReadableFromBytes, WritableToBytes}, +}; + +macro_rules! decl_wrapper { + ($newname:ident, $wrapped:ty) => { +#[derive( Copy , Clone , Default )] +#[repr(transparent)] +pub (crate )struct $newname($wrapped); +// SAFETY: This macro is only used with types where this is ok.
Would it make sense so also annotade this safety requirement on the macro itself?
It is only file private, but could help not overlook it, when using for something new in the same file.
Quoted 57 lines
drivers/android/defs.rs new file +25
-0+unsafe impl ReadableFromBytes for $newname {} +unsafe impl WritableToBytes for $newname {} +impl Deref for $newname { +type Target = $wrapped; +fn deref (&self) -> &Self ::Target { + &self.0 + } + } +impl DerefMut for $newname { +fn deref_mut (&mut self) -> &mut Self ::Target { + &mut self.0 + } + } + }; +} + +decl_wrapper! (BinderVersion , bindings::binder_version); + +impl BinderVersion { +pub (crate )fn current () ->Self { +Self (bindings::binder_version { + protocol_version: bindings::BINDER_CURRENT_PROTOCOL_VERSION as _ , + }) + } +}drivers/android/process.rs new file +0
-0drivers/android/rust_binder.rs +0
-0drivers/android/rust_binderfs.c new file +0
-0include/uapi/linux/magic.h +0
-0rust/bindings/bindings_helper.h +0
-0rust/kernel/lib.rs +0
-0scripts/Makefile.build +0
-0Signature · 1 line
2.42.0.820.g83a721a137-goog
Alice Ryhl
maintainerRe: [PATCH RFC 02/20] rust_binder: add binderfs support to Rust binder
Quoted 8 lines · Finn Behrens
drivers/android/defs.rs line 14
Quoted 6 lines · Alice Ryhl
drivers/android/defs.rs new file +6
-0+macro_rules! decl_wrapper { + ($newname:ident, $wrapped:ty) => { +#[derive( Copy , Clone , Default )] +#[repr(transparent)] +pub (crate )struct $newname($wrapped); +// SAFETY: This macro is only used with types where this is ok.
Quoted 5 lines
Would it make sense so also annotade this safety requirement on the macro itself?
It is only file private, but could help not overlook it, when using for something new in the same file.
Sure, I can move the comment.
Alice
Re: [PATCH RFC 02/20] rust_binder: add binderfs support to Rust binder
In reply to Alice RyhlQuoted 11 lines · Alice Ryhl
drivers/android/context.rs new file +11
-0+/// There is one context per binder file (/dev/binder, /dev/hwbinder, etc) +#[pin_data] +pub (crate )struct Context { +#[pin] + manager:Mutex <Manager >, +pub (crate ) name:CString , +#[pin] + links:ListLinks , +} + +kernel::list::impl_has_list_links! {
drivers/android/context.rs line 96
Quoted 10 lines
drivers/android/context.rs new file +10
-0+ impl HasListLinks<0> for Context { self.links } +} +kernel::list::impl_list_arc_safe! { +impl ListArcSafe <0 >for Context { untracked; } +} +kernel::list::impl_list_item! { +impl ListItem <0 >for Context { + usingListLinks ; + } +}
I think at some point it would be worth introducing a derive macro that does this for us. So for example:
#[pin_data]
#[derive(HasListLinks)]
pub(crate) struct Context {
#[pin]
manager: Mutex<Manager>,
pub(crate) name: CString,
#[pin]
#[links]
links: ListLinks,
}And if you need multiple links you could do:
#[pin_data]
#[derive(HasListLinks)]
struct Foo {
#[links = 0]
a_list: ListLinks,
#[links = 1]
b_list: ListLinks,
}Same for ListItem and HasWork. I have not yet taken a look at your linked list implementation, so I don't know if this is possible (since ListItem seems to have multiple "backends").
I think this improvement can wait though, just wanted to mention it.
Signature · 2 lines
Cheers, Benno
Re: [PATCH RFC 02/20] rust_binder: add binderfs support to Rust binder
Quoted 17 lines · Benno Lossin
Quoted 16 lines · Alice Ryhl
drivers/android/context.rs new file +16
-0+/// There is one context per binder file (/dev/binder, /dev/hwbinder, etc) +#[pin_data] +pub (crate )struct Context { +#[pin] + manager:Mutex <Manager >, +pub (crate ) name:CString , +#[pin] + links:ListLinks , +} + +kernel::list::impl_has_list_links! { +impl HasListLinks <0 >for Context { self.links } +} +kernel::list::impl_list_arc_safe! { +impl ListArcSafe <0 >for Context { untracked; } +}
Quoted 8 lines
drivers/android/context.rs line 96
Quoted 5 lines
drivers/android/context.rs new file +5
-0+kernel::list::impl_list_item! { +impl ListItem <0 >for Context { + usingListLinks ; + } +}I think at some point it would be worth introducing a derive macro that does this for us. So for example:
Agreed.
Quoted 12 lines
#[pin_data]
#[derive(HasListLinks)]
pub(crate) struct Context {
#[pin]
manager: Mutex<Manager>,
pub(crate) name: CString,
#[pin]
#[links]
links: ListLinks,
}Quoted 7 lines
And if you need multiple links you could do:
#[pin_data] #[derive(HasListLinks)] struct Foo { #[links = 0] a_list: ListLinks,
we will need more discussion on how the derive syntax would look like, but I'd expect we can reference the field with names instead of numbers if we use derive macros. In other words type numbering to distinguish different fields should be an implementation detail.
Regards,
Boqun
Quoted 14 lines
#[links = 1] b_list: ListLinks, }Same for
ListItemandHasWork. I have not yet taken a look at your linked list implementation, so I don't know if this is possible (sinceListItemseems to have multiple "backends").I think this improvement can wait though, just wanted to mention it.
Signature · 2 lines
Cheers, Benno
Alice Ryhl
maintainerRe: [PATCH RFC 02/20] rust_binder: add binderfs support to Rust binder
In reply to Benno LossinQuoted 26 lines · Benno Lossin
Quoted 11 lines · Alice Ryhl
drivers/android/context.rs new file +11
-0+/// There is one context per binder file (/dev/binder, /dev/hwbinder, etc) +#[pin_data] +pub (crate )struct Context { +#[pin] + manager:Mutex <Manager >, +pub (crate ) name:CString , +#[pin] + links:ListLinks , +} + +kernel::list::impl_has_list_links! {drivers/android/context.rs line 96
Quoted 10 lines
drivers/android/context.rs new file +10
-0+ impl HasListLinks<0> for Context { self.links } +} +kernel::list::impl_list_arc_safe! { +impl ListArcSafe <0 >for Context { untracked; } +} +kernel::list::impl_list_item! { +impl ListItem <0 >for Context { + usingListLinks ; + } +}I think at some point it would be worth introducing a derive macro that does this for us. So for example:
Quoted 10 lines
#[pin_data]
#[derive(HasListLinks)]
pub(crate) struct Context {
#[pin]
manager: Mutex<Manager>,
pub(crate) name: CString,
#[pin]
#[links]
links: ListLinks,
}Sure, it would be nice to improve the ergonomics of this. However, I don't think it's that important either. The current solution is a bit verbose, but good enough for me.
Alice
Alice Ryhl
maintainerRFC PATCH 3/20 rust_binder: add threading support
In reply to Alice RyhlThe binder driver needs to keep track of the threads that a process uses with the driver for several reasons:
1. When replying to a transaction, it is assumed that you are replying to the "currently active transaction" on the thread you made the syscall from. The syscall does not provide any way to specify which transaction you are replying to.
2. When a thread is sleeping while waiting for incoming transactions, the driver needs to keep track of where it can deliver a transaction to.
3. The BINDER_GET_EXTENDED_ERROR ioctl gives you the last error triggered by a syscall on the same thread, so it needs to keep track of this value for each thread.
4. For binder servers, the driver keeps track of whether a process has enough threads in its transaction thread pool.
Note that not all of the above items are implemented yet. Some of them will appear in later patches.
In this patch, we add the structures to keep track of the threads and implement item 3 and 4 in the above list.
--- drivers/android/defs.rs | 36 ++++++- drivers/android/error.rs | 52 +++++++++++ drivers/android/process.rs | 108 +++++++++++++++++++-- drivers/android/rust_binder.rs | 2 + drivers/android/thread.rs | 206 +++++++++++++++++++++++++++++++++++++++++ scripts/Makefile.build | 2 +- 6 files changed, 396 insertions(+), 10 deletions(-)
drivers/android/defs.rs +35 -1
@@ -2,24 +2,50 @@use core::ops::{Deref ,DerefMut };use kernel::{ - bindings, + bindings::{self, *}, io_buffer::{ReadableFromBytes ,WritableToBytes }, }; +macro_rules! pub_no_prefix { + ($prefix:ident, $($newname:ident),+) => { + $(pub(crate) const $newname: u32 = kernel::macros::concat_idents!($prefix, $newname);)+ + }; +} + +pub_no_prefix! ( + binder_driver_return_protocol_, +BR_DEAD_REPLY , +BR_FAILED_REPLY , +BR_NOOP , +BR_SPAWN_LOOPER , +BR_TRANSACTION_COMPLETE , +BR_OK +); + +pub_no_prefix! ( + binder_driver_command_protocol_, +BC_ENTER_LOOPER , +BC_EXIT_LOOPER , +BC_REGISTER_LOOPER +); +macro_rules! decl_wrapper { ($newname:ident, $wrapped:ty) => {#[derive( Copy , Clone , Default )] #[repr(transparent)] pub (crate )struct $newname($wrapped); +// SAFETY: This macro is only used with types where this is ok. unsafe impl ReadableFromBytes for $newname {}unsafe impl WritableToBytes for $newname {} +impl Deref for $newname {type Target = $wrapped;fn deref (&self) -> &Self ::Target { &self.0 } } +impl DerefMut for $newname {fn deref_mut (&mut self) -> &mut Self ::Target { &mut self.0
@@ -28,7 +54,9 @@ fn deref_mut(&mut self) -> &mut Self::Target { }; } +decl_wrapper! (BinderWriteRead , bindings::binder_write_read);decl_wrapper! (BinderVersion , bindings::binder_version); +decl_wrapper! (ExtendedError , bindings::binder_extended_error);impl BinderVersion {pub (crate )fn current () ->Self {
@@ -37,3 +65,9 @@ pub(crate) fn current() -> Self { }) } } + +impl ExtendedError { +pub (crate )fn new (id:u32 , command:u32 , param:i32 ) ->Self { +Self (bindings::binder_extended_error { id, command, param }) + } +}
drivers/android/error.rs new file +52 -0
52 lines not on this page · raw message
drivers/android/process.rs +100 -8
171 lines not on this page · raw message
drivers/android/rust_binder.rs +2 -0
@@ -14,7 +14,9 @@mod context;mod defs; +mod error;mod process; +mod thread;module! {type :BinderModule ,
drivers/android/thread.rs new file +206 -0
206 lines not on this page · raw message
scripts/Makefile.build +1 -1
@@ -262,7 +262,7 @@ $(obj)/%.lst: $(src)/%.c FORCE # Compile Rust sources (.rs) # --------------------------------------------------------------------------- -rust_allowed_features := const_maybe_uninit_zeroed,new_uninit,offset_of +rust_allowed_features := const_maybe_uninit_zeroed,new_uninit,offset_of,allocator_api # `--out-dir` is required to avoid temporaries being created by `rustc` in the # current working directory, which may be not accessible in the out-of-tree
Signature · 1 line
2.42.0.820.g83a721a137-goog
Re: [PATCH RFC 03/20] rust_binder: add threading support
Quoted 15 lines · Alice Ryhl
drivers/android/error.rs new file +10
-0+ +impl core::fmt::Debug for BinderError { +fn fmt (&self, f: &mut core::fmt::Formatter <'_>) -> core::fmt::Result { +match self.reply { +BR_FAILED_REPLY =>match self.source.as_ref () { +Some (source) => f + .debug_struct ("BR_FAILED_REPLY" ) + .field ("source" , source) + .finish (), +None => f.pad ("BR_FAILED_REPLY" ),
drivers/android/error.rs line 52
Quoted 10 lines
drivers/android/error.rs new file +10
-0+ }, + BR_DEAD_REPLY => f.pad("BR_DEAD_REPLY"), + BR_TRANSACTION_COMPLETE => f.pad("BR_TRANSACTION_COMPLETE"), + _ => f + .debug_struct ("BinderError" ) + .field ("reply" , &self.reply) + .finish(), + } + } +}
Renaming the debug_struct itself feels like it will make it harder to find later, as I would expect that a debug implementation names the struct its from. Also this has the fallback in CamelCase and all defined cases as SCREAMING_SNAKE_CASE. Maybe rather in the defined cases something like f.debug_struct(‘BinderError’).field(‘reply’, “name”)?
Alice Ryhl
maintainerRe: [PATCH RFC 03/20] rust_binder: add threading support
Quoted 31 lines · Finn Behrens
Quoted 15 lines · Alice Ryhl
drivers/android/error.rs new file +10
-0+ +impl core::fmt::Debug for BinderError { +fn fmt (&self, f: &mut core::fmt::Formatter <'_>) -> core::fmt::Result { +match self.reply { +BR_FAILED_REPLY =>match self.source.as_ref () { +Some (source) => f + .debug_struct ("BR_FAILED_REPLY" ) + .field ("source" , source) + .finish (), +None => f.pad ("BR_FAILED_REPLY" ),drivers/android/error.rs line 52
Quoted 10 lines
drivers/android/error.rs new file +10
-0+ }, + BR_DEAD_REPLY => f.pad("BR_DEAD_REPLY"), + BR_TRANSACTION_COMPLETE => f.pad("BR_TRANSACTION_COMPLETE"), + _ => f + .debug_struct ("BinderError" ) + .field ("reply" , &self.reply) + .finish(), + } + } +}Renaming the
debug_structitself feels like it will make it harder to find later, as I would expect that a debug implementation names the struct its from.
Quoted 3 lines
Also this has the fallback in CamelCase and all defined cases as
SCREAMING_SNAKE_CASE. Maybe rather in the defined cases something like f.debug_struct(‘BinderError’).field(‘reply’, “name”)?
Yeah, you're right. I'll improve the debug formatting. Thanks for the suggestion.
Alice
Alice Ryhl
maintainerRFC PATCH 4/20 rust_binder: add work lists
In reply to Alice RyhlThe binder driver uses linked lists of work items to store events that need to be delivered to userspace. There are work lists on both the process and threads.
Work items are expected to implement the DeliverToRead trait, whose name signifies that this type is something that can be delivered to userspace via the read part of the BINDER_WRITE_READ ioctl. The trait defines what happens when a work item is executed, when it is cancelled, how the thread should be notified (wake_up_interruptible_sync or wake_up_interruptible?), and how it can be enqueued to a linked list. For each type that implements the trait, Rust will generate a vtable for the type. Pointers to the dyn DeliverToRead type will be fat pointers where the metadata of the pointer is a pointer to the vtable.
We introduce the concept of a "ready thread". This is a thread that is currently waiting for work items inside the get_work method. The process will keep track of them and deliver new work items to one of the ready threads directly. When there are no ready threads, work items are stored in the process work list.
The work lists added in this patch are not used yet, so the push_work methods are marked with #[allow(dead_code)] to silence the warnings about unused methods. A user is added in the next patch of this patch set.
--- drivers/android/error.rs | 9 ++ drivers/android/process.rs | 126 ++++++++++++++++-- drivers/android/rust_binder.rs | 87 ++++++++++++- drivers/android/thread.rs | 284 +++++++++++++++++++++++++++++++++++++++-- scripts/Makefile.build | 2 +- 5 files changed, 488 insertions(+), 20 deletions(-)
drivers/android/error.rs +9 -0
@@ -11,6 +11,15 @@ pub(crate) struct BinderError { source: Option<Error>, } +impl BinderError { +pub (crate )fn new_dead () ->Self { +Self { + reply:BR_DEAD_REPLY , + source:None , + } + } +} +/// Convert an errno into a `BinderError` and store the errno used to construct it. The errno /// should be stored as the thread's extended error when given to userspace. impl From <Error >for BinderError {
drivers/android/process.rs +118 -8
186 lines not on this page · raw message
drivers/android/rust_binder.rs +86 -1
104 lines not on this page · raw message
drivers/android/thread.rs +274 -10
379 lines not on this page · raw message
scripts/Makefile.build +1 -1
@@ -262,7 +262,7 @@ $(obj)/%.lst: $(src)/%.c FORCE # Compile Rust sources (.rs) # --------------------------------------------------------------------------- -rust_allowed_features := const_maybe_uninit_zeroed,new_uninit,offset_of,allocator_api +rust_allowed_features := const_maybe_uninit_zeroed,new_uninit,offset_of,allocator_api,receiver_trait # `--out-dir` is required to avoid temporaries being created by `rustc` in the # current working directory, which may be not accessible in the out-of-tree
Signature · 1 line
2.42.0.820.g83a721a137-goog
Alice Ryhl
maintainerRFC PATCH 5/20 rust_binder: add nodes and context managers
In reply to Alice RyhlAn important concept for the binder driver is a "node", which is a type of object defined by the binder driver that serves as a "binder server". Whenever you send a transaction, the recipient will be a node.
Binder nodes can exist in many processes. The driver keeps track of this using two fields in Process.
* The nodes rbtree. This structure stores all nodes that this process is the primary owner of. The process field of the Node struct will point at the process that has it in its nodes rbtree.
* The node_refs collection. This keeps track of the nodes from other processes that this process holds a reference to. A process can only send transactions to nodes in this collection.
From userspace, we also make a distinction between local nodes owned by the process itself, and proxy nodes that are owned by a different process. Generally, a process will refer to local nodes using the address of the corresponding userspace object, and it will refer to proxy nodes using a 32-bit id that the kernel assigns to the node. The 32-bit ids are local to each process and assigned consecutively (the same node can have a different 32-bit id in each external process that has a reference to it).
Additionally, a node can also be stored in the context as the "context manager". There will only be one context manager for each context (that is, for each file in /dev/binderfs). The context manager implicitly has the id 0 in every other process, which means that all processes are able to access it by default.
In a later patch, we will add the ability to send nodes from one process to another as part of a transaction. When this happens, the node is added to the node_refs collection of the target process, and the process will be able to start using it from then on. Except for the context manager node, sending nodes in this way is the *only* way for a process to obtain a reference to a node defined by another process. Generally, userspace processes are expected to send their nodes to the context manager process so that the context manager can pass it on to clients that want to connect to it.
Binder nodes are reference counted through the kernel. This generally happens in the following manner:
1. Process A owns a binder node, which it stores in an allocation in userspace. This allocation is reference counted.
2. The kernel owns a Node object that holds a reference count to the userspace object in process A. Changes to this reference count are communicated to process A using the commands BR_ACQUIRE, BR_RELEASE, BR_INCREFS, and BR_DECREFS.
3. Other parts of the kernel own a NodeRef object that holds a reference count to the Node object. Destroying a NodeRef will decrement the refcount of the associated Node in the appropriate way.
4. Process B owns a proxy node, which is a userspace object. Using a 32-bit id, this proxy node refers to a NodeRef object in the kernel. When the proxy node is destroyed, userspace will use the commands BC_ACQUIRE, BC_RELEASE, BC_INCREFS, and BC_DECREFS to tell the kernel to modify the refcount on the NodeRef object.
Via the above chain, process B can own a refcount that keeps a node in process A alive.
There can also be other things than processes than own a NodeRef. For example, the context holds a NodeRef to the context manager node. This keeps the node alive, even if there are no other processes with a reference to it. In a later patch, we will see other instances of this - for example, a transaction's allocation will also own a NodeRef to any nodes embedded in it so that they don't go away while the process is handling the transaction.
There is a potential race condition where the kernel sends BR_ACQUIRE immediately followed by BR_RELEASE. If these are delivered to two different userspace threads, then userspace might see them in reverse order, which could make the refcount drop to zero when it shouldn't. To prevent this from happening, userspace will respond to BR_ACQUIRE commands with a BC_ACQUIRE_DONE after incrementing the refcount. The kernel will postpone BR_RELEASE commands until after userspace has responded with BC_ACQUIRE_DONE, which ensures that this race cannot happen.
--- drivers/android/context.rs | 44 ++++- drivers/android/defs.rs | 17 +- drivers/android/node.rs | 377 +++++++++++++++++++++++++++++++++++++++++ drivers/android/process.rs | 343 ++++++++++++++++++++++++++++++++++++- drivers/android/rust_binder.rs | 2 +- drivers/android/thread.rs | 13 +- rust/helpers.c | 6 + rust/kernel/security.rs | 8 + 8 files changed, 799 insertions(+), 11 deletions(-)
drivers/android/context.rs +43 -1
@@ -3,11 +3,13 @@use kernel::{ list::{HasListLinks ,List ,ListArc ,ListArcSafe ,ListItem ,ListLinks }, prelude::*, + security, str::{CStr ,CString }, sync::{Arc ,Mutex }, + task::Kuid , }; -use crate ::process::Process ; +use crate ::{error::BinderError , node::NodeRef , process::Process };// This module defines the global variable containing the list of contexts. Since the // `kernel::sync` bindings currently don't support mutexes in globals, we use a temporary
@@ -70,6 +72,8 @@ pub(crate) struct ContextList {/// This struct keeps track of the processes using this context, and which process is the context /// manager. struct Manager { + node:Option <NodeRef >, + uid:Option <Kuid >, all_procs:List <Process >, }
@@ -103,6 +107,8 @@ pub(crate) fn new(name: &CStr) -> Result<Arc<Self>> { links <- ListLinks::new(), manager <- kernel::new_mutex!(Manager { all_procs:List ::new (), + node:None , + uid:None , }, "Context::manager"), }))?;
@@ -141,4 +147,40 @@ pub(crate) fn deregister_process(self: &Arc<Self>, proc: &Process) { self.manager.lock ().all_procs.remove (proc); } } + +pub (crate )fn set_manager_node (&self, node_ref:NodeRef ) ->Result { +let mut manager = self.manager.lock (); +if manager.node.is_some () { +pr_warn! ("BINDER_SET_CONTEXT_MGR already set" ); +return Err (EBUSY ); + } + security::binder_set_context_mgr (&node_ref.node.owner.cred)?; + +// If the context manager has been set before, ensure that we use the same euid. +let caller_uid =Kuid ::current_euid (); +if let Some (ref uid) = manager.uid { +if *uid != caller_uid { +return Err (EPERM ); + } + } + + manager.node =Some (node_ref); + manager.uid =Some (caller_uid); +Ok (()) + } + +pub (crate )fn unset_manager_node (&self) { +let node_ref = self.manager.lock ().node.take (); +drop (node_ref); + } + +pub (crate )fn get_manager_node (&self, strong:bool ) ->Result <NodeRef ,BinderError > { + self.manager + .lock () + .node + .as_ref () + .ok_or_else (BinderError ::new_dead)? + .clone (strong) + .map_err (BinderError ::from) + } }
drivers/android/defs.rs +15 -2
@@ -19,14 +19,24 @@ macro_rules! pub_no_prefix { BR_NOOP, BR_SPAWN_LOOPER, BR_TRANSACTION_COMPLETE, -BR_OK + BR_OK, + BR_INCREFS, + BR_ACQUIRE, + BR_RELEASE, +BR_DECREFS );pub_no_prefix! ( binder_driver_command_protocol_,BC_ENTER_LOOPER ,BC_EXIT_LOOPER , -BC_REGISTER_LOOPER +BC_REGISTER_LOOPER , +BC_INCREFS , +BC_ACQUIRE , +BC_RELEASE , +BC_DECREFS , +BC_INCREFS_DONE , +BC_ACQUIRE_DONE );macro_rules! decl_wrapper {
@@ -54,6 +64,9 @@ fn deref_mut(&mut self) -> &mut Self::Target { }; } +decl_wrapper! (BinderNodeDebugInfo , bindings::binder_node_debug_info); +decl_wrapper! (BinderNodeInfoForRef , bindings::binder_node_info_for_ref); +decl_wrapper! (FlatBinderObject , bindings::flat_binder_object);decl_wrapper! (BinderWriteRead , bindings::binder_write_read);decl_wrapper! (BinderVersion , bindings::binder_version);decl_wrapper! (ExtendedError , bindings::binder_extended_error);
drivers/android/node.rs new file +377 -0
377 lines not on this page · raw message
drivers/android/process.rs +339 -4
434 lines not on this page · raw message
drivers/android/rust_binder.rs +1 -1
@@ -19,6 +19,7 @@mod context;mod defs;mod error; +mod node;mod process;mod thread;
@@ -102,7 +103,6 @@ fn arc_try_new(val: T) -> Result<DLArc<T>, alloc::alloc::AllocError> { .map_err(|_| alloc::alloc::AllocError) } -#[allow(dead_code)] fn arc_pin_init (init:impl PinInit <T >) ->Result <DLArc <T >, kernel::error::Error > { ListArc::pin_init(pin_init!(Self { links <- ListLinksSelfPtr::new(),
drivers/android/thread.rs +10 -3
@@ -93,7 +93,6 @@ fn pop_work(&mut self) -> Option<DLArc<dyn DeliverToRead>> { ret } -#[allow(dead_code)] fn push_work (&mut self, work:DLArc <dyn DeliverToRead >) ->PushWorkRes {if self.is_dead {PushWorkRes ::FailedDead (work)
@@ -106,7 +105,6 @@ fn push_work(&mut self, work: DLArc<dyn DeliverToRead>) -> PushWorkRes {/// Used to push work items that do not need to be processed immediately and can wait until the /// thread gets another work item. -#[allow(dead_code)] fn push_work_deferred (&mut self, work:DLArc <dyn DeliverToRead >) { self.work_list.push_back (work); }
@@ -294,7 +292,6 @@ fn get_work(self: &Arc<Self>, wait: bool) -> Result<Option<DLArc<dyn DeliverToRe/// /// Returns whether the item was successfully pushed. This can only fail if the work item is /// already in a work list. -#[allow(dead_code)] pub (crate )fn push_work (&self, work:DLArc <dyn DeliverToRead >) ->PushWorkRes {let sync = work.should_sync_wakeup ();
@@ -311,6 +308,10 @@ pub(crate) fn push_work(&self, work: DLArc<dyn DeliverToRead>) -> PushWorkRes { res } +pub (crate )fn push_work_deferred (&self, work:DLArc <dyn DeliverToRead >) { + self.inner.lock ().push_work_deferred (work); + } +fn write (self: &Arc <Self >, req: &mut BinderWriteRead ) ->Result {let write_start = req.write_buffer.wrapping_add (req.write_consumed);let write_len = req.write_size - req.write_consumed;
@@ -320,6 +321,12 @@ fn write(self: &Arc<Self>, req: &mut BinderWriteRead) -> Result {let before = reader.len ();let cmd = reader.read ::<u32 >()?;match cmd { +BC_INCREFS => self.process.update_ref (reader.read ()?,true ,false )?, +BC_ACQUIRE => self.process.update_ref (reader.read ()?,true ,true )?, +BC_RELEASE => self.process.update_ref (reader.read ()?,false ,true )?, +BC_DECREFS => self.process.update_ref (reader.read ()?,false ,false )?, +BC_INCREFS_DONE => self.process.inc_ref_done (&mut reader,false )?, +BC_ACQUIRE_DONE => self.process.inc_ref_done (&mut reader,true )?,BC_REGISTER_LOOPER => {let valid = self.process.register_thread (); self.inner.lock ().looper_register (valid);
rust/helpers.c +6 -0
@@ -329,6 +329,12 @@ void rust_helper_security_release_secctx(char *secdata, u32 seclen) security_release_secctx(secdata, seclen); }EXPORT_SYMBOL_GPL (rust_helper_security_release_secctx); + +int rust_helper_security_binder_set_context_mgr (const struct cred *mgr) +{ +return security_binder_set_context_mgr (mgr); +} +EXPORT_SYMBOL_GPL (rust_helper_security_binder_set_context_mgr);#endif /*
rust/kernel/security.rs +8 -0
@@ -6,9 +6,17 @@use crate ::{ bindings, + cred::Credential , error::{to_result,Result }, }; +/// Calls the security modules to determine if the given task can become the manager of a binder +/// context. +pub fn binder_set_context_mgr (mgr: &Credential ) ->Result { +// SAFETY: `mrg.0` is valid because the shared reference guarantees a nonzero refcount. +to_result (unsafe { bindings::security_binder_set_context_mgr (mgr.0 .get ()) }) +} +/// A security context string. /// /// The struct has the invariant that it always contains a valid security context.
Signature · 1 line
2.42.0.820.g83a721a137-goog
Alice Ryhl
maintainerRFC PATCH 6/20 rust_binder: add oneway transactions
In reply to Alice RyhlAdd support for sending oneway transactions using the binder driver.
To receive transactions, the process must first use mmap to create a memory region for holding the contents of incoming transactions. The driver will manage the resulting memory using two files: allocation.rs and range_alloc.rs.
The allocation.rs file is responsible for actually managing the mmap'ed region of memory and has methods for writing to it.
The range_alloc.rs file contains a data structure for tracking where in the mmap we are storing different things. It doesn't actually touch the mmap itself. Basically, it's a data structure that stores a set of non-overlapping intervals (the allocations) and it is able to find the smallest offset where the next X bytes are free and allocate that region.
Other than that, this patch introduces a Transaction struct that stores the information related to a transaction, and adds the necessary infrastructure to send and receive them. This uses the work lists introduces in a previous patch to deliver incoming transactions.
There are several different possible implementations of the range allocator, and we have implemented several of them. The simplest possible implementation is to use a linked list to store the allocations and free regions sorted by address. Another possibility is to store the same thing using a red-black tree. The red-black tree is preferable to the linked list because its accesses are logarithmic rather than linear.
This RFC implements the range allocator using a red-black tree.
We have also looked into replacing the red-black tree with an XArray. However, this is challenging because it doesn't have a good way to look up the smallest free region whose size is at least some lower bound. You can use xa_find, but there could be many free regions of the same size, which makes it a challenge to maintain this information correctly. We also run into issues with having to allocate while holding a lock. Finally, the XArray is not optimized for this use-case: all of the indices are going to have gaps between them.
--- drivers/android/allocation.rs | 140 +++++++++++++++++ drivers/android/defs.rs | 39 +++++ drivers/android/error.rs | 10 ++ drivers/android/node.rs | 1 - drivers/android/process.rs | 171 ++++++++++++++++++-- drivers/android/range_alloc.rs | 344 +++++++++++++++++++++++++++++++++++++++++ drivers/android/rust_binder.rs | 54 +++++++ drivers/android/thread.rs | 208 +++++++++++++++++++++++-- drivers/android/transaction.rs | 163 +++++++++++++++++++ rust/helpers.c | 7 + rust/kernel/security.rs | 7 + 11 files changed, 1123 insertions(+), 21 deletions(-)
drivers/android/allocation.rs new file +140 -0
@@ -0,0 +1,140 @@ +// SPDX-License-Identifier: GPL-2.0 +use core::mem::size_of_val; + +use kernel::{bindings, pages::Pages , prelude::*, sync::Arc , user_ptr::UserSlicePtrReader }; + +use crate ::{node::NodeRef , process::Process }; + +#[derive( Default )] +pub (crate )struct AllocationInfo { +/// The target node of the transaction this allocation is associated to. +/// Not set for replies. +pub (crate ) target_node:Option <NodeRef >, +/// Zero the data in the buffer on free. +pub (crate ) clear_on_free:bool , +} + +/// Represents an allocation that the kernel is currently using. +/// +/// When allocations are idle, the range allocator holds the data related to them. +pub (crate )struct Allocation { +pub (crate ) offset:usize , + size:usize , +pub (crate ) ptr:usize , + pages:Arc <Vec <Pages <0 >>>, +pub (crate ) process:Arc <Process >, + allocation_info:Option <AllocationInfo >, + free_on_drop:bool , +} + +impl Allocation { +pub (crate )fn new ( + process:Arc <Process >, + offset:usize , + size:usize , + ptr:usize , + pages:Arc <Vec <Pages <0 >>>, + ) ->Self { +Self { + process, + offset, + size, + ptr, + pages, + allocation_info:None , + free_on_drop:true , + } + } + +fn iterate <T >(&self,mut offset:usize ,mut size:usize ,mut cb:T ) ->Result +where +T :FnMut (&Pages <0 >,usize ,usize ) ->Result , + { +// Check that the request is within the buffer. +if offset.checked_add (size).ok_or (EINVAL )? > self.size { +return Err (EINVAL ); + } + offset += self.offset; +let mut page_index = offset >> bindings::PAGE_SHIFT ; + offset &= (1 << bindings::PAGE_SHIFT ) -1 ; +while size >0 { +let available = core::cmp::min (size, (1 << bindings::PAGE_SHIFT ) - offset); +cb (&self.pages[page_index], offset, available)?; + size -= available; + page_index +=1 ; + offset =0 ; + } +Ok (()) + } + +pub (crate )fn copy_into ( + &self, + reader: &mut UserSlicePtrReader , + offset:usize , + size:usize , + ) ->Result { + self.iterate (offset, size, |page, offset, to_copy| { + page.copy_into_page (reader, offset, to_copy) + }) + } + +pub (crate )fn write <T : ?Sized >(&self, offset:usize , obj: &T ) ->Result { +let mut obj_offset =0 ; + self.iterate (offset,size_of_val (obj), |page, offset, to_copy| { +// SAFETY: The sum of `offset` and `to_copy` is bounded by the size of T. +let obj_ptr =unsafe { (objas *const T as *const u8 ).add (obj_offset) }; +// SAFETY: We have a reference to the object, so the pointer is valid. +unsafe { page.write (obj_ptr, offset, to_copy) }?; + obj_offset += to_copy; +Ok (()) + }) + } + +pub (crate )fn fill_zero (&self) ->Result { + self.iterate (0 , self.size, |page, offset, len| { + page.fill_zero (offset, len) + }) + } + +pub (crate )fn keep_alive (mut self) { + self.process + .buffer_make_freeable (self.offset, self.allocation_info.take ()); + self.free_on_drop =false ; + } + +pub (crate )fn set_info (&mut self, info:AllocationInfo ) { + self.allocation_info =Some (info); + } + +pub (crate )fn get_or_init_info (&mut self) -> &mut AllocationInfo { + self.allocation_info.get_or_insert_with (Default ::default) + } + +pub (crate )fn set_info_clear_on_drop (&mut self) { + self.get_or_init_info ().clear_on_free =true ; + } + +pub (crate )fn set_info_target_node (&mut self, target_node:NodeRef ) { + self.get_or_init_info ().target_node =Some (target_node); + } +} + +impl Drop for Allocation { +fn drop (&mut self) { +if !self.free_on_drop { +return ; + } + +if let Some (mut info) = self.allocation_info.take () { + info.target_node =None ; + +if info.clear_on_free { +if let Err (e) = self.fill_zero () { +pr_warn! ("Failed to clear data on free: {:?}" , e); + } + } + } + + self.process.buffer_raw_free (self.ptr); + } +}
drivers/android/defs.rs +39 -0
@@ -14,6 +14,9 @@ macro_rules! pub_no_prefix {pub_no_prefix! ( binder_driver_return_protocol_, +BR_TRANSACTION , +BR_TRANSACTION_SEC_CTX , +BR_REPLY ,BR_DEAD_REPLY ,BR_FAILED_REPLY ,BR_NOOP ,
@@ -28,6 +31,9 @@ macro_rules! pub_no_prefix {pub_no_prefix! ( binder_driver_command_protocol_, +BC_TRANSACTION , +BC_TRANSACTION_SG , +BC_FREE_BUFFER ,BC_ENTER_LOOPER ,BC_EXIT_LOOPER ,BC_REGISTER_LOOPER ,
@@ -39,6 +45,10 @@ macro_rules! pub_no_prefix {BC_ACQUIRE_DONE ); +pub (crate )const FLAT_BINDER_FLAG_TXN_SECURITY_CTX :u32 = + kernel::bindings::FLAT_BINDER_FLAG_TXN_SECURITY_CTX ; +pub_no_prefix! (transaction_flags_,TF_ONE_WAY ,TF_CLEAR_BUF ); +macro_rules! decl_wrapper { ($newname:ident, $wrapped:ty) => {#[derive( Copy , Clone , Default )]
@@ -67,6 +77,15 @@ fn deref_mut(&mut self) -> &mut Self::Target {decl_wrapper! (BinderNodeDebugInfo , bindings::binder_node_debug_info);decl_wrapper! (BinderNodeInfoForRef , bindings::binder_node_info_for_ref);decl_wrapper! (FlatBinderObject , bindings::flat_binder_object); +decl_wrapper! (BinderTransactionData , bindings::binder_transaction_data); +decl_wrapper! ( +BinderTransactionDataSecctx , + bindings::binder_transaction_data_secctx +); +decl_wrapper! ( +BinderTransactionDataSg , + bindings::binder_transaction_data_sg +);decl_wrapper! (BinderWriteRead , bindings::binder_write_read);decl_wrapper! (BinderVersion , bindings::binder_version);decl_wrapper! (ExtendedError , bindings::binder_extended_error);
@@ -79,6 +98,26 @@ pub(crate) fn current() -> Self { } } +impl BinderTransactionData { +pub (crate )fn with_buffers_size (self, buffers_size:u64 ) ->BinderTransactionDataSg { +BinderTransactionDataSg (bindings::binder_transaction_data_sg { + transaction_data: self.0 , + buffers_size, + }) + } +} + +impl BinderTransactionDataSecctx { +/// View the inner data as wrapped in `BinderTransactionData`. +pub (crate )fn tr_data (&mut self) -> &mut BinderTransactionData { +// SAFETY: Transparent wrapper is safe to transmute. +unsafe { + &mut *(&mut self.transaction_dataas *mut bindings::binder_transaction_data +as *mut BinderTransactionData ) + } + } +} +impl ExtendedError {pub (crate )fn new (id:u32 , command:u32 , param:i32 ) ->Self {Self (bindings::binder_extended_error { id, command, param })
drivers/android/error.rs +10 -0
@@ -4,6 +4,8 @@use crate ::defs::*; +pub (crate )type BinderResult <T = ()> = core::result::Result <T ,BinderError >; +/// An error that will be returned to userspace via the `BINDER_WRITE_READ` ioctl rather than via /// errno. pub (crate )struct BinderError {
@@ -18,6 +20,14 @@ pub(crate) fn new_dead() -> Self { source: None, } } + +pub (crate )fn is_dead (&self) ->bool { + self.reply ==BR_DEAD_REPLY + } + +pub (crate )fn as_errno (&self) -> core::ffi::c_int { + self.source.unwrap_or (EINVAL ).to_errno () + } }/// Convert an errno into a `BinderError` and store the errno used to construct it. The errno
drivers/android/node.rs +0 -1
@@ -49,7 +49,6 @@ pub(crate) struct Node { pub(crate) global_id: u64, ptr: usize, cookie: usize, -#[allow(dead_code)] pub(crate) flags: u32, pub(crate) owner: Arc<Process>, inner: LockedBy<NodeInner, ProcessInner>,
drivers/android/process.rs +162 -9
236 lines not on this page · raw message
drivers/android/range_alloc.rs new file +344 -0
344 lines not on this page · raw message
drivers/android/rust_binder.rs +54 -0
78 lines not on this page · raw message
drivers/android/thread.rs +197 -11
276 lines not on this page · raw message
drivers/android/transaction.rs new file +163 -0
163 lines not on this page · raw message
rust/helpers.c +7 -0
@@ -335,6 +335,13 @@ int rust_helper_security_binder_set_context_mgr(const struct cred *mgr) return security_binder_set_context_mgr(mgr); }EXPORT_SYMBOL_GPL (rust_helper_security_binder_set_context_mgr); + +int rust_helper_security_binder_transaction (const struct cred *from, +const struct cred *to) +{ +return security_binder_transaction (from, to); +} +EXPORT_SYMBOL_GPL (rust_helper_security_binder_transaction);#endif /*
rust/kernel/security.rs +7 -0
@@ -17,6 +17,13 @@ pub fn binder_set_context_mgr(mgr: &Credential) -> Result {to_result (unsafe { bindings::security_binder_set_context_mgr (mgr.0 .get ()) }) } +/// Calls the security modules to determine if binder transactions are allowed from task `from` to +/// task `to`. +pub fn binder_transaction (from: &Credential , to: &Credential ) ->Result { +// SAFETY: `from` and `to` are valid because the shared references guarantee nonzero refcounts. +to_result (unsafe { bindings::security_binder_transaction (from.0 .get (), to.0 .get ()) }) +} +/// A security context string. /// /// The struct has the invariant that it always contains a valid security context.
Signature · 1 line
2.42.0.820.g83a721a137-goog
Alice Ryhl
maintainerRFC PATCH 7/20 rust_binder: add epoll support
In reply to Alice RyhlFrom: Wedson Almeida Filho <[email protected]>
This adds epoll integration, allowing you to get an epoll notification when an incoming transaction arrives.
--- drivers/android/process.rs | 21 +++++++++++++++++---- drivers/android/thread.rs | 39 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 53 insertions(+), 7 deletions(-)
drivers/android/process.rs +17 -4
@@ -122,8 +122,16 @@ pub(crate) fn push_work( }else if self.is_dead {Err ((BinderError ::new_dead (), work)) }else { +let sync = work.should_sync_wakeup (); +// There are no ready threads. Push work to process queue. self.work.push_back (work); + +// Wake up polling threads, if any. +for threadin self.threads.values () { + thread.notify_if_poll_ready (sync); + } +Ok (()) } }
@@ -897,11 +905,16 @@ pub(crate) fn mmap( }pub (crate )fn poll ( - _this:ArcBorrow <'_,Process >, - _file: &File , - _table: &mut PollTable , + this:ArcBorrow <'_,Process >, + file: &File , + table: &mut PollTable , ) ->Result <u32 > { -Err (EINVAL ) +let thread = this.get_thread (kernel::current!().pid ())?; +let (from_proc,mut mask) = thread.poll (file, table); +if mask ==0 && from_proc && !this.inner.lock ().work.is_empty () { + mask |= bindings::POLLIN ; + } +Ok (mask) } }
drivers/android/thread.rs +36 -3
@@ -7,6 +7,7 @@use kernel::{ bindings, + file::{File ,PollCondVar ,PollTable }, io_buffer::{IoBufferReader ,IoBufferWriter }, list::{AtomicListArcTracker ,HasListLinks ,List ,ListArc ,ListArcSafe ,ListItem ,ListLinks ,
@@ -14,7 +15,7 @@ }, prelude::*, security, - sync::{Arc, CondVar, SpinLock}, + sync::{Arc, SpinLock}, types::Either, user_ptr::{UserSlicePtr, UserSlicePtrWriter}, };
@@ -75,6 +76,7 @@ struct InnerThread {const LOOPER_INVALID :u32 =0x08 ;const LOOPER_WAITING :u32 =0x10 ;const LOOPER_WAITING_PROC :u32 =0x20 ; +const LOOPER_POLL :u32 =0x40 ;impl InnerThread {fn new () ->Result <Self > {
@@ -159,6 +161,15 @@ fn is_looper(&self) -> bool {fn should_use_process_work_queue (&self) ->bool { !self.process_work_list && self.is_looper () } + +fn poll (&mut self) ->u32 { + self.looper_flags |=LOOPER_POLL ; +if self.process_work_list || self.looper_need_return { + bindings::POLLIN + }else { +0 + } + } }/// This represents a thread that's used with binder.
@@ -169,7 +180,7 @@ pub(crate) struct Thread {#[pin] inner: SpinLock<InnerThread>,#[pin] - work_condvar: CondVar, + work_condvar: PollCondVar,/// Used to insert this thread into the process' `ready_threads` list. /// /// INVARIANT: May never be used for any other list than the `self.process.ready_threads`.
@@ -201,7 +212,7 @@ pub(crate) fn new(id: i32, process: Arc<Process>) -> Result<Arc<Self>> { id, process, inner <- kernel::new_spinlock!(inner, "Thread::inner"), - work_condvar <- kernel::new_condvar!("Thread::work_condvar"), + work_condvar <- kernel::new_poll_condvar!("Thread::work_condvar"), links <- ListLinks::new(), links_track <- AtomicListArcTracker::new(), }))
@@ -590,6 +601,12 @@ pub(crate) fn write_read(self: &Arc<Self>, data: UserSlicePtr, wait: bool) -> Re ret } +pub (crate )fn poll (&self, file: &File , table: &mut PollTable ) -> (bool ,u32 ) { + table.register_wait (file, &self.work_condvar); +let mut inner = self.inner.lock (); + (inner.should_use_process_work_queue (), inner.poll ()) + } +/// Make the call to `get_work` or `get_work_local` return immediately, if any. pub (crate )fn exit_looper (&self) {let mut inner = self.inner.lock ();
@@ -604,6 +621,22 @@ pub(crate) fn exit_looper(&self) { } } +pub (crate )fn notify_if_poll_ready (&self, sync:bool ) { +// Determine if we need to notify. This requires the lock. +let inner = self.inner.lock (); +let notify = inner.looper_flags &LOOPER_POLL !=0 && inner.should_use_process_work_queue (); +drop (inner); + +// Now that the lock is no longer held, notify the waiters if we have to. +if notify { +if sync { + self.work_condvar.notify_sync (); + }else { + self.work_condvar.notify_one (); + } + } + } +pub (crate )fn release (self: &Arc <Self >) { self.inner.lock ().is_dead =true ;
Signature · 1 line
2.42.0.820.g83a721a137-goog
Alice Ryhl
maintainerRFC PATCH 8/20 rust_binder: add non-oneway transactions
In reply to Alice RyhlFrom: Wedson Almeida Filho <[email protected]>
Make it possible to send transactions that are not oneway transactions, that is, transactions that you need to reply to.
Generally, binder will try to look like a normal function call, where the call blocks until the function returns. This is implemented by allowing you to reply to incoming transactions, and having the sender sleep until a reply arrives.
For each thread, binder will keep track of the current transaction. Furthermore, if you send a transaction from a thread that already has a current transaction, then binder will make that transaction into a "sub-transaction". This mimicks a call stack with normal functions. If you use subtransactions to send calls A->B->A with A and B being two different processes, then binder will ensure that the incoming sub-transaction is executed on the thread in A that sent the original message to B (and that thread in A is not used for any other incoming transactions). This feature is often referred to as "deadlock avoidance" because it avoids the case where A's threadpool has run out of threads, preventing the incoming subtransaction from being processed.
--- drivers/android/defs.rs | 2 + drivers/android/thread.rs | 218 ++++++++++++++++++++++++++++++++++++++++- drivers/android/transaction.rs | 132 ++++++++++++++++++++++--- 3 files changed, 336 insertions(+), 16 deletions(-)
drivers/android/defs.rs +2 -0
@@ -33,6 +33,8 @@ macro_rules! pub_no_prefix { binder_driver_command_protocol_, BC_TRANSACTION, BC_TRANSACTION_SG, + BC_REPLY, + BC_REPLY_SG, BC_FREE_BUFFER, BC_ENTER_LOOPER, BC_EXIT_LOOPER,
drivers/android/thread.rs +214 -4
299 lines not on this page · raw message
drivers/android/transaction.rs +120 -12
206 lines not on this page · raw message
Signature · 1 line
2.42.0.820.g83a721a137-goog
Alice Ryhl
maintainerRFC PATCH 9/20 rust_binder: serialize oneway transactions
In reply to Alice RyhlThe binder driver guarantees that oneway transactions sent to the same node are serialized, that is, userspace will not be given the next one until it has finished processing the previous oneway transaction. This is done to avoid the case where two oneway transactions arrive in opposite order from the order in which they were sent. (E.g., they could be delivered to two different threads, which could appear as-if they were sent in opposite order.)
To fix that, we store pending oneway transactions in a separate list in the node, and don't deliver the next oneway transaction until userspace signals that it has finished processing the previous oneway transaction by calling the BC_FREE_BUFFER ioctl.
--- drivers/android/allocation.rs | 19 +++++++++- drivers/android/node.rs | 79 ++++++++++++++++++++++++++++++++++++++++-- drivers/android/process.rs | 25 ++++++++++--- drivers/android/transaction.rs | 26 ++++++++++++-- 4 files changed, 138 insertions(+), 11 deletions(-)
drivers/android/allocation.rs +18 -1
@@ -3,13 +3,22 @@use kernel::{bindings, pages::Pages , prelude::*, sync::Arc , user_ptr::UserSlicePtrReader }; -use crate ::{node::NodeRef , process::Process }; +use crate ::{ + node::{Node ,NodeRef }, + process::Process , +DArc , +};#[derive( Default )] pub (crate )struct AllocationInfo {/// The target node of the transaction this allocation is associated to. /// Not set for replies. pub (crate ) target_node:Option <NodeRef >, +/// When this allocation is dropped, call `pending_oneway_finished` on the node. +/// +/// This is used to serialize oneway transaction on the same node. Binder guarantees that +/// oneway transactions to the same node are delivered sequentially in the order they are sent. +pub (crate ) oneway_node:Option <DArc <Node >>,/// Zero the data in the buffer on free. pub (crate ) clear_on_free:bool , }
@@ -110,6 +119,10 @@ pub(crate) fn get_or_init_info(&mut self) -> &mut AllocationInfo { self.allocation_info.get_or_insert_with (Default ::default) } +pub (crate )fn set_info_oneway_node (&mut self, oneway_node:DArc <Node >) { + self.get_or_init_info ().oneway_node =Some (oneway_node); + } +pub (crate )fn set_info_clear_on_drop (&mut self) { self.get_or_init_info ().clear_on_free =true ; }
@@ -126,6 +139,10 @@ fn drop(&mut self) { }if let Some (mut info) = self.allocation_info.take () { +if let Some (oneway_node) = info.oneway_node.as_ref () { + oneway_node.pending_oneway_finished (); + } + info.target_node =None ;if info.clear_on_free {
drivers/android/node.rs +77 -2
117 lines not on this page · raw message
drivers/android/process.rs +20 -5
@@ -136,6 +136,11 @@ pub(crate) fn push_work( } } +/// Push work to be cancelled. Only used during process teardown. +pub (crate )fn push_work_for_release (&mut self, work:DLArc <dyn DeliverToRead >) { + self.work.push_back (work); + } +pub (crate )fn remove_node (&mut self, ptr:usize ) { self.nodes.remove (&ptr); }
@@ -740,6 +745,21 @@ fn deferred_release(self: Arc<Self>) { self.ctx.deregister_process (&self); +// Move oneway_todo into the process todolist. + { +let mut inner = self.inner.lock (); +let nodes =take (&mut inner.nodes); +for nodein nodes.values () { + node.release (&mut inner); + } + inner.nodes = nodes; + } + +// Cancel all pending work items. +while let Some (work) = self.get_work () { + work.into_arc ().cancel (); + } +// Move the threads out of `inner` so that we can iterate over them without holding the // lock. let mut inner = self.inner.lock ();
@@ -751,11 +771,6 @@ fn deferred_release(self: Arc<Self>) { thread.release (); } -// Cancel all pending work items. -while let Some (work) = self.get_work () { - work.into_arc ().cancel (); - } -// Free any resources kept alive by allocated buffers. let omapping = self.inner.lock ().mapping.take ();if let Some (mut mapping) = omapping {
drivers/android/transaction.rs +23 -3
@@ -62,9 +62,12 @@ pub(crate) fn new(return Err (err); } }; -if trd.flags &TF_ONE_WAY !=0 && stack_next.is_some () { -pr_warn! ("Oneway transaction should not be in a transaction stack." ); -return Err (EINVAL .into ()); +if trd.flags &TF_ONE_WAY !=0 { +if stack_next.is_some () { +pr_warn! ("Oneway transaction should not be in a transaction stack." ); +return Err (EINVAL .into ()); + } + alloc.set_info_oneway_node (node_ref.node.clone ()); }if trd.flags &TF_CLEAR_BUF !=0 { alloc.set_info_clear_on_drop ();
@@ -165,9 +168,26 @@ pub(crate) fn find_from(&self, thread: &Thread) -> Option<DArc<Transaction>> {/// /// Not used for replies. pub (crate )fn submit (self:DLArc <Self >) ->BinderResult { +let oneway = self.flags &TF_ONE_WAY !=0 ;let process = self.to.clone ();let mut process_inner = process.inner.lock (); +if oneway { +if let Some (target_node) = self.target_node.clone () { +match target_node.submit_oneway (self, &mut process_inner) { +Ok (()) =>return Ok (()), +Err ((err, work)) => { +drop (process_inner); +// Drop work after releasing process lock. +drop (work); +return Err (err); + } + } + }else { +pr_err! ("Failed to submit oneway transaction to node." ); + } + } +let res =if let Some (thread) = self.find_target_thread () {match thread.push_work (self) {PushWorkRes ::Ok =>Ok (()),
Signature · 1 line
2.42.0.820.g83a721a137-goog
Alice Ryhl
maintainerRFC PATCH 10/20 rust_binder: add death notifications
In reply to Alice RyhlFrom: Wedson Almeida Filho <[email protected]>
This adds death notifications that let one process be notified when another process dies.
A process can request to be notified when a process dies using BC_REQUEST_DEATH_NOTIFICATION. This will make the driver send a BR_DEAD_BINDER to userspace when the process dies (or immediately if it is already dead). Userspace is supposed to respond with BC_DEAD_BINDER_DONE once it has processed the notification.
Userspace can unregister from death notifications using the BC_CLEAR_DEATH_NOTIFICATION command. In this case, the kernel will respond with BR_CLEAR_DEATH_NOTIFICATION_DONE once the notification has been removed. Note that if the remote process dies before the kernel has responded with BR_CLEAR_DEATH_NOTIFICATION_DONE, then the kernel will still send a BR_DEAD_BINDER, which userspace must be able to process. In this case, the kernel will wait for the BC_DEAD_BINDER_DONE command before it sends BR_CLEAR_DEATH_NOTIFICATION_DONE.
Note that even if the kernel sends a BR_DEAD_BINDER, this does not remove the death notification. Userspace must still remove it manually using BC_CLEAR_DEATH_NOTIFICATION.
If a process uses BC_RELEASE to destroy its last refcount on a node that has an active death registration, then the death registration is immediately deleted. However, userspace is not supposed to delete a node reference without first deregistering death notifications, so this codepath is not executed under normal circumstances.
--- drivers/android/defs.rs | 10 +- drivers/android/node.rs | 258 ++++++++++++++++++++++++++++++++++++++++- drivers/android/process.rs | 193 +++++++++++++++++++++++++++--- drivers/android/rust_binder.rs | 7 ++ drivers/android/thread.rs | 22 +++- 5 files changed, 471 insertions(+), 19 deletions(-)
drivers/android/defs.rs +8 -2
@@ -23,10 +23,13 @@ macro_rules! pub_no_prefix { BR_SPAWN_LOOPER, BR_TRANSACTION_COMPLETE, BR_OK, + BR_ERROR, BR_INCREFS, BR_ACQUIRE, BR_RELEASE, -BR_DECREFS + BR_DECREFS, + BR_DEAD_BINDER, +BR_CLEAR_DEATH_NOTIFICATION_DONE );pub_no_prefix! (
@@ -44,7 +47,10 @@ macro_rules! pub_no_prefix { BC_RELEASE, BC_DECREFS, BC_INCREFS_DONE, -BC_ACQUIRE_DONE + BC_ACQUIRE_DONE, + BC_REQUEST_DEATH_NOTIFICATION, + BC_CLEAR_DEATH_NOTIFICATION, +BC_DEAD_BINDER_DONE ); pub(crate) const FLAT_BINDER_FLAG_TXN_SECURITY_CTX: u32 =
drivers/android/node.rs +256 -2
288 lines not on this page · raw message
drivers/android/process.rs +179 -14
267 lines not on this page · raw message
drivers/android/rust_binder.rs +7 -0
@@ -100,6 +100,13 @@ impl<T: ?Sized> core::ops::Receiver for DTRWrap<T> {} type DLArc<T> = kernel::list::ListArc<DTRWrap<T>>; impl<T: ListArcSafe> DTRWrap<T> { + fn new(val: impl PinInit<T>) -> impl PinInit<Self> { + pin_init!(Self { + links <- ListLinksSelfPtr::new(), + wrapped <- val, + }) + } + #[allow(dead_code)] fn arc_try_new(val: T) -> Result<DLArc<T>, alloc::alloc::AllocError> { ListArc::pin_init(pin_init!(Self {
drivers/android/thread.rs +21 -1
@@ -391,10 +391,27 @@ pub(crate) fn push_work(&self, work: DLArc<dyn DeliverToRead>) -> PushWorkRes { res } +/// Attempts to push to given work item to the thread if it's a looper thread (i.e., if it's +/// part of a thread pool) and is alive. Otherwise, push the work item to the process instead. +pub (crate )fn push_work_if_looper (&self, work:DLArc <dyn DeliverToRead >) ->BinderResult { +let mut inner = self.inner.lock (); +if inner.is_looper () && !inner.is_dead { + inner.push_work (work); +Ok (()) + }else { +drop (inner); + self.process.push_work (work) + } + } +pub (crate )fn push_work_deferred (&self, work:DLArc <dyn DeliverToRead >) { self.inner.lock ().push_work_deferred (work); } +pub (crate )fn push_return_work (&self, reply:u32 ) { + self.inner.lock ().push_return_work (reply); + } +pub (crate )fn copy_transaction_data ( &self, to_process: Arc<Process>,
@@ -556,7 +573,7 @@ fn transaction<T>(self: &Arc<Self>, tr: &BinderTransactionDataSg, inner: T) ); } - self.inner.lock ().push_return_work (err.reply); + self.push_return_work (err.reply); } }
@@ -684,6 +701,9 @@ fn write(self: &Arc<Self>, req: &mut BinderWriteRead) -> Result { BC_DECREFS => self.process.update_ref(reader.read()?, false, false)?, BC_INCREFS_DONE => self.process.inc_ref_done(&mut reader, false)?, BC_ACQUIRE_DONE => self.process.inc_ref_done(&mut reader, true)?, + BC_REQUEST_DEATH_NOTIFICATION => self.process.request_death(&mut reader, self)?, + BC_CLEAR_DEATH_NOTIFICATION => self.process.clear_death(&mut reader, self)?, + BC_DEAD_BINDER_DONE => self.process.dead_binder_done(reader.read()?, self), BC_REGISTER_LOOPER => {let valid = self.process.register_thread (); self.inner.lock ().looper_register (valid);
Signature · 1 line
2.42.0.820.g83a721a137-goog
Alice Ryhl
maintainerRFC PATCH 11/20 rust_binder: send nodes in transactions
In reply to Alice RyhlTo send a transaction to any process other than the context manager, someone must first send you the binder node. Usually, you get it from the context manager.
The transaction allocation now contains a list of offsets of objects in the transaction that must be translated before they are passed to the target process. In this patch, we only support translation of binder nodes, but future patches will extend this to other object types.
--- drivers/android/allocation.rs | 266 ++++++++++++++++++++++++++++++++++++++++- drivers/android/defs.rs | 44 +++++-- drivers/android/process.rs | 8 ++ drivers/android/thread.rs | 118 +++++++++++++++++- drivers/android/transaction.rs | 5 +- rust/helpers.c | 7 ++ rust/kernel/security.rs | 7 ++ 7 files changed, 436 insertions(+), 19 deletions(-)
drivers/android/allocation.rs +264 -2
@@ -1,9 +1,18 @@// SPDX-License-Identifier: GPL-2.0 -use core::mem::size_of_val; +use core::mem::{size_of, size_of_val,MaybeUninit }; +use core::ops::Range ; -use kernel::{bindings, pages::Pages , prelude::*, sync::Arc , user_ptr::UserSlicePtrReader }; +use kernel::{ + bindings, + io_buffer::{IoBufferReader ,ReadableFromBytes ,WritableToBytes }, + pages::Pages , + prelude::*, + sync::Arc , + user_ptr::UserSlicePtrReader , +};use crate ::{ + defs::*, node::{Node ,NodeRef }, process::Process ,DArc ,
@@ -11,6 +20,8 @@#[derive( Default )] pub (crate )struct AllocationInfo { +/// Range within the allocation where we can find the offsets to the object descriptors. +pub (crate ) offsets:Option <Range <usize >>,/// The target node of the transaction this allocation is associated to. /// Not set for replies. pub (crate ) target_node:Option <NodeRef >,
@@ -87,6 +98,21 @@ pub(crate) fn copy_into( }) } +pub (crate )fn read <T :ReadableFromBytes >(&self, offset:usize ) ->Result <T > { +let mut out =MaybeUninit ::<T >::uninit (); +let mut out_offset =0 ; + self.iterate (offset,size_of ::<T >(), |page, offset, to_copy| { +// SAFETY: The sum of `offset` and `to_copy` is bounded by the size of T. +let obj_ptr =unsafe { (out.as_mut_ptr ()as *mut u8 ).add (out_offset) }; +// SAFETY: The pointer points is in-bounds of the `out` variable, so it is valid. +unsafe { page.read (obj_ptr, offset, to_copy) }?; + out_offset += to_copy; +Ok (()) + })?; +// SAFETY: We just initialised the data. +Ok (unsafe { out.assume_init () }) + } +pub (crate )fn write <T : ?Sized >(&self, offset:usize , obj: &T ) ->Result {let mut obj_offset =0 ; self.iterate (offset,size_of_val (obj), |page, offset, to_copy| {
@@ -119,6 +145,10 @@ pub(crate) fn get_or_init_info(&mut self) -> &mut AllocationInfo { self.allocation_info.get_or_insert_with (Default ::default) } +pub (crate )fn set_info_offsets (&mut self, offsets:Range <usize >) { + self.get_or_init_info ().offsets =Some (offsets); + } +pub (crate )fn set_info_oneway_node (&mut self, oneway_node:DArc <Node >) { self.get_or_init_info ().oneway_node =Some (oneway_node); }
@@ -145,6 +175,15 @@ fn drop(&mut self) { info.target_node =None ; +if let Some (offsets) = info.offsets.clone () { +let view =AllocationView ::new (self, offsets.start); +for iin offsets.step_by (size_of ::<usize >()) { +if view.cleanup_object (i).is_err () { +pr_warn! ("Error cleaning up object at offset {}\n" , i) + } + } + } +if info.clear_on_free {if let Err (e) = self.fill_zero () {pr_warn! ("Failed to clear data on free: {:?}" , e);
@@ -155,3 +194,226 @@ fn drop(&mut self) { self.process.buffer_raw_free (self.ptr); } } + +/// A view into the beginning of an allocation. +/// +/// All attempts to read or write outside of the view will fail. To intentionally access outside of +/// this view, use the `alloc` field of this struct directly. +pub (crate )struct AllocationView <'a> { +pub (crate ) alloc: &'amut Allocation , + limit:usize , +} + +impl <'a>AllocationView <'a> { +pub (crate )fn new (alloc: &'amut Allocation , limit:usize ) ->Self { +AllocationView { alloc, limit } + } + +pub (crate )fn read <T :ReadableFromBytes >(&self, offset:usize ) ->Result <T > { +if offset.checked_add (size_of ::<T >()).ok_or (EINVAL )? > self.limit { +return Err (EINVAL ); + } + self.alloc.read (offset) + } + +pub (crate )fn write <T :WritableToBytes >(&self, offset:usize , obj: &T ) ->Result { +if offset.checked_add (size_of ::<T >()).ok_or (EINVAL )? > self.limit { +return Err (EINVAL ); + } + self.alloc.write (offset, obj) + } + +pub (crate )fn transfer_binder_object ( + &self, + offset:usize , + obj: &bindings::flat_binder_object , + strong:bool , + node_ref:NodeRef , + ) ->Result { +if Arc ::ptr_eq (&node_ref.node.owner, &self.alloc.process) { +// The receiving process is the owner of the node, so send it a binder object (instead +// of a handle). +let (ptr, cookie) = node_ref.node.get_id (); +let mut newobj =FlatBinderObject ::default (); + newobj.hdr.type_ =if strong { +BINDER_TYPE_BINDER + }else { +BINDER_TYPE_WEAK_BINDER + }; + newobj.flags = obj.flags; + newobj.__bindgen_anon_1.binder = ptras _ ; + newobj.cookie = cookieas _ ; + self.write (offset, &newobj)?; +// Increment the user ref count on the node. It will be decremented as part of the +// destruction of the buffer, when we see a binder or weak-binder object. + node_ref.node.update_refcount (true ,1 , strong); + }else { +// The receiving process is different from the owner, so we need to insert a handle to +// the binder object. +let handle = self + .alloc + .process + .insert_or_update_handle (node_ref,false )?; +let mut newobj =FlatBinderObject ::default (); + newobj.hdr.type_ =if strong { +BINDER_TYPE_HANDLE + }else { +BINDER_TYPE_WEAK_HANDLE + }; + newobj.flags = obj.flags; + newobj.__bindgen_anon_1.handle = handle; +if self.write (offset, &newobj).is_err () { +// Decrement ref count on the handle we just created. +let _ = self.alloc.process.update_ref (handle,false , strong); +return Err (EINVAL ); + } + } +Ok (()) + } + +fn cleanup_object (&self, index_offset:usize ) ->Result { +let offset = self.alloc.read (index_offset)?; +let header = self.read ::<BinderObjectHeader >(offset)?; +match header.type_ { +BINDER_TYPE_WEAK_BINDER |BINDER_TYPE_BINDER => { +let obj = self.read ::<FlatBinderObject >(offset)?; +let strong = header.type_ ==BINDER_TYPE_BINDER ; +// SAFETY: The type is `BINDER_TYPE_{WEAK_}BINDER`, so the `binder` field is +// populated. +let ptr =unsafe { obj.__bindgen_anon_1.binder }as usize ; +let cookie = obj.cookieas usize ; + self.alloc.process.update_node (ptr, cookie, strong); +Ok (()) + } +BINDER_TYPE_WEAK_HANDLE |BINDER_TYPE_HANDLE => { +let obj = self.read ::<FlatBinderObject >(offset)?; +let strong = header.type_ ==BINDER_TYPE_HANDLE ; +// SAFETY: The type is `BINDER_TYPE_{WEAK_}HANDLE`, so the `handle` field is +// populated. +let handle =unsafe { obj.__bindgen_anon_1.handle }as _ ; + self.alloc.process.update_ref (handle,false , strong) + } + _ =>Ok (()), + } + } +} + +/// A binder object as it is serialized. +/// +/// # Invariants +/// +/// All bytes must be initialized, and the value of `self.hdr.type_` must be one of the allowed +/// types. +#[repr( C )] +pub (crate )union BinderObject { + hdr: bindings::binder_object_header , + fbo: bindings::flat_binder_object , + fdo: bindings::binder_fd_object , + bbo: bindings::binder_buffer_object , + fdao: bindings::binder_fd_array_object , +} + +/// A view into a `BinderObject` that can be used in a match statement. +pub (crate )enum BinderObjectRef <'a> { +Binder (&'amut bindings::flat_binder_object ), +Handle (&'amut bindings::flat_binder_object ), +Fd (&'amut bindings::binder_fd_object ), +Ptr (&'amut bindings::binder_buffer_object ), +Fda (&'amut bindings::binder_fd_array_object ), +} + +impl BinderObject { +pub (crate )fn read_from (reader: &mut UserSlicePtrReader ) ->Result <BinderObject > { +let object =Self ::read_from_inner (|slice| { +let read_len = usize::min (slice.len (), reader.len ()); +// SAFETY: The length we pass to `read_raw` is at most the length of the slice. +unsafe { + reader + .clone_reader () + .read_raw (slice.as_mut_ptr (), read_len)?; + } +Ok (()) + })?; + +// If we used a object type smaller than the largest object size, then we've read more +// bytes than we needed to. However, we used `.clone_reader()` to avoid advancing the +// original reader. Now, we call `skip` so that the caller's reader is advanced by the +// right amount. +// +// The `skip` call fails if the reader doesn't have `size` bytes available. This could +// happen if the type header corresponds to an object type that is larger than the rest of +// the reader. +// +// Any extra bytes beyond the size of the object are inaccessible after this call, so +// reading them again from the `reader` later does not result in TOCTOU bugs. + reader.skip (object.size ())?; + +Ok (object) + } + +/// Use the provided reader closure to construct a `BinderObject`. +/// +/// The closure should write the bytes for the object into the provided slice. +pub (crate )fn read_from_inner <R >(reader:R ) ->Result <BinderObject > +where +R :FnOnce (&mut [u8 ;size_of ::<BinderObject >()]) ->Result <()>, + { +let mut obj =MaybeUninit ::<BinderObject >::zeroed (); + +// SAFETY: The lengths of `BinderObject` and `[u8; size_of::<BinderObject>()]` are equal, +// and the byte array has an alignment requirement of one, so the pointer cast is okay. +// Additionally, `obj` was initialized to zeros, so the byte array will not be +// uninitialized. + (reader)(unsafe { &mut *obj.as_mut_ptr ().cast () })?; + +// SAFETY: The entire object is initialized, so accessing this field is safe. +let type_ =unsafe { obj.assume_init_ref ().hdr.type_ }; +if Self ::type_to_size (type_).is_none () { +// The value of `obj.hdr_type_` was invalid. +return Err (EINVAL ); + } + +// SAFETY: All bytes are initialized (since we zeroed them at the start) and we checked +// that `self.hdr.type_` is one of the allowed types, so the type invariants are satisfied. +unsafe {Ok (obj.assume_init ()) } + } + +pub (crate )fn as_ref (&mut self) ->BinderObjectRef <'_> { +use BinderObjectRef ::*; +// SAFETY: The constructor ensures that all bytes of `self` are initialized, and all +// variants of this union accept all initialized bit patterns. +unsafe { +match self.hdr.type_ { +BINDER_TYPE_WEAK_BINDER |BINDER_TYPE_BINDER =>Binder (&mut self.fbo), +BINDER_TYPE_WEAK_HANDLE |BINDER_TYPE_HANDLE =>Handle (&mut self.fbo), +BINDER_TYPE_FD =>Fd (&mut self.fdo), +BINDER_TYPE_PTR =>Ptr (&mut self.bbo), +BINDER_TYPE_FDA =>Fda (&mut self.fdao), +// SAFETY: By the type invariant, the value of `self.hdr.type_` cannot have any +// other value than the ones checked above. + _ => core::hint::unreachable_unchecked (), + } + } + } + +pub (crate )fn size (&self) ->usize { +// SAFETY: The entire object is initialized, so accessing this field is safe. +let type_ =unsafe { self.hdr.type_ }; + +// SAFETY: The type invariants guarantee that the type field is correct. +unsafe {Self ::type_to_size (type_).unwrap_unchecked () } + } + +fn type_to_size (type_:u32 ) ->Option <usize > { +match type_ { +BINDER_TYPE_WEAK_BINDER =>Some (size_of ::<bindings::flat_binder_object >()), +BINDER_TYPE_BINDER =>Some (size_of ::<bindings::flat_binder_object >()), +BINDER_TYPE_WEAK_HANDLE =>Some (size_of ::<bindings::flat_binder_object >()), +BINDER_TYPE_HANDLE =>Some (size_of ::<bindings::flat_binder_object >()), +BINDER_TYPE_FD =>Some (size_of ::<bindings::binder_fd_object >()), +BINDER_TYPE_PTR =>Some (size_of ::<bindings::binder_buffer_object >()), +BINDER_TYPE_FDA =>Some (size_of ::<bindings::binder_fd_array_object >()), + _ =>None , + } + } +}
drivers/android/defs.rs +34 -10
93 lines not on this page · raw message
drivers/android/process.rs +8 -0
@@ -591,6 +591,14 @@ pub(crate) fn update_ref(&self, handle: u32, inc: bool, strong: bool) -> ResultOk (()) } +/// Decrements the refcount of the given node, if one exists. +pub (crate )fn update_node (&self, ptr:usize , cookie:usize , strong:bool ) { +let mut inner = self.inner.lock (); +if let Ok (Some (node)) = inner.get_existing_node (ptr, cookie) { + inner.update_node_refcount (&node,false , strong,1 ,None ); + } + } +pub (crate )fn inc_ref_done (&self, reader: &mut UserSlicePtrReader , strong:bool ) ->Result {let ptr = reader.read ::<usize >()?;let cookie = reader.read ::<usize >()?;
drivers/android/thread.rs +112 -6
151 lines not on this page · raw message
drivers/android/transaction.rs +4 -1
@@ -32,6 +32,7 @@ pub(crate) struct Transaction { code: u32, pub(crate) flags: u32, data_size: usize, + offsets_size: usize, data_address: usize, sender_euid: Kuid, txn_security_ctx_off: Option<usize>,
@@ -85,6 +86,7 @@ pub(crate) fn new( code: trd.code, flags: trd.flags, data_size: trd.data_size as _, + offsets_size: trd.offsets_size as _, data_address, allocation <- kernel::new_spinlock!(Some(alloc), "Transaction::new"), txn_security_ctx_off,
@@ -116,6 +118,7 @@ pub(crate) fn new_reply( code: trd.code, flags: trd.flags, data_size: trd.data_size as _, + offsets_size: trd.offsets_size as _, data_address: alloc.ptr, allocation <- kernel::new_spinlock!(Some(alloc), "Transaction::new"), txn_security_ctx_off: None,
@@ -229,7 +232,7 @@ fn do_work(self: DArc<Self>, thread: &Thread, writer: &mut UserSlicePtrWriter) - tr.flags = self.flags; tr.data_size = self.data_sizeas _ ; tr.data.ptr.buffer = self.data_addressas _ ; - tr.offsets_size =0 ; + tr.offsets_size = self.offsets_sizeas _ ;if tr.offsets_size >0 { tr.data.ptr.offsets = (self.data_address +ptr_align (self.data_size))as _ ; }
rust/helpers.c +7 -0
@@ -342,6 +342,13 @@ int rust_helper_security_binder_transaction(const struct cred *from, return security_binder_transaction(from, to); }EXPORT_SYMBOL_GPL (rust_helper_security_binder_transaction); + +int rust_helper_security_binder_transfer_binder (const struct cred *from, +const struct cred *to) +{ +return security_binder_transfer_binder (from, to); +} +EXPORT_SYMBOL_GPL (rust_helper_security_binder_transfer_binder);#endif /*
rust/kernel/security.rs +7 -0
@@ -24,6 +24,13 @@ pub fn binder_transaction(from: &Credential, to: &Credential) -> Result {to_result (unsafe { bindings::security_binder_transaction (from.0 .get (), to.0 .get ()) }) } +/// Calls the security modules to determine if task `from` is allowed to send binder objects +/// (owned by itself or other processes) to task `to` through a binder transaction. +pub fn binder_transfer_binder (from: &Credential , to: &Credential ) ->Result { +// SAFETY: `from` and `to` are valid because the shared references guarantee nonzero refcounts. +to_result (unsafe { bindings::security_binder_transfer_binder (from.0 .get (), to.0 .get ()) }) +} +/// A security context string. /// /// The struct has the invariant that it always contains a valid security context.
Signature · 1 line
2.42.0.820.g83a721a137-goog
Alice Ryhl
maintainerRFC PATCH 12/20 rust_binder: add BINDER_TYPE_PTR support
In reply to Alice RyhlImplement support for the scatter-gather feature of binder, which lets you embed pointers in binder transactions and have them be translated so that the recipient gets a pointer that also works for them.
This works by adding a second kind of object to the offset array, namely the BINDER_TYPE_PTR object. This object has a pointer and length embedded. The kernel will copy the data behind the pointer, and update the address of the pointer so that the recipient will be able to follow the pointer and see the same data.
These objects are supported recursively. Other than the pointer in the main transaction buffer, each buffer may be pointed at by a pointer in one of the other buffers. This can be used to build arbitrary trees of buffers.
--- drivers/android/defs.rs | 1 + drivers/android/error.rs | 9 ++ drivers/android/thread.rs | 340 +++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 344 insertions(+), 6 deletions(-)
drivers/android/defs.rs +1 -0
@@ -105,6 +105,7 @@ fn default() -> Self {decl_wrapper! (BinderNodeInfoForRef , bindings::binder_node_info_for_ref);decl_wrapper! (FlatBinderObject , bindings::flat_binder_object);decl_wrapper! (BinderObjectHeader , bindings::binder_object_header); +decl_wrapper! (BinderBufferObject , bindings::binder_buffer_object);decl_wrapper! (BinderTransactionData , bindings::binder_transaction_data);decl_wrapper! (BinderTransactionDataSecctx ,
drivers/android/error.rs +9 -0
@@ -50,6 +50,15 @@ fn from(_: core::alloc::AllocError) -> Self { } } +impl From <alloc::collections::TryReserveError >for BinderError { +fn from (_: alloc::collections::TryReserveError ) ->Self { +Self { + reply:BR_FAILED_REPLY , + source:Some (ENOMEM ), + } + } +} +impl core::fmt::Debug for BinderError {fn fmt (&self, f: &mut core::fmt::Formatter <'_>) -> core::fmt::Result {match self.reply {
drivers/android/thread.rs +334 -6
409 lines not on this page · raw message
Signature · 1 line
2.42.0.820.g83a721a137-goog
Alice Ryhl
maintainerRFC PATCH 13/20 rust_binder: add BINDER_TYPE_FD support
In reply to Alice RyhlAdd support for sending fds over binder.
Unlike the other object types, file descriptors are not translated until the transaction is actually received by the recipient. Until that happens, we store u32::MAX as the fd.
Translating fds is done in a two-phase process. First, the file descriptors are allocated and written to the allocation. Then, once we have allocated all of them, we commit them to the files in question. Using this strategy, we are able to guarantee that we either send all of the fds, or none of them.
--- drivers/android/allocation.rs | 71 ++++++++++++++++++++++++++++++++++++++++++ drivers/android/defs.rs | 4 ++- drivers/android/error.rs | 6 ++++ drivers/android/thread.rs | 42 ++++++++++++++++++++++--- drivers/android/transaction.rs | 53 ++++++++++++++++++++++++------- rust/helpers.c | 8 +++++ rust/kernel/file.rs | 2 +- rust/kernel/security.rs | 11 +++++++ 8 files changed, 179 insertions(+), 18 deletions(-)
drivers/android/allocation.rs +71 -0
@@ -4,10 +4,12 @@use kernel::{ bindings, + file::{File ,FileDescriptorReservation }, io_buffer::{IoBufferReader ,ReadableFromBytes ,WritableToBytes }, pages::Pages , prelude::*, sync::Arc , + types::ARef , user_ptr::UserSlicePtrReader , };
@@ -32,6 +34,8 @@ pub(crate) struct AllocationInfo { pub(crate) oneway_node: Option<DArc<Node>>,/// Zero the data in the buffer on free. pub(crate) clear_on_free: bool, +/// List of files embedded in this transaction. + file_list: FileList, }/// Represents an allocation that the kernel is currently using.
@@ -160,6 +164,38 @@ pub(crate) fn set_info_clear_on_drop(&mut self) {pub (crate )fn set_info_target_node (&mut self, target_node:NodeRef ) { self.get_or_init_info ().target_node =Some (target_node); } + +pub (crate )fn info_add_fd (&mut self, file:ARef <File >, buffer_offset:usize ) ->Result { + self.get_or_init_info () + .file_list + .files_to_translate + .try_push (FileEntry { + file, + buffer_offset, + })?; + +Ok (()) + } + +pub (crate )fn translate_fds (&mut self) ->Result <TranslatedFds > { +let file_list =match self.allocation_info.as_mut () { +Some (info) => &mut info.file_list, +None =>return Ok (TranslatedFds ::new ()), + }; + +let files = core::mem::take (&mut file_list.files_to_translate); +let mut reservations =Vec ::try_with_capacity (files.len ())?; +for file_infoin files { +let res =FileDescriptorReservation ::new (bindings::O_CLOEXEC )?; + self.write ::<u32 >(file_info.buffer_offset, &res.reserved_fd ())?; + reservations.try_push (Reservation { + res, + file: file_info.file, + })?; + } + +Ok (TranslatedFds { reservations }) + } }impl Drop for Allocation {
@@ -417,3 +453,38 @@ fn type_to_size(type_: u32) -> Option<usize> { } } } + +#[derive( Default )] +struct FileList { + files_to_translate:Vec <FileEntry >, +} + +struct FileEntry { +/// The file for which a descriptor will be created in the recipient process. + file:ARef <File >, +/// The offset in the buffer where the file descriptor is stored. + buffer_offset:usize , +} + +pub (crate )struct TranslatedFds { + reservations:Vec <Reservation >, +} + +struct Reservation { + res:FileDescriptorReservation , + file:ARef <File >, +} + +impl TranslatedFds { +pub (crate )fn new () ->Self { +Self { + reservations:Vec ::new (), + } + } + +pub (crate )fn commit (self) { +for entryin self.reservations { + entry.res.commit (entry.file); + } + } +}
drivers/android/defs.rs +3 -1
@@ -54,9 +54,10 @@ macro_rules! pub_no_prefix {BC_DEAD_BINDER_DONE ); +pub (crate )const FLAT_BINDER_FLAG_ACCEPTS_FDS :u32 = kernel::bindings::FLAT_BINDER_FLAG_ACCEPTS_FDS ;pub (crate )const FLAT_BINDER_FLAG_TXN_SECURITY_CTX :u32 = kernel::bindings::FLAT_BINDER_FLAG_TXN_SECURITY_CTX ; -pub_no_prefix! (transaction_flags_,TF_ONE_WAY ,TF_CLEAR_BUF ); +pub_no_prefix! (transaction_flags_,TF_ONE_WAY ,TF_ACCEPT_FDS ,TF_CLEAR_BUF );pub (crate )use bindings::{BINDER_TYPE_BINDER ,BINDER_TYPE_FD ,BINDER_TYPE_FDA ,BINDER_TYPE_HANDLE ,BINDER_TYPE_PTR ,
@@ -104,6 +105,7 @@ fn default() -> Self {decl_wrapper! (BinderNodeDebugInfo , bindings::binder_node_debug_info);decl_wrapper! (BinderNodeInfoForRef , bindings::binder_node_info_for_ref);decl_wrapper! (FlatBinderObject , bindings::flat_binder_object); +decl_wrapper! (BinderFdObject , bindings::binder_fd_object);decl_wrapper! (BinderObjectHeader , bindings::binder_object_header);decl_wrapper! (BinderBufferObject , bindings::binder_buffer_object);decl_wrapper! (BinderTransactionData , bindings::binder_transaction_data);
drivers/android/error.rs +6 -0
@@ -41,6 +41,12 @@ fn from(source: Error) -> Self { } } +impl From <kernel::file::BadFdError >for BinderError { +fn from (source: kernel::file::BadFdError ) ->Self { +BinderError ::from (Error ::from (source)) + } +} +impl From <core::alloc::AllocError >for BinderError {fn from (_: core::alloc::AllocError ) ->Self {Self {
drivers/android/thread.rs +37 -5
72 lines not on this page · raw message
drivers/android/transaction.rs +42 -11
95 lines not on this page · raw message
rust/helpers.c +8 -0
@@ -349,6 +349,14 @@ int rust_helper_security_binder_transfer_binder(const struct cred *from, return security_binder_transfer_binder(from, to); }EXPORT_SYMBOL_GPL (rust_helper_security_binder_transfer_binder); + +int rust_helper_security_binder_transfer_file (const struct cred *from, +const struct cred *to, +struct file *file) +{ +return security_binder_transfer_file (from, to, file); +} +EXPORT_SYMBOL_GPL (rust_helper_security_binder_transfer_file);#endif /*
rust/kernel/file.rs +1 -1
@@ -107,7 +107,7 @@ pub mod flags {/// Instances of this type are always ref-counted, that is, a call to `get_file` ensures that the /// allocation remains valid at least until the matching call to `fput`. #[repr(transparent)] -pub struct File (Opaque <bindings::file >); +pub struct File (pub (crate )Opaque <bindings::file >);// SAFETY: By design, the only way to access a `File` is via an immutable reference or an `ARef`. // This means that the only situation in which a `File` can be accessed mutably is when the
rust/kernel/security.rs +11 -0
@@ -8,6 +8,7 @@ bindings, cred::Credential, error::{to_result, Result}, + file::File, };/// Calls the security modules to determine if the given task can become the manager of a binder
@@ -31,6 +32,16 @@ pub fn binder_transfer_binder(from: &Credential, to: &Credential) -> Result {to_result (unsafe { bindings::security_binder_transfer_binder (from.0 .get (), to.0 .get ()) }) } +/// Calls the security modules to determine if task `from` is allowed to send the given file to +/// task `to` (which would get its own file descriptor) through a binder transaction. +pub fn binder_transfer_file (from: &Credential , to: &Credential , file: &File ) ->Result { +// SAFETY: `from`, `to` and `file` are valid because the shared references guarantee nonzero +// refcounts. +to_result (unsafe { + bindings::security_binder_transfer_file (from.0 .get (), to.0 .get (), file.0 .get ()) + }) +} +/// A security context string. /// /// The struct has the invariant that it always contains a valid security context.
Signature · 1 line
2.42.0.820.g83a721a137-goog
Alice Ryhl
maintainerRFC PATCH 14/20 rust_binder: add BINDER_TYPE_FDA support
In reply to Alice RyhlIn the previous patch, we introduced support for BINDER_TYPE_FD objects that let you send a single fd, and in this patch, we add support for FD arrays. One important difference between BINDER_TYPE_FD and BINDER_TYPE_FDA is that FD arrays will close the file descriptors when the transaction allocation is freed, whereas FDs sent using BINDER_TYPE_FD are not closed.
Note that BINDER_TYPE_FDA is used only with hwbinder.
--- drivers/android/allocation.rs | 74 ++++++++++++++++++++++++++++++++--- drivers/android/defs.rs | 1 + drivers/android/thread.rs | 87 +++++++++++++++++++++++++++++++++++++++--- drivers/android/transaction.rs | 13 ++++--- 4 files changed, 159 insertions(+), 16 deletions(-)
drivers/android/allocation.rs +69 -5
@@ -4,7 +4,7 @@use kernel::{ bindings, - file::{File ,FileDescriptorReservation }, + file::{DeferredFdCloser ,File ,FileDescriptorReservation }, io_buffer::{IoBufferReader ,ReadableFromBytes ,WritableToBytes }, pages::Pages , prelude::*,
@@ -165,18 +165,38 @@ pub(crate) fn set_info_target_node(&mut self, target_node: NodeRef) { self.get_or_init_info ().target_node =Some (target_node); } -pub (crate )fn info_add_fd (&mut self, file:ARef <File >, buffer_offset:usize ) ->Result { +/// Reserve enough space to push at least `num_fds` fds. +pub (crate )fn info_add_fd_reserve (&mut self, num_fds:usize ) ->Result { + self.get_or_init_info () + .file_list + .files_to_translate + .try_reserve (num_fds)?; + +Ok (()) + } + +pub (crate )fn info_add_fd ( + &mut self, + file:ARef <File >, + buffer_offset:usize , + close_on_free:bool , + ) ->Result { self.get_or_init_info () .file_list .files_to_translate .try_push (FileEntry { file, buffer_offset, + close_on_free, })?;Ok (()) } +pub (crate )fn set_info_close_on_free (&mut self, cof:FdsCloseOnFree ) { + self.get_or_init_info ().file_list.close_on_free = cof.0 ; + } +pub (crate )fn translate_fds (&mut self) ->Result <TranslatedFds > {let file_list =match self.allocation_info.as_mut () {Some (info) => &mut info.file_list,
@@ -184,17 +204,38 @@ pub(crate) fn translate_fds(&mut self) -> Result<TranslatedFds> { };let files = core::mem::take (&mut file_list.files_to_translate); + +let num_close_on_free = files.iter ().filter (|entry| entry.close_on_free).count (); +let mut close_on_free =Vec ::try_with_capacity (num_close_on_free)?; +let mut reservations =Vec ::try_with_capacity (files.len ())?;for file_infoin files {let res =FileDescriptorReservation ::new (bindings::O_CLOEXEC )?; - self.write ::<u32 >(file_info.buffer_offset, &res.reserved_fd ())?; +let fd = res.reserved_fd (); + self.write ::<u32 >(file_info.buffer_offset, &fd)?; reservations.try_push (Reservation { res, file: file_info.file, })?; +if file_info.close_on_free { + close_on_free.try_push (fd)?; + } } -Ok (TranslatedFds { reservations }) +Ok (TranslatedFds { + reservations, + close_on_free:FdsCloseOnFree (close_on_free), + }) + } + +/// Should the looper return to userspace when freeing this allocation? +pub (crate )fn looper_need_return_on_free (&self) ->bool { +// Closing fds involves pushing task_work for execution when we return to userspace. Hence, +// we should return to userspace asap if we are closing fds. +match self.allocation_info { +Some (ref info) => !info.file_list.close_on_free.is_empty (), +None =>false , + } } }
@@ -220,6 +261,18 @@ fn drop(&mut self) { } } +for &fdin &info.file_list.close_on_free { +let closer =match DeferredFdCloser ::new () { +Ok (closer) => closer, +Err (core::alloc::AllocError ) => { +// Ignore allocation failures. +break ; + } + }; + + closer.close_fd (fd); + } +if info.clear_on_free {if let Err (e) = self.fill_zero () {pr_warn! ("Failed to clear data on free: {:?}" , e);
@@ -457,6 +510,7 @@ fn type_to_size(type_: u32) -> Option<usize> {#[derive( Default )] struct FileList { files_to_translate:Vec <FileEntry >, + close_on_free:Vec <u32 >, }struct FileEntry {
@@ -464,10 +518,15 @@ struct FileEntry { file: ARef<File>,/// The offset in the buffer where the file descriptor is stored. buffer_offset: usize, +/// Whether this fd should be closed when the allocation is freed. + close_on_free: bool, }pub (crate )struct TranslatedFds { reservations:Vec <Reservation >, +/// If commit is called, then these fds should be closed. (If commit is not called, then they +/// shouldn't be closed.) + close_on_free:FdsCloseOnFree , }struct Reservation {
@@ -479,12 +538,17 @@ impl TranslatedFds {pub (crate )fn new () ->Self {Self { reservations:Vec ::new (), + close_on_free:FdsCloseOnFree (Vec ::new ()), } } -pub (crate )fn commit (self) { +pub (crate )fn commit (self) ->FdsCloseOnFree {for entryin self.reservations { entry.res.commit (entry.file); } + + self.close_on_free } } + +pub (crate )struct FdsCloseOnFree (Vec <u32 >);
drivers/android/defs.rs +1 -0
@@ -106,6 +106,7 @@ fn default() -> Self {decl_wrapper! (BinderNodeInfoForRef , bindings::binder_node_info_for_ref);decl_wrapper! (FlatBinderObject , bindings::flat_binder_object);decl_wrapper! (BinderFdObject , bindings::binder_fd_object); +decl_wrapper! (BinderFdArrayObject , bindings::binder_fd_array_object);decl_wrapper! (BinderObjectHeader , bindings::binder_object_header);decl_wrapper! (BinderBufferObject , bindings::binder_buffer_object);decl_wrapper! (BinderTransactionData , bindings::binder_transaction_data);
drivers/android/thread.rs +82 -5
105 lines not on this page · raw message
drivers/android/transaction.rs +7 -6
@@ -288,17 +288,18 @@ fn do_work(self: DArc<Self>, thread: &Thread, writer: &mut UserSlicePtrWriter) - writer.write (&*tr)?; } +let mut alloc = self.allocation.lock ().take ().ok_or (ESRCH )?; +// Dismiss the completion of transaction with a failure. No failure paths are allowed from // here on out. send_failed_reply.dismiss (); -// It is now the user's responsibility to clear the allocation. -let alloc = self.allocation.lock ().take (); -if let Some (alloc) = alloc { - alloc.keep_alive (); - } +// Commit files, and set FDs in FDA to be closed on buffer free. +let close_on_free = files.commit (); + alloc.set_info_close_on_free (close_on_free); - files.commit (); +// It is now the user's responsibility to clear the allocation. + alloc.keep_alive ();// When this is not a reply and not a oneway transaction, update `current_transaction`. If // it's a reply, `current_transaction` has already been updated appropriately.
Signature · 1 line
2.42.0.820.g83a721a137-goog
Alice Ryhl
maintainerRFC PATCH 15/20 rust_binder: add process freezing
In reply to Alice RyhlWhen you want to freeze a process, you should process all incoming transactions before you freeze it. This patch helps with that. The idea is that before you freeze the process, you mark it as frozen in the binder driver. When this happens, all new incoming transactions are rejected, which lets you empty the queue of incoming transactions that were sent before you decided to freeze the process. Once you have processed every transaction in that queue, you can perform the actual freeze operation.
--- drivers/android/context.rs | 39 +++++++++++ drivers/android/defs.rs | 3 + drivers/android/error.rs | 8 +++ drivers/android/process.rs | 155 ++++++++++++++++++++++++++++++++++++++++- drivers/android/thread.rs | 12 +++- drivers/android/transaction.rs | 50 ++++++++++++- rust/kernel/sync/condvar.rs | 10 +++ 7 files changed, 272 insertions(+), 5 deletions(-)
drivers/android/context.rs +39 -0
@@ -69,6 +69,18 @@ pub(crate) struct ContextList { list: List<Context>, } +pub (crate )fn get_all_contexts () ->Result <Vec <Arc <Context >>> { +let lock =CONTEXTS .lock (); + +let count = lock.list.iter ().count (); + +let mut ctxs =Vec ::try_with_capacity (count)?; +for ctxin &lock.list { + ctxs.try_push (Arc ::from (ctx))?; + } +Ok (ctxs) +} +/// This struct keeps track of the processes using this context, and which process is the context /// manager. struct Manager {
@@ -183,4 +195,31 @@ pub(crate) fn get_manager_node(&self, strong: bool) -> Result<NodeRef, BinderErr .clone(strong) .map_err (BinderError ::from) } + +pub (crate )fn for_each_proc <F >(&self,mut func:F ) +where +F :FnMut (&Process ), + { +let lock = self.manager.lock (); +for procin &lock.all_procs { +func (&proc); + } + } + +pub (crate )fn get_all_procs (&self) ->Result <Vec <Arc <Process >>> { +let lock = self.manager.lock (); +let count = lock.all_procs.iter ().count (); + +let mut procs =Vec ::try_with_capacity (count)?; +for procin &lock.all_procs { + procs.try_push (Arc ::from (proc))?; + } +Ok (procs) + } + +pub (crate )fn get_procs_with_pid (&self, pid:i32 ) ->Result <Vec <Arc <Process >>> { +let mut procs = self.get_all_procs ()?; + procs.retain (|proc| proc.task.pid () == pid); +Ok (procs) + } }
drivers/android/defs.rs +3 -0
@@ -20,6 +20,7 @@ macro_rules! pub_no_prefix { BR_REPLY, BR_DEAD_REPLY, BR_FAILED_REPLY, + BR_FROZEN_REPLY, BR_NOOP, BR_SPAWN_LOOPER, BR_TRANSACTION_COMPLETE,
@@ -120,6 +121,8 @@ fn default() -> Self { );decl_wrapper! (BinderWriteRead , bindings::binder_write_read);decl_wrapper! (BinderVersion , bindings::binder_version); +decl_wrapper! (BinderFrozenStatusInfo , bindings::binder_frozen_status_info); +decl_wrapper! (BinderFreezeInfo , bindings::binder_freeze_info);decl_wrapper! (ExtendedError , bindings::binder_extended_error);impl BinderVersion {
drivers/android/error.rs +8 -0
@@ -21,6 +21,13 @@ pub(crate) fn new_dead() -> Self { } } +pub (crate )fn new_frozen () ->Self { +Self { + reply:BR_FROZEN_REPLY , + source:None , + } + } +pub (crate )fn is_dead (&self) ->bool { self.reply ==BR_DEAD_REPLY }
@@ -76,6 +83,7 @@ fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { None => f.pad("BR_FAILED_REPLY"), }, BR_DEAD_REPLY => f.pad("BR_DEAD_REPLY"), + BR_FROZEN_REPLY => f.pad("BR_FROZEN_REPLY"), BR_TRANSACTION_COMPLETE => f.pad("BR_TRANSACTION_COMPLETE"), _ => f .debug_struct ("BinderError" )
drivers/android/process.rs +154 -1
215 lines not on this page · raw message
drivers/android/thread.rs +10 -2
@@ -458,6 +458,10 @@ pub(crate) fn set_current_transaction(&self, transaction: DArc<Transaction>) { self.inner.lock ().current_transaction =Some (transaction); } +pub (crate )fn has_current_transaction (&self) ->bool { + self.inner.lock ().current_transaction.is_some () + } +/// Attempts to fetch a work item from the thread-local queue. The behaviour if the queue is /// empty depends on `wait`: if it is true, the function waits for some work to be queued (or a /// signal); otherwise it returns indicating that none is available.
@@ -482,7 +486,7 @@ fn get_work_local(self: &Arc<Self>, wait: bool) -> Result<Option<DLArc<dyn Deliv } inner.looper_flags |=LOOPER_WAITING ; -let signal_pending = self.work_condvar.wait (&mut inner); +let signal_pending = self.work_condvar.wait_freezable (&mut inner); inner.looper_flags &= !LOOPER_WAITING ;if signal_pending {
@@ -533,7 +537,7 @@ fn get_work(self: &Arc<Self>, wait: bool) -> Result<Option<DLArc<dyn DeliverToRe } inner.looper_flags |=LOOPER_WAITING |LOOPER_WAITING_PROC ; -let signal_pending = self.work_condvar.wait (&mut inner); +let signal_pending = self.work_condvar.wait_freezable (&mut inner); inner.looper_flags &= !(LOOPER_WAITING |LOOPER_WAITING_PROC );if signal_pending || inner.looper_need_return {
@@ -1043,6 +1047,10 @@ fn deliver_single_reply( reply: Either<DLArc<Transaction>, u32>, transaction: &DArc<Transaction>, ) -> bool { +if let Either ::Left (transaction) = &reply { + transaction.set_outstanding (&mut self.process.inner.lock ()); + } + {let mut inner = self.inner.lock ();if !inner.pop_transaction_replied (transaction) {
drivers/android/transaction.rs +48 -2
125 lines not on this page · raw message
rust/kernel/sync/condvar.rs +10 -0
@@ -191,6 +191,16 @@ pub fn wait<T: ?Sized, B: Backend>(&self, guard: &mut Guard<'_, T, B>) -> bool {crate ::current!().signal_pending () } +/// Releases the lock and waits for a notification in interruptible and freezable mode. +#[must_use = "wait returns if a signal is pending, so the caller must check the return value" ] +pub fn wait_freezable <T : ?Sized ,B :Backend >(&self, guard: &mut Guard <'_,T ,B >) ->bool { + self.wait_internal ( + bindings::TASK_INTERRUPTIBLE | bindings::TASK_FREEZABLE , + guard, + ); +crate ::current!().signal_pending () + } +/// Releases the lock and waits for a notification in uninterruptible mode. /// /// Similar to [`CondVar::wait`], except that the wait is not interruptible. That is, the
Signature · 1 line
2.42.0.820.g83a721a137-goog
Alice Ryhl
maintainerRFC PATCH 16/20 rust_binder: add TF_UPDATE_TXN support
In reply to Alice RyhlWhen a process is frozen, incoming oneway transactions are held in a queue until the process is unfrozen. If many oneway transactions are sent, then the process could run out of space for them. This patch adds a flag that avoids this by replacing previous oneway transactions in the queue to avoid having transactions of the same type build up. This can be useful when only the most recent transaction is necessary.
--- drivers/android/defs.rs | 8 +++++++- drivers/android/node.rs | 19 +++++++++++++++++++ drivers/android/transaction.rs | 26 ++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-)
drivers/android/defs.rs +7 -1
@@ -58,7 +58,13 @@ macro_rules! pub_no_prefix {pub (crate )const FLAT_BINDER_FLAG_ACCEPTS_FDS :u32 = kernel::bindings::FLAT_BINDER_FLAG_ACCEPTS_FDS ;pub (crate )const FLAT_BINDER_FLAG_TXN_SECURITY_CTX :u32 = kernel::bindings::FLAT_BINDER_FLAG_TXN_SECURITY_CTX ; -pub_no_prefix! (transaction_flags_,TF_ONE_WAY ,TF_ACCEPT_FDS ,TF_CLEAR_BUF ); +pub_no_prefix! ( + transaction_flags_, +TF_ONE_WAY , +TF_ACCEPT_FDS , +TF_CLEAR_BUF , +TF_UPDATE_TXN +);pub (crate )use bindings::{BINDER_TYPE_BINDER ,BINDER_TYPE_FD ,BINDER_TYPE_FDA ,BINDER_TYPE_HANDLE ,BINDER_TYPE_PTR ,
drivers/android/node.rs +19 -0
@@ -298,6 +298,25 @@ pub(crate) fn pending_oneway_finished(&self) { } } } + +/// Finds an outdated transaction that the given transaction can replace. +/// +/// If one is found, it is removed from the list and returned. +pub (crate )fn take_outdated_transaction ( + &self, + new: &Transaction , + guard: &mut Guard <'_,ProcessInner ,SpinLockBackend >, + ) ->Option <DLArc <Transaction >> { +let inner = self.inner.access_mut (guard); +let mut cursor_opt = inner.oneway_todo.cursor_front (); +while let Some (cursor) = cursor_opt { +if new.can_replace (&cursor.current ()) { +return Some (cursor.remove ()); + } + cursor_opt = cursor.next (); + } +None + } }impl DeliverToRead for Node {
drivers/android/transaction.rs +26 -0
@@ -201,6 +201,9 @@ fn drop_outstanding_txn(&self) {/// /// Not used for replies. pub (crate )fn submit (self:DLArc <Self >) ->BinderResult { +// Defined before `process_inner` so that the destructor runs after releasing the lock. +let mut _t_outdated =None ; +let oneway = self.flags &TF_ONE_WAY !=0 ;let process = self.to.clone ();let mut process_inner = process.inner.lock ();
@@ -211,6 +214,10 @@ pub(crate) fn submit(self: DLArc<Self>) -> BinderResult {if let Some (target_node) = self.target_node.clone () {if process_inner.is_frozen { process_inner.async_recv =true ; +if self.flags &TF_UPDATE_TXN !=0 { + _t_outdated = + target_node.take_outdated_transaction (&self, &mut process_inner); + } }match target_node.submit_oneway (self, &mut process_inner) {Ok (()) =>return Ok (()),
@@ -251,6 +258,25 @@ pub(crate) fn submit(self: DLArc<Self>) -> BinderResult { } } +/// Check whether one oneway transaction can supersede another. +pub (crate )fn can_replace (&self, old: &Transaction ) ->bool { +if self.from.process.task.pid () != old.from.process.task.pid () { +return false ; + } + +if self.flags & old.flags & (TF_ONE_WAY |TF_UPDATE_TXN ) != (TF_ONE_WAY |TF_UPDATE_TXN ) { +return false ; + } + +let target_node_match =match (self.target_node.as_ref (), old.target_node.as_ref ()) { + (None ,None ) =>true , + (Some (tn1),Some (tn2)) =>Arc ::ptr_eq (tn1, tn2), + _ =>false , + }; + + self.code == old.code && self.flags == old.flags && target_node_match + } +fn prepare_file_list (&self) ->Result <TranslatedFds > {let mut alloc = self.allocation.lock ().take ().ok_or (ESRCH )?;
Signature · 1 line
2.42.0.820.g83a721a137-goog
Alice Ryhl
maintainerRFC PATCH 17/20 rust_binder: add oneway spam detection
In reply to Alice RyhlFrom: Matt Gilbride <[email protected]>
The idea is that once we cross a certain threshold of free async space, whoever is responsible for the low async space is likely to try to send another async transaction.
This change allows servers to turn on oneway spam detection and return a different binder reply when it is detected.
--- drivers/android/allocation.rs | 3 ++ drivers/android/defs.rs | 1 + drivers/android/process.rs | 39 ++++++++++++++++++++++++-- drivers/android/range_alloc.rs | 62 ++++++++++++++++++++++++++++++++++++++++-- drivers/android/rust_binder.rs | 1 - drivers/android/thread.rs | 11 ++++++-- drivers/android/transaction.rs | 5 ++++ rust/kernel/task.rs | 2 +- 8 files changed, 115 insertions(+), 9 deletions(-)
drivers/android/allocation.rs +3 -0
@@ -49,6 +49,7 @@ pub(crate) struct Allocation { pub(crate) process: Arc<Process>, allocation_info: Option<AllocationInfo>, free_on_drop: bool, + pub(crate) oneway_spam_detected: bool, }impl Allocation {
@@ -58,6 +59,7 @@ pub(crate) fn new( size: usize, ptr: usize, pages: Arc<Vec<Pages<0>>>, + oneway_spam_detected: bool, ) -> Self { Self { process,
@@ -65,6 +67,7 @@ pub(crate) fn new( size, ptr, pages, + oneway_spam_detected, allocation_info: None, free_on_drop: true, }
drivers/android/defs.rs +1 -0
@@ -24,6 +24,7 @@ macro_rules! pub_no_prefix { BR_NOOP, BR_SPAWN_LOOPER, BR_TRANSACTION_COMPLETE, + BR_ONEWAY_SPAM_SUSPECT, BR_OK, BR_ERROR, BR_INCREFS,
drivers/android/process.rs +36 -3
@@ -92,6 +92,8 @@ pub(crate) struct ProcessInner { pub(crate) sync_recv: bool,/// Process received async transactions since last frozen. pub(crate) async_recv: bool, +/// Check for oneway spam + oneway_spam_detection_enabled: bool, }impl ProcessInner {
@@ -113,6 +115,7 @@ fn new() -> Self { is_frozen: false, sync_recv: false, async_recv: false, + oneway_spam_detection_enabled: false, } }
@@ -658,17 +661,21 @@ pub(crate) fn buffer_alloc( self: &Arc<Self>, size: usize, is_oneway: bool, + from_pid: i32, ) -> BinderResult<Allocation> {let alloc = range_alloc::ReserveNewBox ::try_new ()?;let mut inner = self.inner.lock ();let mapping = inner.mapping.as_mut ().ok_or_else (BinderError ::new_dead)?; -let offset = mapping.alloc.reserve_new (size, is_oneway, alloc)?; +let offset = mapping + .alloc + .reserve_new (size, is_oneway, from_pid, alloc)?;Ok (Allocation ::new ( self.clone (), offset, size, mapping.address + offset, mapping.pages.clone (), + mapping.alloc.oneway_spam_detected, )) }
@@ -677,7 +684,14 @@ pub(crate) fn buffer_get(self: &Arc<Self>, ptr: usize) -> Option<Allocation> {let mapping = inner.mapping.as_mut ()?;let offset = ptr.checked_sub (mapping.address)?;let (size, odata) = mapping.alloc.reserve_existing (offset).ok ()?; -let mut alloc =Allocation ::new (self.clone (), offset, size, ptr, mapping.pages.clone ()); +let mut alloc =Allocation ::new ( + self.clone (), + offset, + size, + ptr, + mapping.pages.clone (), + mapping.alloc.oneway_spam_detected, + );if let Some (data) = odata { alloc.set_info (data); }
@@ -762,6 +776,14 @@ fn set_max_threads(&self, max: u32) { self.inner.lock ().max_threads = max; } +fn set_oneway_spam_detection_enabled (&self, enabled:u32 ) { + self.inner.lock ().oneway_spam_detection_enabled = enabled !=0 ; + } + +pub (crate )fn is_oneway_spam_detection_enabled (&self) ->bool { + self.inner.lock ().oneway_spam_detection_enabled + } +fn get_node_debug_info (&self, data:UserSlicePtr ) ->Result {let (mut reader,mut writer) = data.reader_writer ();
@@ -948,9 +970,17 @@ fn deferred_release(self: Arc<Self>) {if let Some (mut mapping) = omapping {let address = mapping.address;let pages = mapping.pages.clone (); +let oneway_spam_detected = mapping.alloc.oneway_spam_detected; mapping.alloc.take_for_each (|offset, size, odata| {let ptr = offset + address; -let mut alloc =Allocation ::new (self.clone (), offset, size, ptr, pages.clone ()); +let mut alloc =Allocation ::new ( + self.clone (), + offset, + size, + ptr, + pages.clone (), + oneway_spam_detected, + );if let Some (data) = odata { alloc.set_info (data); }
@@ -1144,6 +1174,9 @@ fn write( bindings::BINDER_SET_CONTEXT_MGR_EXT => { this.set_as_manager(Some(reader.read()?), &thread)? } + bindings::BINDER_ENABLE_ONEWAY_SPAM_DETECTION => { + this.set_oneway_spam_detection_enabled(reader.read()?) + } bindings::BINDER_FREEZE => ioctl_freeze(reader)?, _ => return Err(EINVAL), }
drivers/android/range_alloc.rs +60 -2
138 lines not on this page · raw message
drivers/android/rust_binder.rs +0 -1
@@ -107,7 +107,6 @@ fn new(val: impl PinInit<T>) -> impl PinInit<Self> { }) } -#[allow(dead_code)] fn arc_try_new (val:T ) ->Result <DLArc <T >, alloc::alloc::AllocError > { ListArc::pin_init(pin_init!(Self { links <- ListLinksSelfPtr::new(),
drivers/android/thread.rs +9 -2
@@ -909,7 +909,7 @@ pub(crate) fn copy_transaction_data( size_of::<usize>(), );let secctx_off = adata_size + aoffsets_size + abuffers_size; -let mut alloc =match to_process.buffer_alloc (len, is_oneway) { +let mut alloc =match to_process.buffer_alloc (len, is_oneway, self.process.task.pid ()) {Ok (alloc) => alloc,Err (err) => {pr_warn! (
@@ -1191,8 +1191,15 @@ fn oneway_transaction_inner(self: &Arc<Self>, tr: &BinderTransactionDataSg) -> Blet handle =unsafe { tr.transaction_data.target.handle };let node_ref = self.process.get_transaction_node (handle)?; security::binder_transaction (&self.process.cred, &node_ref.node.owner.cred)?; -let list_completion =DTRWrap ::arc_try_new (DeliverCode ::new (BR_TRANSACTION_COMPLETE ))?;let transaction =Transaction ::new (node_ref,None , self, tr)?; +let code =if self.process.is_oneway_spam_detection_enabled () + && transaction.oneway_spam_detected + { +BR_ONEWAY_SPAM_SUSPECT + }else { +BR_TRANSACTION_COMPLETE + }; +let list_completion =DTRWrap ::arc_try_new (DeliverCode ::new (code))?;let completion = list_completion.clone_arc (); self.inner.lock ().push_work (list_completion);match transaction.submit () {
drivers/android/transaction.rs +5 -0
@@ -38,6 +38,7 @@ pub(crate) struct Transaction { data_address: usize, sender_euid: Kuid, txn_security_ctx_off: Option<usize>, + pub(crate) oneway_spam_detected: bool, } kernel::list::impl_list_arc_safe! {
@@ -70,6 +71,7 @@ pub(crate) fn new(return Err (err); } }; +let oneway_spam_detected = alloc.oneway_spam_detected;if trd.flags &TF_ONE_WAY !=0 {if stack_next.is_some () {pr_warn! ("Oneway transaction should not be in a transaction stack." );
@@ -98,6 +100,7 @@ pub(crate) fn new( allocation <- kernel::new_spinlock!(Some(alloc), "Transaction::new"), is_outstanding: AtomicBool::new(false), txn_security_ctx_off, + oneway_spam_detected, }))?) }
@@ -115,6 +118,7 @@ pub(crate) fn new_reply(return Err (err); } }; +let oneway_spam_detected = alloc.oneway_spam_detected;if trd.flags &TF_CLEAR_BUF !=0 { alloc.set_info_clear_on_drop (); }
@@ -132,6 +136,7 @@ pub(crate) fn new_reply( allocation <- kernel::new_spinlock!(Some(alloc), "Transaction::new"), is_outstanding: AtomicBool::new(false), txn_security_ctx_off: None, + oneway_spam_detected, }))?) }
rust/kernel/task.rs +1 -1
@@ -81,7 +81,7 @@ unsafe impl Send for Task {}unsafe impl Sync for Task {}/// The type of process identifiers (PIDs). -type Pid = bindings::pid_t ; +pub type Pid = bindings::pid_t ;/// The type of user identifiers (UIDs). #[derive( Copy , Clone )]
Signature · 1 line
2.42.0.820.g83a721a137-goog
Alice Ryhl
maintainerRFC PATCH 18/20 rust_binder: add binder_logs/state
In reply to Alice RyhlThe binderfs directory has four files intended for debugging the driver. This patch implements the state file so that you can use it to view the current state of the driver.
--- drivers/android/node.rs | 37 ++++++++++++++++++++ drivers/android/process.rs | 75 +++++++++++++++++++++++++++++++++++++++++ drivers/android/rust_binder.rs | 23 ++++++++++++- drivers/android/thread.rs | 68 +++++++++++++++++++++++++++++++++++++ drivers/android/transaction.rs | 25 ++++++++++++++ rust/bindings/bindings_helper.h | 1 + rust/kernel/lib.rs | 1 + rust/kernel/seq_file.rs | 47 ++++++++++++++++++++++++++ 8 files changed, 276 insertions(+), 1 deletion(-)
drivers/android/node.rs +37 -0
@@ -7,6 +7,8 @@ TryNewListArc, }, prelude::*, + seq_file::SeqFile, + seq_print, sync::lock::{spinlock::SpinLockBackend, Guard}, sync::{Arc, LockedBy, SpinLock}, user_ptr::UserSlicePtrWriter,
@@ -111,6 +113,41 @@ pub(crate) fn new( }) } +#[inline(never)] +pub (crate )fn debug_print (&self, m: &mut SeqFile ) ->Result <()> { +let weak; +let strong; +let has_weak; +let has_strong; +let active_inc_refs; + { +let mut guard = self.owner.inner.lock (); +let inner = self.inner.access_mut (&mut guard); + weak = inner.weak.count; + has_weak = inner.weak.has_count; + strong = inner.strong.count; + has_strong = inner.strong.has_count; + active_inc_refs = inner.active_inc_refs; + } + +let has_weak =if has_weak {"Y" }else {"N" }; +let has_strong =if has_strong {"Y" }else {"N" }; + +seq_print! ( + m, +"node gid:{},ptr:{:#x},cookie:{:#x}: strong{}{} weak{}{} active{}\n" , + self.global_id, + self.ptr, + self.cookie, + strong, + has_strong, + weak, + has_weak, + active_inc_refs + ); +Ok (()) + } +pub (crate )fn get_id (&self) -> (usize ,usize ) { (self.ptr, self.cookie) }
drivers/android/process.rs +75 -0
87 lines not on this page · raw message
drivers/android/rust_binder.rs +22 -1
@@ -10,6 +10,8 @@ HasListLinks, ListArc, ListArcSafe, ListItem, ListLinks, ListLinksSelfPtr, TryNewListArc, }, prelude::*, + seq_file::SeqFile, + seq_print, sync::Arc, types::ForeignOwnable, user_ptr::UserSlicePtrWriter,
@@ -347,7 +349,13 @@ unsafe impl<T> Sync for AssertSync<T> {} }#[no_mangle] -unsafe extern "C" fn rust_binder_state_show (_: *mut seq_file ) -> core::ffi::c_int { +unsafe extern "C" fn rust_binder_state_show (ptr: *mut seq_file ) -> core::ffi::c_int { +// SAFETY: The caller ensures that the pointer is valid and exclusive for the duration in which +// this method is called. +let m =unsafe {SeqFile ::from_raw (ptr) }; +if let Err (err) =rust_binder_state_show_impl (m) { +seq_print! (m,"failed to generate state: {:?}\n" , err); + }0 }
@@ -360,3 +368,16 @@ unsafe impl<T> Sync for AssertSync<T> {}unsafe extern "C" fn rust_binder_transaction_log_show (_: *mut seq_file ) -> core::ffi::c_int {0 } + +fn rust_binder_state_show_impl (m: &mut SeqFile ) ->Result <()> { +let contexts = context::get_all_contexts ()?; +for ctxin contexts { +let procs = ctx.get_all_procs ()?; +seq_print! (m,"context {}: ({} processes)\n" , &*ctx.name, procs.len ()); +for procin procs { + proc.debug_print (m)?; +seq_print! (m,"\n" ); + } + } +Ok (()) +}
drivers/android/thread.rs +68 -0
80 lines not on this page · raw message
drivers/android/transaction.rs +25 -0
@@ -5,6 +5,8 @@ io_buffer::IoBufferWriter, list::ListArcSafe, prelude::*, + seq_file::SeqFile, + seq_print, sync::{Arc, SpinLock}, task::Kuid, types::{Either, ScopeGuard},
@@ -140,6 +142,29 @@ pub(crate) fn new_reply( }))?) } +#[inline(never)] +pub (crate )fn debug_print (&self, m: &mut SeqFile ) { +let from_pid = self.from.process.task.pid_in_current_ns (); +let to_pid = self.to.task.pid_in_current_ns (); +let from_tid = self.from.id; +match self.target_node.as_ref () { +Some (target_node) => { +let node_id = target_node.global_id; +seq_print! ( + m, +"{}(tid:{})->{}(nid:{})" , + from_pid, + from_tid, + to_pid, + node_id + ); + } +None => { +seq_print! (m,"{}(tid:{})->{}(nid:_)" , from_pid, from_tid, to_pid); + } + } + } +/// Determines if the transaction is stacked on top of the given transaction. pub (crate )fn is_stacked_on (&self, onext: &Option <DArc <Self >>) ->bool {match (&self.stack_next, onext) {
rust/bindings/bindings_helper.h +1 -0
@@ -15,6 +15,7 @@#include <linux/pid_namespace.h> #include <linux/poll.h> #include <linux/security.h> +#include <linux/seq_file.h> #include <linux/slab.h> #include <linux/refcount.h> #include <linux/rust_binder.h>
rust/kernel/lib.rs +1 -0
@@ -48,6 +48,7 @@pub mod print;pub mod rbtree;pub mod security; +pub mod seq_file;mod static_assert;#[doc(hidden)] pub mod std_vendor;
rust/kernel/seq_file.rs new file +47 -0
47 lines not on this page · raw message
Signature · 1 line
2.42.0.820.g83a721a137-goog
Alice Ryhl
maintainerRFC PATCH 19/20 rust_binder: add vma shrinker
In reply to Alice RyhlWhen the system is under memory pressure, we want the driver to release unused pages. We do this by registering a memory shrinker with the kernel.
The data for incoming transactions is stored in an mmap'ed region. Previously in this patch series, we just allocated all of the pages in that region immediately. With this patch series, we do not allocate the pages until we need them.
Furthermore, when we no longer need a page, we mark it as "available" using an lru list. If the system is under memory pressure, this allows the shrinker to free that page by removing it from the lru list. If we need to use the page again before the shrinker frees it, then we just remove it from the lru list, and we don't need to reallocate the page.
The page range abstraction is split into a fast path and slow path. The slow path is only used when a page is not allocated, which should only happen on first use, and when the system is under memory pressure.
I'm not yet completely happy with this implementation. Specifically, I would like to improve the robustness of the unsafe code found in allocation.rs.
The slow-path/fast-path implementation in page_range.rs is different from C Binder's current implementation, and was suggested to me by Carlos.
--- drivers/android/allocation.rs | 80 ++--- drivers/android/process.rs | 129 +++---- drivers/android/range_alloc.rs | 44 ++- drivers/android/rust_binder.rs | 6 + rust/bindings/bindings_helper.h | 2 + rust/helpers.c | 20 ++ rust/kernel/lib.rs | 1 + rust/kernel/page_range.rs | 715 ++++++++++++++++++++++++++++++++++++++ rust/kernel/sync/lock.rs | 24 ++ rust/kernel/sync/lock/mutex.rs | 10 + rust/kernel/sync/lock/spinlock.rs | 10 + 11 files changed, 931 insertions(+), 110 deletions(-)
drivers/android/allocation.rs +32 -48
@@ -6,7 +6,6 @@ bindings, file::{DeferredFdCloser, File, FileDescriptorReservation}, io_buffer::{IoBufferReader, ReadableFromBytes, WritableToBytes}, - pages::Pages, prelude::*, sync::Arc, types::ARef,
@@ -41,11 +40,15 @@ pub(crate) struct AllocationInfo {/// Represents an allocation that the kernel is currently using. /// /// When allocations are idle, the range allocator holds the data related to them. +/// +/// # Invariants +/// +/// This allocation corresponds to an allocation in the range allocator, so the relevant pages are +/// marked in use in the page range. pub (crate )struct Allocation {pub (crate ) offset:usize , size:usize ,pub (crate ) ptr:usize , - pages:Arc <Vec <Pages <0 >>>,pub (crate ) process:Arc <Process >, allocation_info:Option <AllocationInfo >, free_on_drop:bool ,
@@ -58,7 +61,6 @@ pub(crate) fn new( offset: usize, size: usize, ptr: usize, - pages: Arc<Vec<Pages<0>>>, oneway_spam_detected: bool, ) -> Self { Self {
@@ -66,30 +68,17 @@ pub(crate) fn new( offset, size, ptr, - pages, oneway_spam_detected, allocation_info: None, free_on_drop: true, } } -fn iterate <T >(&self,mut offset:usize ,mut size:usize ,mut cb:T ) ->Result -where -T :FnMut (&Pages <0 >,usize ,usize ) ->Result , - { -// Check that the request is within the buffer. -if offset.checked_add (size).ok_or (EINVAL )? > self.size { -return Err (EINVAL ); - } - offset += self.offset; -let mut page_index = offset >> bindings::PAGE_SHIFT ; - offset &= (1 << bindings::PAGE_SHIFT ) -1 ; -while size >0 { -let available = core::cmp::min (size, (1 << bindings::PAGE_SHIFT ) - offset); -cb (&self.pages[page_index], offset, available)?; - size -= available; - page_index +=1 ; - offset =0 ; +fn size_check (&self, offset:usize , size:usize ) ->Result { +let overflow_fail = offset.checked_add (size).is_none (); +let cmp_size_fail = offset.wrapping_add (size) > self.size; +if overflow_fail || cmp_size_fail { +return Err (EFAULT ); }Ok (()) }
@@ -100,42 +89,37 @@ pub(crate) fn copy_into( offset: usize, size: usize, ) -> Result { - self.iterate(offset, size, |page, offset, to_copy| { - page.copy_into_page (reader, offset, to_copy) - }) + self.size_check(offset, size)?; + +// SAFETY: While this object exists, the range allocator will keep the range allocated, and +// in turn, the pages will be marked as in use. +unsafe { + self.process + .pages + .copy_into (reader, self.offset + offset, size) + } }pub (crate )fn read <T :ReadableFromBytes >(&self, offset:usize ) ->Result <T > { -let mut out =MaybeUninit ::<T >::uninit (); -let mut out_offset =0 ; - self.iterate (offset,size_of ::<T >(), |page, offset, to_copy| { -// SAFETY: The sum of `offset` and `to_copy` is bounded by the size of T. -let obj_ptr =unsafe { (out.as_mut_ptr ()as *mut u8 ).add (out_offset) }; -// SAFETY: The pointer points is in-bounds of the `out` variable, so it is valid. -unsafe { page.read (obj_ptr, offset, to_copy) }?; - out_offset += to_copy; -Ok (()) - })?; -// SAFETY: We just initialised the data. -Ok (unsafe { out.assume_init () }) + self.size_check (offset,size_of ::<T >())?; + +// SAFETY: While this object exists, the range allocator will keep the range allocated, and +// in turn, the pages will be marked as in use. +unsafe { self.process.pages.read (self.offset + offset) } }pub (crate )fn write <T : ?Sized >(&self, offset:usize , obj: &T ) ->Result { -let mut obj_offset =0 ; - self.iterate (offset,size_of_val (obj), |page, offset, to_copy| { -// SAFETY: The sum of `offset` and `to_copy` is bounded by the size of T. -let obj_ptr =unsafe { (objas *const T as *const u8 ).add (obj_offset) }; -// SAFETY: We have a reference to the object, so the pointer is valid. -unsafe { page.write (obj_ptr, offset, to_copy) }?; - obj_offset += to_copy; -Ok (()) - }) + self.size_check (offset,size_of_val ::<T >(obj))?; + +// SAFETY: While this object exists, the range allocator will keep the range allocated, and +// in turn, the pages will be marked as in use. +unsafe { self.process.pages.write (self.offset + offset, obj) } }pub (crate )fn fill_zero (&self) ->Result { - self.iterate (0 , self.size, |page, offset, len| { - page.fill_zero (offset, len) - }) +// SAFETY: While this object exists, the range allocator will keep the range allocated, and +// in turn, the pages will be marked as in use. +unsafe { self.process.pages.fill_zero (self.offset, self.size) } }pub (crate )fn keep_alive (mut self) {
drivers/android/process.rs +69 -60
207 lines not on this page · raw message
drivers/android/range_alloc.rs +42 -2
77 lines not on this page · raw message
drivers/android/rust_binder.rs +6 -0
@@ -9,6 +9,7 @@ list::{ HasListLinks, ListArc, ListArcSafe, ListItem, ListLinks, ListLinksSelfPtr, TryNewListArc, }, + page_range::Shrinker, prelude::*, seq_file::SeqFile, seq_print,
@@ -173,12 +174,17 @@ const fn ptr_align(value: usize) -> usize { (value + size) & !size } +// SAFETY: We call register in `init`. +static BINDER_SHRINKER :Shrinker =unsafe {Shrinker ::new () }; +struct BinderModule {}impl kernel::Module for BinderModule {fn init (_module: &'static kernel::ThisModule ) ->Result <Self > {crate ::context::CONTEXTS .init (); +BINDER_SHRINKER .register (kernel::c_str!("android-binder" ))?; +// SAFETY: The module is being loaded, so we can initialize binderfs. #[cfg( CONFIG_ANDROID_BINDERFS_RUST )] unsafe {
rust/bindings/bindings_helper.h +2 -0
@@ -12,6 +12,7 @@#include <linux/fdtable.h> #include <linux/file.h> #include <linux/fs.h> +#include <linux/list_lru.h> #include <linux/pid_namespace.h> #include <linux/poll.h> #include <linux/security.h>
@@ -21,6 +22,7 @@#include <linux/rust_binder.h> #include <linux/wait.h> #include <linux/sched.h> +#include <linux/shrinker.h> #include <linux/task_work.h> #include <linux/workqueue.h> #include <uapi/linux/android/binder.h>
rust/helpers.c +20 -0
@@ -93,6 +93,12 @@ void rust_helper_spin_unlock(spinlock_t *lock) }EXPORT_SYMBOL_GPL (rust_helper_spin_unlock); +int rust_helper_spin_trylock (spinlock_t *lock) +{ +return spin_trylock (lock); +} +EXPORT_SYMBOL_GPL (rust_helper_spin_trylock); +void rust_helper_init_wait (struct wait_queue_entry *wq_entry) {init_wait (wq_entry);
@@ -310,6 +316,20 @@ struct vm_area_struct *rust_helper_vma_lookup(struct mm_struct *mm, }EXPORT_SYMBOL_GPL (rust_helper_vma_lookup); +unsigned long rust_helper_list_lru_count (struct list_lru *lru) +{ +return list_lru_count (lru); +} +EXPORT_SYMBOL_GPL (rust_helper_list_lru_count); + +unsigned long rust_helper_list_lru_walk (struct list_lru *lru, +list_lru_walk_cb isolate,void *cb_arg, +unsigned long nr_to_walk) +{ +return list_lru_walk (lru, isolate, cb_arg, nr_to_walk); +} +EXPORT_SYMBOL_GPL (rust_helper_list_lru_walk); +void rust_helper_rb_link_node (struct rb_node *node,struct rb_node *parent,struct rb_node **rb_link) {
rust/kernel/lib.rs +1 -0
@@ -43,6 +43,7 @@pub mod kunit;pub mod list;pub mod mm; +pub mod page_range;pub mod pages;pub mod prelude;pub mod print;
rust/kernel/page_range.rs new file +715 -0
715 lines not on this page · raw message
rust/kernel/sync/lock.rs +24 -0
@@ -51,6 +51,14 @@ unsafe fn init(#[must_use] unsafe fn lock (ptr: *mut Self ::State ) ->Self ::GuardState ; +/// Tries to acquire the lock, making the caller its owner. +/// +/// # Safety +/// +/// Callers must ensure that [`Backend::init`] has been previously called. +#[must_use] +unsafe fn trylock (ptr: *mut Self ::State ) ->Option <Self ::GuardState >; +/// Releases the lock, giving up its ownership. /// /// # Safety
@@ -121,6 +129,22 @@ pub fn lock(&self) -> Guard<'_, T, B> {// SAFETY: The lock was just acquired. unsafe {Guard ::new (self, state) } } + +/// Acquires the lock and gives the caller access to the data protected by it. +pub fn trylock (&self) ->Option <Guard <'_,T ,B >> { +// SAFETY: The constructor of the type calls `init`, so the existence of the object proves +// that `init` was called. +let state =unsafe {B ::trylock (self.state.get ())? }; +// SAFETY: The lock was just acquired. +unsafe {Some (Guard ::new (self, state)) } + } + +/// Get a raw pointer to the data without touching the lock. +/// +/// It is up to the user to make sure that the pointer is used correctly. +pub fn get_ptr (&self) -> *mut T { + self.data.get () + } }/// A lock guard.
rust/kernel/sync/lock/mutex.rs +10 -0
@@ -111,6 +111,16 @@ unsafe fn lock(ptr: *mut Self::State) -> Self::GuardState {unsafe { bindings::mutex_lock (ptr) }; } +unsafe fn trylock (ptr: *mut Self ::State ) ->Option <Self ::GuardState > { +// SAFETY: The safety requirements of this function ensure that `ptr` points to valid +// memory, and that it has been initialised before. +if unsafe { bindings::mutex_trylock (ptr) } !=0 { +Some (()) + }else { +None + } + } +unsafe fn unlock (ptr: *mut Self ::State , _guard_state: &Self ::GuardState ) {// SAFETY: The safety requirements of this function ensure that `ptr` is valid and that the // caller is the owner of the mutex.
rust/kernel/sync/lock/spinlock.rs +10 -0
@@ -110,6 +110,16 @@ unsafe fn lock(ptr: *mut Self::State) -> Self::GuardState {unsafe { bindings::spin_lock (ptr) } } +unsafe fn trylock (ptr: *mut Self ::State ) ->Option <Self ::GuardState > { +// SAFETY: The safety requirements of this function ensure that `ptr` points to valid +// memory, and that it has been initialised before. +if unsafe { bindings::spin_trylock (ptr) } !=0 { +Some (()) + }else { +None + } + } +unsafe fn unlock (ptr: *mut Self ::State , _guard_state: &Self ::GuardState ) {// SAFETY: The safety requirements of this function ensure that `ptr` is valid and that the // caller is the owner of the mutex.
Signature · 1 line
2.42.0.820.g83a721a137-goog
Alice Ryhl
maintainerRFC PATCH 20/20 binder: delete the C implementation
In reply to Alice RyhlThe ultimate goal of this project is to replace the C implementation.
--- drivers/android/Kconfig | 36 - drivers/android/binder.c | 6630 ---------------------------------------- drivers/android/binder_alloc.c | 1284 -------- drivers/android/binderfs.c | 827 ----- 4 files changed, 8777 deletions(-)
drivers/android/Kconfig +0 -36
@@ -1,18 +1,6 @@ # SPDX-License-Identifier: GPL-2.0 menu "Android" -config ANDROID_BINDER_IPC - bool "Android Binder IPC Driver" - depends on MMU - default n - help - Binder is used in Android for both communication between processes, - and remote method invocation. - - This means one Android process can call a method/routine in another - Android process, using Binder to identify, invoke and pass arguments - between said processes. - config ANDROID_BINDER_IPC_RUST bool "Android Binder IPC Driver in Rust" depends on MMU && RUST
@@ -24,18 +12,6 @@ config ANDROID_BINDER_IPC_RUST Android process, using Binder to identify, invoke and pass arguments between said processes. -config ANDROID_BINDERFS - bool "Android Binderfs filesystem" - depends on ANDROID_BINDER_IPC - default n - help - Binderfs is a pseudo-filesystem for the Android Binder IPC driver - which can be mounted per-ipc namespace allowing to run multiple - instances of Android. - Each binderfs mount initially only contains a binder-control device. - It can be used to dynamically allocate new binder IPC devices via - ioctls. - config ANDROID_BINDERFS_RUST bool "Android Binderfs filesystem in Rust" depends on ANDROID_BINDER_IPC_RUST
@@ -48,18 +24,6 @@ config ANDROID_BINDERFS_RUST It can be used to dynamically allocate new binder IPC devices via ioctls. -config ANDROID_BINDER_DEVICES - string "Android Binder devices" - depends on ANDROID_BINDER_IPC - default "binder,hwbinder,vndbinder" - help - Default value for the binder.devices parameter. - - The binder.devices parameter is a comma-separated list of strings - that specifies the names of the binder device nodes that will be - created. Each binder device has its own context manager, and is - therefore logically separated from the other devices. - config ANDROID_BINDER_DEVICES_RUST string "Android Binder devices in Rust" depends on ANDROID_BINDER_IPC_RUST
drivers/android/binder.c deleted +0 -6630
2,946 lines not on this page · raw message
drivers/android/binder_alloc.c deleted +0 -1284
1,284 lines not on this page · raw message
drivers/android/binderfs.c deleted +0 -827
827 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.42.0.820.g83a721a137-goog
Part of the diff is not shown here. View the raw message.
Re: [PATCH RFC 20/20] binder: delete the C implementation
Quoted 3 lines · Alice Ryhl
The ultimate goal of this project is to replace the C implementation.
Signed-off-by Alice Ryhl <[email protected]>
:)
Seriously, this is all great stuff, thanks for posting it, very impressive. Let's see how testing goes!
greg k-h
Re: [PATCH RFC 20/20] binder: delete the C implementation
In reply to Alice RyhlQuoted 3 lines · Alice Ryhl
The ultimate goal of this project is to replace the C implementation.
Signed-off-by Alice Ryhl <[email protected]>
Nice, great work Alice!
As far as logistics I think it would be nice to make the transition right after an LTS branch off. This should give the Rust binder more time to mature before landing on the subsequent LTS release.
--
Carlos Llamas
Carlos Llamas
maintainerQuoted 39 lines · Alice Ryhl
We're generally not proponents of rewrites (nasty uncomfortable things that make you late for dinner!). So why rewrite Binder?
Binder has been evolving over the past 15+ years to meet the evolving needs of Android. Its responsibilities, expectations, and complexity have grown considerably during that time. While we expect Binder to continue to evolve along with Android, there are a number of factors that currently constrain our ability to develop/maintain it. Briefly those are:
1. Complexity: Binder is at the intersection of everything in Android and fulfills many responsibilities beyond IPC. It has become many things to many people, and due to its many features and their interactions with each other, its complexity is quite high. In just 6kLOC it must deliver transactions to the right threads. It must correctly parse and translate the contents of transactions, which can contain several objects of different types (e.g., pointers, fds) that can interact with each other. It controls the size of thread pools in userspace, and ensures that transactions are assigned to threads in ways that avoid deadlocks where the threadpool has run out of threads. It must track refcounts of objects that are shared by several processes by forwarding refcount changes between the processes correctly. It must handle numerous error scenarios and it combines/nests 13 different locks, 7 reference counters, and atomic variables. Finally, It must do all of this as fast and efficiently as possible. Minor performance regressions can cause a noticeably degraded user experience.
2. Things to improve: Thousand-line functions [1], error-prone error handling [2], and confusing structure can occur as a code base grows organically. After more than a decade of development, this codebase could use an overhaul.
3. Security critical: Binder is a critical part of Android's sandboxing strategy. Even Android's most de-privileged sandboxes (e.g. the Chrome renderer, or SW Codec) have direct access to Binder. More than just about any other component, it's important that Binder provide robust security, and itself be robust against security vulnerabilities.
Quoted 5 lines
It's #1 (high complexity) that has made continuing to evolve Binder and resolving #2 (tech debt) exceptionally difficult without causing #3 (security issues). For Binder to continue to meet Android's needs, we need better ways to manage (and reduce!) complexity without increasing the risk.
I 100% agree with this as the vast majority of my time is spent chasing after memory corruption issues. The fixes here may look simple on the surface but the complexity makes them non-trivial and there are many "hidden" things to watch out for.
Quoted 10 lines
The biggest change is obviously the choice of programming language. We decided to use Rust because it directly addresses a number of the challenges within Binder that we have faced during the last years. It prevents mistakes with ref counting, locking, bounds checking, and also does a lot to reduce the complexity of error handling. Additionally, we've been able to use the more expressive type system to encode the ownership semantics of the various structs and pointers, which takes the complexity of managing object lifetimes out of the hands of the programmer, reducing the risk of use-after-frees and similar problems.
The history in binder teach us that is quite hard to write a patch with some confidence that it won't introduce one of these issues.
Quoted 22 lines
Rust has many different pointer types that it uses to encode ownership semantics into the type system, and this is probably one of the most important aspects of how it helps in Binder. The Binder driver has a lot of different objects that have complex ownership semantics; some pointers own a refcount, some pointers have exclusive ownership, and some pointers just reference the object and it is kept alive in some other manner. With Rust, we can use a different pointer type for each kind of pointer, which enables the compiler to enforce that the ownership semantics are implemented correctly.
Another useful feature is Rust's error handling. Rust allows for more simplified error handling with features such as destructors, and you get compilation failures if errors are not properly handled. This means that even though Rust requires you to spend more lines of code than C on things such as writing down invariants that are left implicit in C, the Rust driver is still slightly smaller than C binder: Rust is 5.5kLOC and C is 5.8kLOC. (These numbers are excluding blank lines, comments, binderfs, and any debugging facilities in C that are not yet implemented in the Rust driver. The numbers include abstractions in
rust/kernel/that are unlikely to be used by other drivers than Binder.)
Quoted 6 lines
Although this rewrite completely rethinks how the code is structured and how assumptions are enforced, we do not fundamentally change *how* the driver does the things it does. A lot of careful thought has gone into the existing design. The rewrite is aimed rather at improving code health, structure, readability, robustness, security, maintainability and extensibility. We also include more inline documentation, and
Can you expand a bit more on what the plan is here? Is it a two step process? e.g. replacing first and then revisiting the *how* binder does things later?
Quoted 97 lines
improve how assumptions in the code are enforced. Furthermore, all unsafe code is annotated with a SAFETY comment that explains why it is correct.
We have left the binderfs filesystem component in C. Rewriting it in Rust would be a large amount of work and requires a lot of bindings to the file system interfaces. Binderfs has not historically had the same challenges with security and complexity, so rewriting binderfs seems to have lower value than the rest of Binder.
Correctness and feature parity
------------------------------Rust binder passes all tests that validate the correctness of Binder in the Android Open Source Project. We can boot a device, and run a variety of apps and functionality without issues. We have performed this both on the Cuttlefish Android emulator device, and on a Pixel 6 Pro.
As for feature parity, Rust binder currently implements all features that C binder supports, with the exception of some debugging facilities. The missing debugging facilities will be added before we submit the Rust implementation upstream.
Performance numbers
-------------------We have tested the driver using two different benchmarks:
binderThroughputTest [3] and binderRpcBenchmark [4]. These benchmarks show that the Rust implementation has very promising performance characteristics. That said, these are only microbenchmarks with very simple workloads, and there is still a lot of work to be done before we can truly understand how the drivers compare in the real world.binderThroughputTest:
Some visualizations of the benchmarking results are available at the following links:Average latency with no payload: https://raw.githubusercontent.com/Darksonn/linux/rust-binder-rfc/img-for-rust-binder-rfc/Average%20latency%20with%20no%20payload.png
Average latency with 4k payload: https://raw.githubusercontent.com/Darksonn/linux/rust-binder-rfc/img-for-rust-binder-rfc/Average%20latency%20with%204k%20payload.png
99 percentile latency with no payload: https://raw.githubusercontent.com/Darksonn/linux/rust-binder-rfc/img-for-rust-binder-rfc/99%20percentile%20latency%20with%20no%20payload.png
99 percentile latency with 4k payload: https://raw.githubusercontent.com/Darksonn/linux/rust-binder-rfc/img-for-rust-binder-rfc/99%20percentile%20latency%20with%204k%20payload.pngRaw data with empty payloads:
+-----------+----------+---------+----------+---------+----------+----------+ | c/s pairs | Rust avg | C avg | Rust 99p | C 99p | Avg frac | 99p frac | +-----------+----------+---------+----------+---------+----------+----------+ | 1 | 17.517 | 17.278 | 31.169 | 34.464 | +1.38% | -9.56% | | 2 | 17.405 | 17.425 | 36.051 | 36.825 | -0.11% | -2.10% | | 4 | 27.623 | 27.524 | 46.305 | 45.776 | +0.36% | +1.16% | | 8 | 25.152 | 25.461 | 61.442 | 61.279 | -1.21% | +0.27% | | 16 | 50.251 | 49.987 | 120.158 | 121.297 | +0.53% | -0.94% | | 32 | 99.439 | 100.537 | 238.891 | 238.404 | -1.09% | +0.20% | +-----------+----------+---------+----------+---------+----------+----------+ Raw data with 4k payloads: +-----------+----------+---------+----------+---------+----------+----------+ | c/s pairs | Rust avg | C avg | Rust 99p | C 99p | Avg frac | 99p frac | +-----------+----------+---------+----------+---------+----------+----------+ | 1 | 19.422 | 19.811 | 30.233 | 31.616 | -1.96% | -4.37% | | 2 | 18.393 | 18.277 | 34.790 | 35.319 | +0.63% | -1.50% | | 4 | 29.350 | 29.283 | 48.544 | 47.730 | +0.23% | +1.71% | | 8 | 25.075 | 25.283 | 66.040 | 65.226 | -0.82% | +1.25% | | 16 | 58.608 | 58.949 | 156.657 | 159.709 | -0.58% | -1.91% | | 32 | 127.404 | 129.459 | 321.249 | 326.945 | -1.59% | -1.74% | +-----------+----------+---------+----------+---------+----------+----------+These tables depict roundtrip latencies of transactions as measured by binderThroughputTest. Each measurement is given in microseconds. Each row has a sample size of 10 million iterations. Negative percentages are better for Rust.
We've found that Rust binder has similar performance to C binder on the binderThroughputTest benchmark. The average latencies fluctuate between -1.96% and +1.38%.
binderRpcBenchmark:
+---------------------+-----------+---------+----------+---------+-----------+----------+ | Benchmark | Time Rust | Time C | CPU Rust | CPU C | Time frac | CPU frac | +---------------------+-----------+---------+----------+---------+-----------+----------+ | pingTransaction | 21.595 | 22.167 | 9.625 | 9.692 | -2.58% | -0.69% | | repeatBinder | 33.982 | 34.648 | 16.252 | 16.681 | -1.92% | -2.57% | | throughput/64 | 26.774 | 26.587 | 11.995 | 11.823 | +0.70% | +1.45% | | throughput/1024 | 33.679 | 33.867 | 15.140 | 15.137 | -0.56% | +0.02% | | throughput/2048 | 39.744 | 40.092 | 17.898 | 17.926 | -0.87% | -0.16% | | throughput/4096 | 52.585 | 53.457 | 23.788 | 24.067 | -1.63% | -1.16% | | throughput/8182 | 76.352 | 77.148 | 35.135 | 35.228 | -1.03% | -0.26% | | throughput/16364 | 121.875 | 122.877 | 57.342 | 57.614 | -0.82% | -0.47% | | throughput/32728 | 212.380 | 212.765 | 101.838 | 101.589 | -0.18% | +0.25% | | throughput/65535 | 442.983 | 421.935 | 222.642 | 212.494 | +4.99% | +4.78% | | throughput/65536 | 431.250 | 416.916 | 216.634 | 210.160 | +3.44% | +3.08% | | throughput/65537 | 512.902 | 492.272 | 242.472 | 232.786 | +4.19% | +4.16% | | repeatTwoPageString | 456.546 | 445.398 | 222.921 | 219.821 | +2.50% | +1.41% | +---------------------+-----------+---------+----------+---------+-----------+----------+This table depicts wall clock time and cpu time measurements over various test cases. Each measurement is given in microseconds. The throughput benchmarks correspond to the
BM_throughputForTransportAndBytestest case, and the number is the size of the payload. Negative percentages are better for Rust.
Quoted 5 lines
From the above, we find that Rust binder is competitive for all test cases except for those with very large transaction sizes. However, this is a very rare case in practice [5] and we've been able to fix all other performance issues that we've run into, so there's no reason to think that we won't also be able to fix this issue. We did not fix it for this
This isn't terrible. Besides being rare binder shouldn't really be used to send large transaction sizes. You are better off using something else in those scenarios e.g. shm. However, it would be nice to get to the bottom of this.
Quoted 7 lines
RFC because we prioritized getting the RFC out to provide context for the upcoming discussion at Linux Plumbers Conference [6].
We ran all of the benchmarks with cross-language LTO enabled, so that C code can be inlined into Rust code. We get similar results on the Cuttlefish Android emulator (which has an x86 architecture).
Quoted 5 lines
The Binder driver is very performance critical, and although our initial numbers are promising, we must gain a better understanding of how it performs in realistic workloads and not just in simple benchmarks. What we ultimately care about is the performance impact that it has on the whole system. Much work remains to be done on this front.
I can send a "stress" test that would tell us more information about the performance in critical scenarios. Perhaps this can land as kselftest.
Quoted 127 lines
Dependencies
------------When implementing kernel drivers in Rust, you must write bindings for each subsystem that we need to call into from Rust. Binder requires quite a few of them. We have not included them in this patch series, but you can view them at the following branch:
https://github.com/Darksonn/linux/commits/rust-binder-rfc
The branch is based on top of commit
639409a4ac8e("Merge tag 'wq-for-6.7-rust-bindings' ofgit://git.kernel.org/pub/scm/linux/kernel/git/tj/wq"),
which is available in mainline. I did not base it on a tag, since there is not yet any tag that includes the Rust workqueue abstractions.This RFC uses the kernel's red-black tree for key/value mappings, but we are aware that the red-black tree is deprecated. We did this to make the performance comparison more fair, since C binder also uses rbtree for this. We intend to replace these with XArrays instead. That said, we don't think that XArray is a good fit for the range allocator, and we propose to continue using the red-black tree for the range allocator. (see patch 6)
Thank you
---------* Wedson Almeida Filho who wrote the first version of the driver and started the project.
* Miguel Ojeda for his support, and leading the Rust-for-Linux effort, and helping us navigate the upstream community.
* Matt Gilbride for his work on the range allocator and oneway spam detection.
* Carlos Llamas for patiently answering all my questions to help me understand the C driver, and co-presenting with me at LPC and Kangrejos.
* Greg KH for reviews and guidance on upstream development.
* Todd Kjos for reviewing the cover letter, answering questions, and pointers on benchmarking the driver.
* Matthew Maurer for his mentorship and help with navigating the build system, including getting LTO working.
* John Stultz for his help with debugging a performance issue.
* Andreas Hindborg for his help with getting LTO working.
* Benno Lossin, Gary Guo, Andreas Hindborg, Miguel Ojeda, Wedson Almeida Filho, Martin Rodriguez Reboredo, Björn Roy Baron, Boqun Feng, Tejun Heo, Nathan Huckleberry for reviewing various bindings needed by Binder.
Thank you,
Alice[1]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/android/binder.c?h=v6.5#n2896
[2]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/android/binder.c?h=v6.5#n3658
[3]: https://android-review.googlesource.com/c/platform/frameworks/native/+/2680818
[4]: https://cs.android.com/android/platform/superproject/main/+/main:frameworks/native/libs/binder/tests/binderRpcBenchmark.cpp
[5]: https://cs.android.com/android/_/android/platform/frameworks/native/+/b85e7f7dbd0463d2ba78d53d50e64489fcb01ec4:libs/binder/tests/binderRpcBenchmark.cpp;l=206-217;bpv=1;bpt=0
[6]: https://lpc.events/event/17/contributions/1427/Signed-off-by Alice Ryhl <[email protected]>---
Alice Ryhl (15):
rust_binder: add binderfs support to Rust binder
rust_binder: add threading support
rust_binder: add work lists
rust_binder: add nodes and context managers
rust_binder: add oneway transactions
rust_binder: serialize oneway transactions
rust_binder: send nodes in transactions
rust_binder: addBINDER_TYPE_PTRsupport
rust_binder: addBINDER_TYPE_FDsupport
rust_binder: addBINDER_TYPE_FDAsupport
rust_binder: add process freezing
rust_binder: addTF_UPDATE_TXNsupport
rust_binder: add binder_logs/state
rust_binder: add vma shrinker
binder: delete the C implementationMatt Gilbride (1):
rust_binder: add oneway spam detectionWedson Almeida Filho (4):
rust_binder: define a Rust binder driver
rust_binder: add epoll support
rust_binder: add non-oneway transactions
rust_binder: add death notificationsdrivers/android/Kconfig | 19 +- drivers/android/Makefile | 2 + drivers/android/allocation.rs | 541 ++ drivers/android/binder.c | 6630 ----------------------- drivers/android/binder_alloc.c | 1284 ----- drivers/android/context.rs | 225 + drivers/android/defs.rs | 171 + drivers/android/error.rs | 94 + drivers/android/node.rs | 761 +++ drivers/android/process.rs | 1412 +++++ drivers/android/range_alloc.rs | 442 ++ drivers/android/rust_binder.rs | 389 ++ drivers/android/{binderfs.c => rust_binderfs.c} | 135 +- drivers/android/thread.rs | 1552 ++++++ drivers/android/transaction.rs | 428 ++ include/linux/rust_binder.h | 16 + include/uapi/linux/android/binder.h | 30 +- include/uapi/linux/magic.h | 1 + rust/bindings/bindings_helper.h | 6 + rust/helpers.c | 48 + rust/kernel/file.rs | 2 +- rust/kernel/lib.rs | 9 + rust/kernel/page_range.rs | 715 +++ rust/kernel/security.rs | 33 + rust/kernel/seq_file.rs | 47 + rust/kernel/sync/condvar.rs | 10 + rust/kernel/sync/lock.rs | 24 + rust/kernel/sync/lock/mutex.rs | 10 + rust/kernel/sync/lock/spinlock.rs | 10 + rust/kernel/task.rs | 2 +- scripts/Makefile.build | 2 +- 31 files changed, 7061 insertions(+), 7989 deletions(-)
Quoted 8 lines
---
base-commit:b4be1bd6c44225bf7276a4666fd30b8da9cba517
change-id: 20231101-rust-binder-464b89651887Best regards,
Signature · 1 line
Alice Ryhl <[email protected]>
Thanks Alice and others that have worked hard on this. This is a very cool and exciting move for Android. I'm very much looking forward to playing with this Rust binder and getting further on-field results.
--
Carlos Llamas
Alice Ryhl
maintainerQuoted 11 lines · Carlos Llamas
Quoted 6 lines · Alice Ryhl
Although this rewrite completely rethinks how the code is structured and how assumptions are enforced, we do not fundamentally change *how* the driver does the things it does. A lot of careful thought has gone into the existing design. The rewrite is aimed rather at improving code health, structure, readability, robustness, security, maintainability and extensibility. We also include more inline documentation, and
Can you expand a bit more on what the plan is here? Is it a two step process? e.g. replacing first and then revisiting the *how* binder does things later?
Yes, a big part of the motivation behind this rewrite is to make it easier to continue evolving Binder.
For example, we would like to make Binder have more thorough epoll support and the ability for a single-threaded server to handle many incoming transactions at the same time, similar to how you can use many non-blocking tcp sockets on a single thread today. This would have a number of performance benefits, like fewer threads, less contact switching, etc.
We would prefer to not attempt this in the C driver because of how challenging it is to make significant changes.
Alice