Content-Length: 19607 | pFad | http://lwn.net/Articles/830173/

Supporting Linux kernel development in Rust [LWN.net]
|
|
Subscribe / Log in / New account

Supporting Linux kernel development in Rust

Supporting Linux kernel development in Rust

Posted Aug 31, 2020 21:19 UTC (Mon) by adobriyan (subscriber, #30858)
Parent article: Supporting Linux kernel development in Rust

What's the plan for the other copy_from/to_user() form?

template<typename T>
int copy_from_user(T* k, user_ptr<T> u);


to post comments

Supporting Linux kernel development in Rust

Posted Aug 31, 2020 21:32 UTC (Mon) by adobriyan (subscriber, #30858) [Link] (1 responses)

and this is just terrible. First, only _interruptible/_killable version fail. Second, 9 lines to take proxy preference and then 7 lines to do what could be done statically in 1 line.

pub extern "C" fn init_module() -> i32 {
let mut mutex_guard = MUTEX.acquire();
let parrot_ref = match mutex_guard.get_mut() {
Some(p) => p,
None => {
unsafe {
printk!("%s", to_ptr!(c_string!("Failed to get reference to global state")))
};
return -1;
}
};
match parrot_ref.init() {
Ok(_) => 0,
Err(e) => {
unsafe { printk!("%s", to_ptr!(e)) };
-1
}
}
}

Supporting Linux kernel development in Rust

Posted Aug 31, 2020 22:52 UTC (Mon) by geofft (subscriber, #59789) [Link]

Yes, the parrot example is a (quick) demo of writing a Rust kernel module without specific bindings. It's not intended to be an example of what Rust code in the kernel should look like.

https://github.com/fishinabarrel/linux-kernel-module-rust... is safe bindings to copy_from_user / copy_to_user. See the rest of that repository for examples of its use.

Supporting Linux kernel development in Rust

Posted Aug 31, 2020 21:39 UTC (Mon) by josh (subscriber, #17465) [Link] (6 responses)

You appear to be quoting C++ code, and it's not clear what you're referring to.

Supporting Linux kernel development in Rust

Posted Aug 31, 2020 21:41 UTC (Mon) by adobriyan (subscriber, #30858) [Link] (5 responses)

copy_from_user/copy_to_user of a structure, not char[] buffer

Supporting Linux kernel development in Rust

Posted Aug 31, 2020 22:19 UTC (Mon) by nickodell (subscriber, #125165) [Link] (4 responses)

I assume that Rust has an equivalent of the C++ reinterpret_cast.

Supporting Linux kernel development in Rust

Posted Aug 31, 2020 22:23 UTC (Mon) by josh (subscriber, #17465) [Link]

It does. We also have work in progress to provide a safe way to do that, via the "safe transmute" project.

Supporting Linux kernel development in Rust

Posted Aug 31, 2020 22:25 UTC (Mon) by adobriyan (subscriber, #30858) [Link]

Cast covers implementation part, what about interface? I've posted what C++ would do.

Supporting Linux kernel development in Rust

Posted Aug 31, 2020 22:47 UTC (Mon) by notriddle (subscriber, #130608) [Link]

The Rust version of reinterpret_cast is called transmute. https://doc.rust-lang.org/stable/std/mem/fn.transmute.html

But copy_from_user itself looks closer to ptr::copy. https://doc.rust-lang.org/stable/std/ptr/fn.copy.html

Both of these functions are unsafe, as any form of copy_from_user must be, since there's no way to be sure that the contents of userspace memory are valid for whatever data structure you're transmuting them into. You would need to ensure that the data structure in question can accept any arbitrary byte sequence, which is what "safe transmute" proposals are supposed to do.

Supporting Linux kernel development in Rust

Posted Aug 31, 2020 22:55 UTC (Mon) by nybble41 (subscriber, #55106) [Link]

> I assume that Rust has an equivalent of the C++ reinterpret_cast.

It exists (you can cast raw pointers from one type to another and dereference them within unsafe blocks) but, like reinterpret_cast, you need to be very careful about how you use it. This is one area where it is probably easier to accidentally trigger undefined behavior in unsafe Rust code than in C, since Rust places more constraints on pointers/references than C does. At a minimum the target object would need to have a repr(C) type to ensure a consistent ABI, and the Copy trait as evidence that the content can be safely duplicated with a straightforward byte copy. The operation itself would also need to be marked as "unsafe" since there is no way that overwriting a Rust object (even one which is Copy and repr(C)) with arbitrary data from a buffer can be guaranteed to preserve whatever invariants might be expected by the object's implementation. With all that said, however, the std::ptr::read_unaligned function[1] is fairly close to a typed copy_from_user—without, obviously, the extra checking and error recovery that comes with accessing user memory from kernel mode.

[1] https://doc.rust-lang.org/std/ptr/fn.read_unaligned.html


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds









ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://lwn.net/Articles/830173/

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy