linux-imx/rust/kernel
Alice Ryhl 6896fba16f FROMLIST: rust: uaccess: add typed accessors for userspace pointers
Add safe methods for reading and writing Rust values to and from
userspace pointers.

The C methods for copying to/from userspace use a function called
`check_object_size` to verify that the kernel pointer is not dangling.
However, this check is skipped when the length is a compile-time
constant, with the assumption that such cases trivially have a correct
kernel pointer.

In this patch, we apply the same optimization to the typed accessors.
For both methods, the size of the operation is known at compile time to
be size_of of the type being read or written. Since the C side doesn't
provide a variant that skips only this check, we create custom helpers
for this purpose.

The majority of reads and writes to userspace pointers in the Rust
Binder driver uses these accessor methods. Benchmarking has found that
skipping the `check_object_size` check makes a big difference for the
cases being skipped here. (And that the check doesn't make a difference
for the cases that use the raw read/write methods.)

This code is based on something that was originally written by Wedson on
the old rust branch. It was modified by Alice to skip the
`check_object_size` check, and to update various comments, including the
notes about kernel pointers in `WritableToBytes`.

Co-developed-by: Wedson Almeida Filho <wedsonaf@gmail.com>
Signed-off-by: Wedson Almeida Filho <wedsonaf@gmail.com>
Signed-off-by: Alice Ryhl <aliceryhl@google.com>

Bug: 324206405
Link: https://lore.kernel.org/all/20240208-alice-mm-v2-3-d821250204a6@google.com/
Change-Id: I6dbbbc5bfada25ca0b63903317336cb5ec5c4d25
[ Replace "the the" with "the". ]
Signed-off-by: Alice Ryhl <aliceryhl@google.com>
2024-02-15 15:24:02 +00:00
..
init rust: init: update expanded macro explanation 2023-08-21 14:31:49 +02:00
sync FROMLIST: rust: file: add abstraction for poll_table 2024-02-13 19:46:05 +00:00
allocator.rs UPSTREAM: rust: bindings: rename const binding using sed 2024-02-08 16:12:24 +00:00
build_assert.rs rust: upgrade to Rust 1.68.2 2023-05-31 17:35:03 +02:00
cred.rs FROMLIST: rust: file: add Kuid wrapper 2024-02-13 19:46:05 +00:00
error.rs UPSTREAM: rust: support srctree-relative links 2024-02-08 16:12:24 +00:00
file.rs FROMLIST: rust: file: add DeferredFdCloser 2024-02-13 19:46:05 +00:00
init.rs UPSTREAM: rust: kernel: remove #[allow(clippy::new_ret_no_self)] 2024-02-08 16:12:24 +00:00
ioctl.rs UPSTREAM: rust: support srctree-relative links 2024-02-08 16:12:24 +00:00
kunit.rs UPSTREAM: rust: support srctree-relative links 2024-02-08 16:12:24 +00:00
lib.rs FROMLIST: rust: uaccess: add userspace pointers 2024-02-14 15:16:03 +00:00
prelude.rs rust: add derive macro for Zeroable 2023-08-21 14:31:48 +02:00
print.rs UPSTREAM: rust: support srctree-relative links 2024-02-08 16:12:24 +00:00
rbtree.rs FROMLIST: rust: rbtree: add RBTree::entry 2024-02-13 19:50:06 +00:00
security.rs FROMLIST: rust: security: add abstraction for secctx 2024-02-13 19:42:51 +00:00
static_assert.rs rust: static_assert: add static_assert! macro 2022-12-04 01:59:16 +01:00
std_vendor.rs rust: upgrade to Rust 1.68.2 2023-05-31 17:35:03 +02:00
str.rs UPSTREAM: rust: kernel: str: Implement Debug for CString 2024-02-08 16:12:24 +00:00
sync.rs FROMLIST: rust: file: add abstraction for poll_table 2024-02-13 19:46:05 +00:00
task.rs FROMLIST: rust: file: add DeferredFdCloser 2024-02-13 19:46:05 +00:00
time.rs FROMGIT: rust: time: add msecs to jiffies conversion 2024-02-12 15:51:53 +00:00
types.rs FROMLIST: rust: uaccess: add typed accessors for userspace pointers 2024-02-15 15:24:02 +00:00
uaccess.rs FROMLIST: rust: uaccess: add typed accessors for userspace pointers 2024-02-15 15:24:02 +00:00
workqueue.rs UPSTREAM: rust: support srctree-relative links 2024-02-08 16:12:24 +00:00