Skip to contentSkip to conversation

Add dma coherent allocator abstraction

PATCH Superseded Abdiel Janulgue 2 participants 7 messages Nov 4, 2024

Subsystems (2)

v3 of 15 Superseded Latest is v15 →

lore
Share & tools
lore

Abdiel Janulgue

author reviewer
#0 lore raw

v3 cover Add dma coherent allocator abstraction

This series adds support for the dma coherent allocator.

Changes since v2:
- Fixed missing header for generating the bindings.

Changes since v1:
- Fix missing info in commit log where EOVERFLOW is used
- Restrict the dma coherent allocator to numeric types for now for valid behaviour.
- Build slice dynamically.

Abdiel Janulgue (2):
rust: error: Add EOVERFLOW
rust: add dma coherent allocator abstraction.

 rust/bindings/bindings_helper.h |   1 +
 rust/kernel/dma.rs              | 165 ++++++++++++++++++++++++++++++++
 rust/kernel/error.rs            |   1 +
 rust/kernel/lib.rs              |   1 +
 4 files changed, 168 insertions(+)
 create mode 100644 rust/kernel/dma.rs

base-commit: ae7851c29747fa3765ecb722fe722117a346f988

Signature · 1 line
2.43.0
author reviewer
#1 lore raw

PATCH v3 1/2 rust: error: Add EOVERFLOW

Trivial addition for missing EOVERFLOW error. This is used by a subsequent patch that might require returning EOVERFLOW as a result of checked_mul.

---
 rust/kernel/error.rs | 1 +
 1 file changed, 1 insertion(+)
rust/kernel/error.rs +1 -0
index 7cd3bbab52f2..92bfdaaedb02 100644
@@ -63,6 +63,7 @@ macro_rules! declare_err {
     declare_err!(EPIPE, "Broken pipe.");
     declare_err!(EDOM, "Math argument out of domain of func.");
     declare_err!(ERANGE, "Math result not representable.");
+    declare_err!(EOVERFLOW, "Value too large for defined data type.");
     declare_err!(ERESTARTSYS, "Restart the system call.");
     declare_err!(ERESTARTNOINTR, "System call was interrupted by a signal and will be restarted.");
     declare_err!(ERESTARTNOHAND, "Restart if no handler.");
Signature · 1 line
2.43.0
author reviewer
#2 lore raw

PATCH v3 2/2 rust: add dma coherent allocator abstraction.

In reply to Abdiel Janulgue

Add a simple dma coherent allocator rust abstraction. Based on Andreas Hindborg's dma abstractions from the rnvme driver.

---
 rust/bindings/bindings_helper.h |   1 +
 rust/kernel/dma.rs              | 165 ++++++++++++++++++++++++++++++++
 rust/kernel/lib.rs              |   1 +
 3 files changed, 167 insertions(+)
 create mode 100644 rust/kernel/dma.rs
rust/bindings/bindings_helper.h +1 -0
index a80783fcbe04..3ff2abbfaef6 100644
@@ -10,6 +10,7 @@
 #include <linux/blk-mq.h>
 #include <linux/blk_types.h>
 #include <linux/blkdev.h>
+#include <linux/dma-mapping.h>
 #include <linux/errname.h>
 #include <linux/ethtool.h>
 #include <linux/firmware.h>
rust/kernel/dma.rs new file +165 -0

165 lines not on this page · raw message

rust/kernel/lib.rs +1 -0
index b62451f64f6e..b713c92eb1ef 100644
@@ -32,6 +32,7 @@
 pub mod block;
 mod build_assert;
 pub mod device;
+pub mod dma;
 pub mod error;
 #[cfg(CONFIG_RUST_FW_LOADER_ABSTRACTIONS)]
 pub mod firmware;
Signature · 1 line
2.43.0
reviewer
#3 lore raw

Re: [PATCH v3 2/2] rust: add dma coherent allocator abstraction.

rust/kernel/dma.rs line 22

Quoted 7 lines · Abdiel Janulgue
rust/kernel/dma.rs new file +7 -0
+/// Abstraction of dma_alloc_coherent
+///
+/// # Invariants
+///
+/// For the lifetime of an instance of CoherentAllocation, the cpu address is a valid pointer
+/// to an allocated region of consistent memory and we hold a reference to the device.
+pub struct CoherentAllocation<T: Add> {

Requiring T: Add is very unusual. Why?

I don't even see any additions anywhere.

Alice

author reviewer
#4 lore raw

Re: [PATCH v3 2/2] rust: add dma coherent allocator abstraction.

Quoted 12 lines · Alice Ryhl

rust/kernel/dma.rs line 22

Quoted 7 lines · Abdiel Janulgue
rust/kernel/dma.rs new file +7 -0
+/// Abstraction of dma_alloc_coherent
+///
+/// # Invariants
+///
+/// For the lifetime of an instance of CoherentAllocation, the cpu address is a valid pointer
+/// to an allocated region of consistent memory and we hold a reference to the device.
+pub struct CoherentAllocation<T: Add> {

Requiring T: Add is very unusual. Why?

Quoted 3 lines

I don't even see any additions anywhere.

Alice

Background here:
https://lore.kernel.org/all/[email protected]/

Basically this aims to restrict the abstraction to non-ZST types. Are there better (clever) ways to do this?

Thanks!

/Abdiel

reviewer
#5 lore raw

Re: [PATCH v3 2/2] rust: add dma coherent allocator abstraction.

Quoted 18 lines · Abdiel Janulgue
Quoted 14 lines · Alice Ryhl

rust/kernel/dma.rs line 22

Quoted 7 lines · Abdiel Janulgue
rust/kernel/dma.rs new file +7 -0
+/// Abstraction of dma_alloc_coherent
+///
+/// # Invariants
+///
+/// For the lifetime of an instance of CoherentAllocation, the cpu address is a valid pointer
+/// to an allocated region of consistent memory and we hold a reference to the device.
+pub struct CoherentAllocation<T: Add> {

Requiring T: Add is very unusual. Why?

I don't even see any additions anywhere.

Quoted 7 lines
Quoted 1 line

Alice

Background here:
https://lore.kernel.org/all/[email protected]/

Basically this aims to restrict the abstraction to non-ZST types. Are there better (clever) ways to do this?

That doesn't work:

struct MyZST {}

impl Add for MyZST {
    type Output = MyZST;
    fn add(&self, rhs: MyZST) -> MyZST {
        MyZST
    }
}

You'll need your own trait, or you can build_assert! that the size is non-zero in the constructor. Also, you'll need to require the FromBytes/AsBytes traits because you're converting the values to/from bytes, which is not safe to do for all types.

Alice

author reviewer
#6 lore raw

Re: [PATCH v3 2/2] rust: add dma coherent allocator abstraction.

Quoted 3 lines · Alice Ryhl

You'll need your own trait, or you can build_assert! that the size is non-zero in the constructor.

I think we can catch this in the constructor. I'll try the build_assert! approach first as it's simpler.

/Abdiel