|
|
Subscribe / Log in / New account

Rust is the future of systems programming, C is the new Assembly (Packt)

Rust is the future of systems programming, C is the new Assembly (Packt)

Posted Aug 29, 2019 6:39 UTC (Thu) by roc (subscriber, #30627)
In reply to: Rust is the future of systems programming, C is the new Assembly (Packt) by felixfix
Parent article: Rust is the future of systems programming, C is the new Assembly (Packt)

A good start would be to make it possible to write some kinds of drivers in Rust. This would already be quite a lot of work --- integrating Rust into the Linux build system, writing Rust wrappers around kernel APIs --- but much has already done experimentally. There's a good chance that encouraging people to submit their wacky drivers in Rust would improve the quality of the driver, partly because you can focus attention on the unsafe parts (if any ... many Linux drivers probably don't need much or any unsafe code).


(Log in to post comments)

Rust is the future of systems programming, C is the new Assembly (Packt)

Posted Aug 29, 2019 11:40 UTC (Thu) by moltonel (guest, #45207) [Link]

Rust is the future of systems programming, C is the new Assembly (Packt)

Posted Aug 29, 2019 16:33 UTC (Thu) by josh (subscriber, #17465) [Link]

I have, yes. I spoke with Greg Kroah-Hartman, and he said he'd be willing to accept a framework in the kernel for writing drivers in Rust, as long as 1) for now it wasn't enabled by default (even if you did "make allyesconfig") so that people don't *need* Rust to build the kernel, and 2) it shows real benefits beyond writing C, such as safe wrappers for kernel APIs.

I'd be happy to talk with someone interested in writing such a framework.

Rust is the future of systems programming, C is the new Assembly (Packt)

Posted Aug 29, 2019 18:34 UTC (Thu) by klmrdn (guest, #134116) [Link]

I don't have many experience with Linux kernel programming, but I love Rust and I would love to help if I can.

Rust is the future of systems programming, C is the new Assembly (Packt)

Posted Aug 29, 2019 20:30 UTC (Thu) by lucasamorim (guest, #106112) [Link]

I would love to help too

Rust is the future of systems programming, C is the new Assembly (Packt)

Posted Aug 29, 2019 21:06 UTC (Thu) by slankes (subscriber, #134120) [Link]

I would also love to help too

Rust is the future of systems programming, C is the new Assembly (Packt)

Posted Aug 30, 2019 4:21 UTC (Fri) by juchiast (guest, #134127) [Link]

I would love to help too!

Rust is the future of systems programming, C is the new Assembly (Packt)

Posted Aug 30, 2019 4:21 UTC (Fri) by Rufus-Clobin (guest, #134129) [Link]

I would too lovely help also.

Rust is the future of systems programming, C is the new Assembly (Packt)

Posted Aug 30, 2019 4:25 UTC (Fri) by geofft (subscriber, #59789) [Link]

We've got a framework in progress with some safe wrappers and we're working on getting it in a state suitable for upstreaming: https://github.com/fishinabarrel/linux-kernel-module-rust

We don't quite have enough useful API surface for a real module (although there are a few neat demos). I'd like to get it to the point where it's you can have a small filesystem or maybe a small LSM or something entirely in safe Rust.

Rust is the future of systems programming, C is the new Assembly (Packt)

Posted Aug 30, 2019 12:53 UTC (Fri) by lzh (guest, #134122) [Link]

In my simple and exprimental project (https://github.com/lizhuohua/linux-kernel-module-rust), I implemented a simple driver for the Ethernet controller used on raspberry pi 3, based on Geofft's amazing project (https://github.com/fishinabarrel/linux-kernel-module-rust). And I found that the performance impact is actually negligible. However, I found the following problems:

1. Linux kernel doesn't guarantee API/ABI stability, so it's hard to design a universal Rust interface. At least it is almost impossible to make it work on all versions of Linux.
2. Many C language features have no corresponding Rust support. For example, it seems like impossible to construct a struct with bitfields.
But yes, I believe Rust is a promising language for kernel programming. Let's join forces to make this happen!

Rust is the future of systems programming, C is the new Assembly (Packt)

Posted Aug 30, 2019 15:36 UTC (Fri) by josh (subscriber, #17465) [Link]

> 2. Many C language features have no corresponding Rust support. For example, it seems like impossible to construct a struct with bitfields.

That's one of the items on the FFI working group's list to work on.

Rust is the future of systems programming, C is the new Assembly (Packt)

Posted Aug 31, 2019 11:48 UTC (Sat) by slankes (subscriber, #134120) [Link]

I would too lovely help also.

Rust is the future of systems programming, C is the new Assembly (Packt)

Posted Sep 1, 2019 7:19 UTC (Sun) by da-x (guest, #113602) [Link]

I contemplated this for awhile. Some conclusions:

* While FFI is used mostly to wrap Rust APIs around libraries originally written in C/C++, where the state of the loaded C/C++ code is demarcated, doing the other way around is much more difficult because of the statefulness of the _surrounding_ C/C++ code.
* Sticking points are the heavy use of intrusive data-structures alongside kernel APIs, e.g. `offsetof`. Providing safety around that means that you cannot use some of the APIs directly.
* Many APIs use raw pointers all over. How do you ensure safety around these APIs? For example, for VFS - do you do something similar to FUSE but *inside* the kernel for some subsystems?
* Some of the kernel APIs make heavy use of the C preprocessor.
* In debugging, kernel backtraces would need to deal with backtraces inside the kernel code, alongside backtraces for the C code. I'm guessing that kernel developers would like to see demanded symbols in kernel oops reports.
* How would kernel Rust code unwind in panic situations? (I'm guessing `fn drop` calls would be skipped).
* Problem domain is similar to loading Rust-based DSOs in userspace, for frameworks that are not originally written in Rust (think about a GIMP plugin, for instance). Perhaps we should tackle that problem first?

Bottom line, I think that it would be quite hard to extract value from this, given how much of the APIs exposed by the kernel are engrossed in C-based complexity and unsafety assumptions. When I do imagine such framework, I see it would need a to provide a thick layer with a disparate set of APIs, if it ever wants to be viable and a fully idiomatic Rust.

Maybe it's just a crazy idea, but perhaps a different project can be realized, for example - having Redox's kernel and Linux linked together, providing a single kernel binary, and drivers for Redox co-exist in the same space, gradually supplanting Linux drivers.

tl;dr: Rust is NFG. Try again in 2030.

Posted Sep 1, 2019 14:24 UTC (Sun) by ksandstr (guest, #60862) [Link]

These conclusions are generally met with a variation of the motte-and-bailey argument, i.e. that Rust has an "unsafe" mode where all the manageable evils of C are permitted, albeit with back-to-front declaration syntax, semantically significant scoping[-1], an unhelpful type system, and a single compiler whose intermediate language is only equivalent to C but which supports fewer hardware targets. The advocate will then ignore these concerns, describing an ideal where existing code is supplanted with an incompatible language for which there are fewer skilled programmers, no semantics formalized to a standards-language level and then immobilized in stone every dozen years or so, no independent implementation from even hardware vendors, no fifty-year history, and no nigh-universal acceptance outside its niche as Mozilla's in-house language for browser implementation.

So it's not unreasonable to put that line of argument to rest just on the back of Rust's immaturity as a language, regardless of its supposedly groundbreaking special features (which don't exist inside "unsafe" blocks), and the discussion moved forward perhaps to its conclusion.

Rust is a huge crock. The reasons why it looks good to some are that contemporary C++ is also a crock (e.g. for attempting parity with Rust); and because its advocacybots[0] soak up all the flak that its features and lack thereof should rather face. Oh, and also security[1], and indistinct references to studies supposedly proving how people[2] just aren't good enough to handle sharp-cornered objects. This is not substantially different from the 1996 Java deal[3], only now it's a good thing that there's not going to eventually be a sufficiently smart just-in-time compiler and prescient garbage collector.

>(...) having Redox's kernel and Linux linked together, providing a single kernel binary, and drivers for Redox co-exist in the same space, gradually supplanting Linux drivers.

This is known as driver or subsystem porting, and it's commonly discussed in terms of having the younger system built up without reimplementing "the boring parts". In the context of Rust this takes on a rather millennarian tone where, instead of an eventual Redox 6.0 having hardware support (filesystem, device mapper, network stack, etc.) parity with Linux 12.34, the latter would have been rewritten to exclude non-Rust languages. A cultural revolution, if you will; certainly a great leap forward. Ensuring that definitely all the bathwater is gone so it won't be proven innocent of causing all our problems.

[-1] beyond identifier shadowing, which has a dedicated warning setting encouraged by common best practice.
[0] it's interesting to compare recent Rust advocacy with some from 2017; the arguments are the same and the conversations never move forward, akin to the hog having successfully dragged the farmer into a bout of mud-wrestling and wound up happy whether he wins or loses.
[1] in the sense of a small category of pointer bugs which can certainly look complete to the amateur who's never seen other use cases, let alone put them to practice himself; but not (say) AB-BA races, dangling keys, context violations such as misordered locking, and so forth.
[2] cheapest possible subcontractors, who can't but whose bugs aren't immediately apparent.
[3] in particular, MMUs still don't exist, and knee-jerkably deadly hazards still lurk even in data the program has itself generated.

tl;dr: Rust is NFG. Try again in 2030.

Posted Sep 1, 2019 18:06 UTC (Sun) by Cyberax (✭ supporter ✭, #52523) [Link]

> This is not substantially different from the 1996 Java deal[3], only now it's a good thing that there's not going to eventually be a sufficiently smart just-in-time compiler and prescient garbage collector.
Whut? Rust by design has statically managed object lifetime, that's the reason for the whole borrow-checker thingie.

Please, do stop smoking whatever you're smoking.

tl;dr: Rust is NFG. Try again in 2030.

Posted Sep 1, 2019 20:58 UTC (Sun) by roc (subscriber, #30627) [Link]

Superior trolling. Good work.

tl;dr: Rust is NFG. Try again in 2030.

Posted Sep 2, 2019 1:37 UTC (Mon) by flussence (subscriber, #85566) [Link]

Where can I get a copy of the point-scoring system you use for all that green ink?

Rust is the future of systems programming, C is the new Assembly (Packt)

Posted Sep 1, 2019 20:54 UTC (Sun) by roc (subscriber, #30627) [Link]

Those are challenges worth highlighting. They need not be showstoppers. The details matter and they'll have to be addressed individually. E.g.

> * Sticking points are the heavy use of intrusive data-structures alongside kernel APIs, e.g. `offsetof`. Providing safety around that means that you cannot use some of the APIs directly.

Rust can express some uses of intrusive data structures safely (e.g. https://docs.rs/intrusive-collections/0.8.1/intrusive_col...) so it comes down whether the implicit lifetime constraints in play in each situation are expressible.

> * Many APIs use raw pointers all over. How do you ensure safety around these APIs? For example, for VFS - do you do something similar to FUSE but *inside* the kernel for some subsystems?

Similar issue: raw pointers themselves aren't the issue --- they can be cast to references --- the question is, can you assign valid and useful Rust lifetimes to those references? And that depends on the invariants of the specific API. But the common idioms of C code (e.g., "pointer parameter guaranteed to be valid for the duration of the call") are easily translated into Rust.

> * Some of the kernel APIs make heavy use of the C preprocessor.

They'll have to be translated by hand into inline, possibly generic Rust code, or possibly into Rust macros.

> * In debugging, kernel backtraces would need to deal with backtraces inside the kernel code, alongside backtraces for the C code. I'm guessing that kernel developers would like to see demanded symbols in kernel oops reports.

Should be OK if the build-the-kernel-with-LLVM people are already getting those backtraces. Rust demangling has recently been standardized and there's a library to do it.

> * How would kernel Rust code unwind in panic situations? (I'm guessing `fn drop` calls would be skipped).

Unwinding out to the nearest C code is an option, but probably you just want panic to call BUG().

> * Problem domain is similar to loading Rust-based DSOs in userspace, for frameworks that are not originally written in Rust (think about a GIMP plugin, for instance). Perhaps we should tackle that problem first?

This is being done. E.g. Federico did librsvg, people are writing GStreamer plugins in Rust. https://github.com/sdroege/gst-plugin-rs

Rust is the future of systems programming, C is the new Assembly (Packt)

Posted Nov 26, 2019 15:47 UTC (Tue) by silvergate (guest, #135759) [Link]

Why not (re-) try to do something like UDI (http://www.projectudi.org/) in Rust? I don't know much about driver development, but an abstract driver framework looks interesting: You write the framework (API) once, drivers once and two (or more) implementations (one for linux and one for Redox)... and can use the same driver on linux and Redox. Another pro: this would curcument the unstable linux driver API problem, con: overhead, a lot of work (more?).


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