[PATCH v2 00/11] rust: I/O type generalization and projection

Gary Guo posted 11 patches 1 month, 3 weeks ago
There is a newer version of this series
drivers/gpu/nova-core/gsp.rs      |  44 ++-
drivers/gpu/nova-core/gsp/cmdq.rs |  65 ++--
drivers/gpu/nova-core/gsp/fw.rs   |  84 ++---
rust/kernel/devres.rs             |  11 +-
rust/kernel/dma.rs                | 232 ++++++------
rust/kernel/io.rs                 | 769 ++++++++++++++++++++++++++++++++------
rust/kernel/io/mem.rs             |  10 +-
rust/kernel/io/poll.rs            |   6 +-
rust/kernel/io/register.rs        |  40 +-
rust/kernel/pci/io.rs             |  80 ++--
rust/kernel/ptr.rs                |   7 +
samples/rust/rust_dma.rs          |  14 +-
12 files changed, 948 insertions(+), 414 deletions(-)
[PATCH v2 00/11] rust: I/O type generalization and projection
Posted by Gary Guo 1 month, 3 weeks ago
This series generalize `Mmio`/`MmioRaw` type from just an untyped region
to typed representations (so `MmioRaw<T>` is `__iomem *T`). This allows
us to remove the `IoKnownSize` trait; the information is sourced from
just the pointer from the `KnownSize` trait instead.

This enables us to implement `Io` trait for `Coherent<T>`, enabling
unified handling of MMIO and DMA coherent memory. It also paves the way
to uniformly support shared system memory, which Tyr will likely need
[1].

Built on this generalization, this series also add a `io::View` type
which represents a subview of a bigger I/O region, and a `io_project!()`
macro that provides a safe way to perform this. Some Nova code has been
converted in this series to demonstrate cleanups possible with this
addition.

New `io_read!()`, `io_write!()` has been added that supersedes
`dma_read!()`, `dma_write!()` macro. Although, they work for primitives
only (to be exact, types that the backend is `IoCapable` of).
One feature that was lost from the old `dma_read!()` and `dma_write!()`
series was the ability to read/write a large structs. However, the
semantics was unclear to begin with, as there was no guarantee about their
atomicity even for structs that were small enough to fit in u32.

In this series, I've introduced a new patch that re-introduces the
capability in the form of copying methods.

    dma_read!(foo, bar) -> io_project!(foo, bar).copy_read()
    dma_write!(foo, bar, baz) -> io_project!(foo, bar).copy_write(baz)

The semantics for these are modelled after memcpy so it has clear
semantics. This also makes it work for MMIO, which maps to
`memcpy_{from,to}io`.

This series depend on the projection syntax rework series [2].

Link: https://rust-for-linux.zulipchat.com/#narrow/channel/288089-General/topic/Generic.20I.2FO.20backends/near/572377073 [1]
Link: https://lore.kernel.org/rust-for-linux/20260415-projection-syntax-rework-v1-0-450723cb3727@garyguo.net/ [2]

Changes since v1:
- Rebased on projection syntax rework
- Added a new patch to forbid use of untyped I/O accessors and register
  macros on typed I/O structs (Alex).
- Fixed a few safety comments (Andreas).
- Added a new patch that implements copying methods (see above).
- Link to v1: https://lore.kernel.org/rust-for-linux/20260323153807.1360705-1-gary@kernel.org/

---
Gary Guo (11):
      rust: io: generalize `MmioRaw` to pointer to arbitrary type
      rust: io: generalize `Mmio` to arbitrary type
      rust: io: use pointer types instead of address
      rust: io: add missing safety requirement in `IoCapable` methods
      rust: io: restrict untyped IO access and `register!` to `Region`
      rust: io: add view type
      rust: dma: add methods to unsafely create reference from subview
      rust: io: add `read_val` and `write_val` function on I/O view
      gpu: nova-core: use I/O projection for cleaner encapsulation
      rust: dma: drop `dma_read!` and `dma_write!` API
      rust: io: add copying methods

 drivers/gpu/nova-core/gsp.rs      |  44 ++-
 drivers/gpu/nova-core/gsp/cmdq.rs |  65 ++--
 drivers/gpu/nova-core/gsp/fw.rs   |  84 ++---
 rust/kernel/devres.rs             |  11 +-
 rust/kernel/dma.rs                | 232 ++++++------
 rust/kernel/io.rs                 | 769 ++++++++++++++++++++++++++++++++------
 rust/kernel/io/mem.rs             |  10 +-
 rust/kernel/io/poll.rs            |   6 +-
 rust/kernel/io/register.rs        |  40 +-
 rust/kernel/pci/io.rs             |  80 ++--
 rust/kernel/ptr.rs                |   7 +
 samples/rust/rust_dma.rs          |  14 +-
 12 files changed, 948 insertions(+), 414 deletions(-)
---
base-commit: 77a9bb0193d790fb71c0edfc567bddc1b56fb3ff
change-id: 20260421-io_projection-16e7dc5ba7e4
prerequisite-change-id: 20260415-projection-syntax-rework-b790a305bc52:v1
prerequisite-patch-id: 110c29f61d0e7259d64057b6f364587c5ee501f5
prerequisite-patch-id: c157387d475fa22e32fc87d831a1ec384d256ca4
prerequisite-patch-id: 4109733485cae727763b2cd9ece69742e3d17cbf
prerequisite-patch-id: 5b686696f7dada42bb0768d255699dda0252234b
prerequisite-patch-id: a736703c95dc35a23e8c52bbdcba0ca9f38b55fb

Best regards,
--  
Gary Guo <gary@garyguo.net>