Re:Resistance to Rust abstractions for DMA mapping
by meltyness on Tuesday February 04, 2025 @12:58AM
(#42928592)
Hm, doesn't this violate RAII? Does this need to be public?
[ Parent
| on HN ]
Re:Resistance to Rust abstractions for DMA mapping
by lmm on Tuesday February 04, 2025 @01:09AM
(#42928663)
> doesn't this violate RAII?
It's C, you don't get any RAII.
Re:Resistance to Rust abstractions for DMA mapping
by meltyness on Tuesday February 04, 2025 @01:13AM
(#42928689)
Not the Rust abstraction. If mutation is needed, you'd write closures into the interface, or provider borrows. It's definitely weird to barf out ownership of half of the fields.
E: I guess it sort of depends whether the handle is Copy semantics or not.
E2: I guess they're correctly leaking the resource, but I question whether they should, protections like that are kind of the whole point of using Rust to begin with.
Re:Resistance to Rust abstractions for DMA mapping
by pjmlp on Tuesday February 04, 2025 @02:54AM
(#42929428)
No, but you get defer via compiler extensions, and maybe in C2y as standard language feature.
Re:Resistance to Rust abstractions for DMA mapping
by flohofwoe on Tuesday February 04, 2025 @03:35AM
(#42929760)
I seem to remember that the Linux kernel already uses __attribute__((cleanup)
(which is basically GCC's non-standard defer).
Re:Resistance to Rust abstractions for DMA mapping
by pjmlp on Tuesday February 04, 2025 @05:41AM
(#42930720)
I think the Linux kernel uses most GCC extensions, and they also disable strict aliasing. :)
Re:Resistance to Rust abstractions for DMA mapping
by Dagonfly on Tuesday February 04, 2025 @08:35AM
(#42932111)
This is similar to `Box::into_raw` in Rust `std`. It destructs the RAII struct into a raw pointer. After that it's pretty much expected that you are responsible for cleanup.
Re:Resistance to Rust abstractions for DMA mapping
by meltyness on Tuesday February 04, 2025 @08:58AM
(#42932374)
Seems like the preferred interface would be a purely functional block that provides the access.
Obviously forcing callers to free means adding lints and docs. Returning borrows leaves the likelihood of entering into explicit lifetimes hell and such in calling code.
A functional interface like (paraphrasing)
// pub fn apply_op(&mut self, FnMut(&mut T, &bindings::dma_addr_t... ))
Keeps pandora inside the box.
Re:Resistance to Rust abstractions for DMA mapping
by Dagonfly on Tuesday February 04, 2025 @09:08AM
(#42932499)
As most of the kernel is in C, they probably need a raw pointer quite regularly to pass it into existing C APIs.
Just like `Box::into_raw`, this method shouldn't be used in pure Rust code. It's only available for C FFI.
Re:Resistance to Rust abstractions for DMA mapping
by meltyness on Tuesday February 04, 2025 @09:32AM
(#42932797)
Right but, this isn't merely FFI code, those are via Bindgen, this is the abstraction intended to be provided to all DMA driver authors as the API, as I understand it.
Re:Resistance to Rust abstractions for DMA mapping
by Tobu on Thursday February 06, 2025 @01:02PM
(#42964851)
The method is called `CoherentAllocation::into_parts`. And it takes self by value, which means the struct is consumed (this is implied by the into_ prefix). Of course you would either reconstruct the allocation or free the allocation yourself after this.
See Vec::into_raw_parts or Box::into_raw for an stdlib analogy.