Skip to content

rust: Add io_pgtable and RTKit abstractions

Asahi Lina asahi lkml rust-for-linux 1 version In review

Get series with b4
b4 am [email protected] change-id 20230224-rust-iopt-rtkit-6d8b554d204c

Versions newest first

  1. v1 Latest5 patches 35 repliesIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/5rust: Add a Sealed traitrust/kernel/lib.rs+5 -00 replies
2/5rust: device: Add a minimal RawDevice traitrust/bindings/bindings_helper.h rust/kernel/device.rs rust/kernel/lib.rs+25 -020 replies
3/5rust: io_pgtable: Add io_pgtable abstractionrust/bindings/bindings_helper.h rust/kernel/io_pgtable.rs rust/kernel/lib.rs+354 -00 replies
4/5rust: soc: apple: rtkit: Add Apple RTKit abstractionrust/bindings/bindings_helper.h rust/kernel/lib.rs rust/kernel/soc/apple/mod.rs +2 more+272 -00 replies
5/5rust: device: Add a stub abstraction for devicesrust/helpers.c rust/kernel/device.rs+88 -115 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

Cover letter [PATCH 0/5] rust: Add io_pgtable and RTKit abstractions

Hi everyone!

This series is part of the set of dependencies for the drm/asahi Apple M1/M2 GPU driver.

It builds on top of the error helper series [1] and adds the skeleton of a Device abstraction and two proper subsystem abstractions that use
it: io_pgtable and RTKit.

Many kernel subsystems need to take struct device pointers. In the downstream rust-for-linux/rust branch, this is abstracted as a RawDevice trait that specific struct device subtypes (like platform_device) can implement such that all device subclasses can interoperate with device class-agnostic kernel APIs. Importing this trivial trait is all that is needed to start building and upstreaming abstractions.

The first patch in this series just adds a private Sealed trait, which is a common Rust pattern to allow crates to have traits that cannot be implemented outside the trait. This is important where implementing such traits incorrectly could lead to soundness issues, and where in general there is no reason for anything outside the kernel crate to do so.

The next patch introduces the RawDevice trait, which just represents anything that contains a struct device. This is enough for kernel abstractions that need to pass down such pointers to the underlying C code to compile.

The third patch adds the io_pgtable abstraction. The Apple GPU driver uses this common code to manage the GPU page tables, which are mostly standard ARMv8 page tables. The common code does need a new variant type to handle the specific PTE permission scheme and a few other details, which I already submitted [2]. This abstraction does not include that new type. If the underlying C support goes in first, I can update this series to add the missing wrapper in a later revision (it's just a few extra lines). See the panfrost driver for another example of a GPU driver reusing common io_pgtable code (with a specific format variant too).

The fourth patch adds the Apple RTKit subsystem abstraction, which is part of drivers/soc/apple. This builds on some support patches that should already be in the SoC tree queued for 6.3. While this code only makes sense on ARM64 (which does not yet have upstream Rust support), it should compile on x86 too with CONFIG_COMPILE_TEST. If you want to compile test this yourself at this point, you will need to merge in [3] (this will no longer be required as soon as Linus pulls the SoC stuff in this merge window).

Note: Although this patch adds a consumer for the Error stuff introduced previously, it is conditionally compiled so we still need the dead_code allow tags to avoid warnings when RTKit is not enabled. Also, since the Rust build system does not yet have split crates with helpers/bindings to allow for modular abstractions, this abstraction hard-depends on CONFIG_APPLE_RTKIT=y. We expect to remove this restriction once the build system is improved, but from talks with the DRM folks we're okay with the driver introducing built-in dependencies on some kernel subsystems (even though the driver itself is buildable as a module).

The fifth patch adds some more bits of the Device abstraction, to give context on the direction that is headed in. It includes a basic Device type that represents an owned reference to a generic struct device. This doesn't have to go in right now, but I thought it would be useful to provide some more context.

The branch at [4] contains the full series of patches rebased on upstream leading to the complete driver, for reference on how these APIs and abstractions are used.

Please let me know if you have any comments on how this is structured or would like to see the abstractions upstreamed separate! It's very difficult to upstream things only when they have a user, since the dependency chain for that user (the GPU driver) is very long. I hope that bundling these together helps give some context and get some momentum going with the upstreaming process.

[1] https://lore.kernel.org/rust-for-linux/[email protected]/T/#t
[2] https://lore.kernel.org/asahi/[email protected]/
[3] https://github.com/AsahiLinux/linux.git asahi-soc-rtkit-pmgr-6.3
[4] https://github.com/AsahiLinux/linux/tree/gpu/rebase-20230224

---
Asahi Lina (3):
rust: Add a Sealed trait
rust: io_pgtable: Add io_pgtable abstraction
rust: soc: apple: rtkit: Add Apple RTKit abstraction

Wedson Almeida Filho (2):
rust: device: Add a minimal RawDevice trait
rust: device: Add a stub abstraction for devices

 rust/bindings/bindings_helper.h |   3 +
 rust/helpers.c                  |  13 ++
 rust/kernel/device.rs           |  97 +++++++++++
 rust/kernel/io_pgtable.rs       | 351 ++++++++++++++++++++++++++++++++++++++++
 rust/kernel/lib.rs              |   9 ++
 rust/kernel/soc/apple/mod.rs    |   6 +
 rust/kernel/soc/apple/rtkit.rs  | 259 +++++++++++++++++++++++++++++
 rust/kernel/soc/mod.rs          |   5 +
 8 files changed, 743 insertions(+)

---
base-commit: 0ac13d87afc0086c0be43e7988173295a0864d5d
change-id: 20230224-rust-iopt-rtkit-6d8b554d204c

Thank you,
~~ Lina