Skip to contentSkip to conversation

io: add io_pgtable abstraction

PATCH Superseded Alice Ryhl 4 participants 5 messages Jan 13 – Jan 18

Subsystems (3)

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Alice Ryhl

author maintainer
#0 lore raw

PATCH v5 1/1 io: add io_pgtable abstraction

From: Asahi Lina <[email protected]>

This will be used by the Tyr driver to create and modify the page table of each address space on the GPU. Each time a mapping gets created or removed by userspace, Tyr will call into GPUVM, which will figure out which calls to map_pages and unmap_pages are required to map the data in question in the page table so that the GPU may access those pages when using that address space.

The Rust type wraps the struct using a raw pointer rather than the usual Opaque+ARef approach because Opaque+ARef requires the target type to be refcounted.

---
We did not discuss where this should go in the MAINTAINERS file yet. As
an initial suggestion I added it under IOMMU SUBSYSTEM, but please let
me know what you prefer. There is also a DEVICE I/O & IRQ [RUST]
subsystem that it could fall under.
---
Changes in v5:
- Fix warning by removing #[must_use] from map_pages.
- Reword comment on NOOP_FLUSH_OPS
- Add blank line after alloc_io_pgtable_ops
- Reword safety comment in Drop to refer ttbr method
- List in MAINTAINERS
- Pick up Reviewed-by: Daniel
- Link to v4: https://lore.kernel.org/r/[email protected]

Changes in v4:
- Rename prot::PRIV to prot::PRIVILEGED
- Adjust map_pages to return the length even on error.
- Explain return value in docs of map_pages and unmap_pages.
- Explain in map_pages that the caller must explicitly flush the TLB before accessing the resulting mapping.
- Add a safety requirement that access to a given range is required to
be exclusive.
- Reword comment on NOOP_FLUSH_OPS.
- Rebase on v6.19-rc1 and pick up tags.
- Link to v3: https://lore.kernel.org/r/[email protected]

Changes in v3:
- Almost entirely rewritten from scratch.
- Link to v2: https://lore.kernel.org/all/[email protected]/

---
 MAINTAINERS                     |   1 +
 rust/bindings/bindings_helper.h |   3 +-
 rust/kernel/io.rs               |   1 +
 rust/kernel/io/pgtable.rs       | 276 ++++++++++++++++++++++++++++++++++++++++
 4 files changed, 280 insertions(+), 1 deletion(-)
MAINTAINERS +1 -0
index 5b11839cba9de1e9e43f63787578edd8c429ca39..eccdb937762a7545fcd5db0194ab7c1b6dfdfde6 100644
@@ -13241,6 +13241,7 @@ F:	drivers/iommu/
 F:	include/linux/iommu.h
 F:	include/linux/iova.h
 F:	include/linux/of_iommu.h
+F:	rust/kernel/io/pgtable.rs
 
 IOMMUFD
 M:	Jason Gunthorpe <[email protected]>
rust/bindings/bindings_helper.h +2 -1
index a067038b4b422b4256f4a2b75fe644d47e6e82c8..1b05a5e4cfb4780fdc27813d708a8f1a6a2d9913 100644
@@ -56,9 +56,10 @@
 #include <linux/fdtable.h>
 #include <linux/file.h>
 #include <linux/firmware.h>
-#include <linux/interrupt.h>
 #include <linux/fs.h>
 #include <linux/i2c.h>
+#include <linux/interrupt.h>
+#include <linux/io-pgtable.h>
 #include <linux/ioport.h>
 #include <linux/jiffies.h>
 #include <linux/jump_label.h>
rust/kernel/io.rs +1 -0
index 98e8b84e68d11ef74b2026d8c3d847a127f4672d..88253158448cbf493ca200a87ef9ba958255e761 100644
@@ -10,6 +10,7 @@
 };
 
 pub mod mem;
+pub mod pgtable;
 pub mod poll;
 pub mod resource;
 
rust/kernel/io/pgtable.rs new file +276 -0

276 lines not on this page · raw message

---
base-commit: 3e7f562e20ee87a25e104ef4fce557d39d62fa85
change-id: 20251111-io-pgtable-fe0822b4ebdd

Best regards,

Signature · 1 line
Alice Ryhl <[email protected]>
maintainer
#1 lore raw
Quoted 4 lines · Alice Ryhl

We did not discuss where this should go in the MAINTAINERS file yet. As an initial suggestion I added it under IOMMU SUBSYSTEM, but please let me know what you prefer. There is also a DEVICE I/O & IRQ [RUST] subsystem that it could fall under.

Either sounds good to me.

MAINTAINERS F: drivers/iommu/ · line 13244

Quoted 9 lines
MAINTAINERS +1 -0
index 5b11839cba9de1e9e43f63787578edd8c429ca39..eccdb937762a7545fcd5db0194ab7c1b6dfdfde6 100644
@@ -13241,6 +13241,7 @@ F:	drivers/iommu/
 F:	include/linux/iommu.h
 F:	include/linux/iova.h
 F:	include/linux/of_iommu.h
+F:	rust/kernel/io/pgtable.rs

If it's going to be IOMMU, let's move the file to rust/kernel/iommu/, otherwise it gets picked up by both entries.

Tested-by on v5 1/1
#2 lore raw
In reply to Alice Ryhl
Quoted 13 lines · Alice Ryhl

From: Asahi Lina <[email protected]>

This will be used by the Tyr driver to create and modify the page table of each address space on the GPU. Each time a mapping gets created or removed by userspace, Tyr will call into GPUVM, which will figure out which calls to map_pages and unmap_pages are required to map the data in question in the page table so that the GPU may access those pages when using that address space.

The Rust type wraps the struct using a raw pointer rather than the usual Opaque+ARef approach because Opaque+ARef requires the target type to be refcounted.

Quoted 7 lines

---

As part of the wip for booting Tyr firmware, I have been using this io_pgtable abstraction including v5.

Built and tested on arm64 with rustc 1.91.0 and bindgen 0.72.1.

#3 lore raw
In reply to Alice Ryhl

Hi Alice,

Quoted 5 lines · Alice Ryhl
 MAINTAINERS                     |   1 +
 rust/bindings/bindings_helper.h |   3 +-
 rust/kernel/io.rs               |   1 +
 rust/kernel/io/pgtable.rs       | 276 ++++++++++++++++++++++++++++++++++++++++
 4 files changed, 280 insertions(+), 1 deletion(-)

Thanks for your contribution! I'd like to queue that for 6.20 but agree with Danilo that there should be a dedicated iommu directory under rust/kernel/. Can you please move the file and send a v6?

That directory might some day also contain everything needed to having full IOMMU drivers written in Rust :)

-Joerg

author maintainer
#4 lore raw
Quoted 10 lines · Joerg Roedel

Hi Alice,

Quoted 5 lines · Alice Ryhl
 MAINTAINERS                     |   1 +
 rust/bindings/bindings_helper.h |   3 +-
 rust/kernel/io.rs               |   1 +
 rust/kernel/io/pgtable.rs       | 276 ++++++++++++++++++++++++++++++++++++++++
 4 files changed, 280 insertions(+), 1 deletion(-)
Quoted 6 lines

Thanks for your contribution! I'd like to queue that for 6.20 but agree with Danilo that there should be a dedicated iommu directory under rust/kernel/. Can you please move the file and send a v6?

That directory might some day also contain everything needed to having full IOMMU drivers written in Rust :)

Done!