top | item 23800201

Linux kernel in-tree Rust support

468 points| littlestymaar | 5 years ago |lore.kernel.org | reply

491 comments

order
[+] roca|5 years ago|reply
Rust in Linux will be fantastic except for compile time. Rust (and the world) needs a Manhattan Project to build a fast Rust compiler (where by "fast" I mean both efficient and scalably parallel when compiling a workspace with many crates and long dependency chains).

To put this in perspective, though: increasing the number of people paid to work on the Rust compiler by 10x would only mean hiring about 25 people. Compared to the size of the projects that are starting to depend on Rust, that's a rounding error.

[+] crazypython|5 years ago|reply
GCC 10 supports Dlang directly. (The support is still immature and it's being updated to the newer version of Dlang.)

Dlang compiles quickly not because the language is simple, but because the compiler authors care about compilation speed:

Lexer skips four spaces at once https://github.com/dlang/dmd/pull/11095

Optimize core logic to 7x compilation speed of language feature https://github.com/dlang/dmd/pull/11303

Cache rarely used types to improve memory locality https://github.com/dlang/dmd/pull/11363

[+] sebazzz|5 years ago|reply
Typescript allows to compile without type checking at the sacrifice of correctness. If you would have a slow computer or huge code-base, you could consider using the type checker only for tests, pre-checkin check, or CI. (Not that Typescript is slow - but as an conceptual example)

Would something similar be possible for Rust, so you still have the ability for correctness for CI and release builds, but allow for fast compilation when necessary or desired?

[+] the8472|5 years ago|reply
I assume dependency trees in the kernel would be much more shallow.
[+] darksaints|5 years ago|reply
I know a lot of people will disagree with me, and I know that the rust compiler has room for improvement, but compile time isn't really that bad for rust when you consider what it actually does for you.

Assuming no unsafe blocks, rust's type system eliminates entire classes of memory safety errors. It fundamentally eliminates data races. It eliminates large swaths of logical errors, due to its use of algebraic data types, and strong type enforcement. No null pointers, explicit error handling, and no undefined behavior.

It boggles my mind that people will balk at compile times for rust, but then not even bat an eye at their C++ testkit that runs for 8+ hours, primarily checking for errors that rust would never let you make in the first place.

[+] rumanator|5 years ago|reply
> Rust in Linux will be fantastic except for compile time. Rust (and the world) needs a Manhattan Project to build a fast Rust compiler (where by "fast" I mean both efficient and scalably parallel when compiling a workspace with many crates and long dependency chains).

A basic technique to parallelize sections is to split a project into modules. Is this not an option for the linux kernel?

[+] crb002|5 years ago|reply
https://github.com/StanfordSNR/gg - should be almost the same as the GCC/LLVM thunk extractor. You have to pay for the borrow checker, but LLVM IR optimization passes should be the same complexity.
[+] fluffything|5 years ago|reply
> increasing the number of people paid to work on the Rust compiler by 10x would only mean hiring about 25 people.

Citation needed ?

I know at least 20 people being paid to work on the Rust compiler, which crank's your 10x number from 25 to 200. And this is only the people I can come up with from the top of my head.

[+] zoul|5 years ago|reply
How much of the needed speedup is engineering and how much is research?
[+] Ericson2314|5 years ago|reply
Replace "faster" with "more incremental".

Trying to get a program to quickly do the exact same thing over and over again is a colossal waste of resource.

[+] zelly|5 years ago|reply
Yes, we should have a national politburo assign funds to Rust development. Although most of private industry (proles) disagrees, us in the Administration believe that Rust is superior. The proles--ahem I mean programmers with jobs--keep causing problems by using Unsafe technologies and being too dumb to use them. Rust should be funded by The People. It is a Public Good after all because using anything else would be an act of defiance against the will of The People.
[+] mekster|5 years ago|reply
> increasing the number of people paid to work on the Rust compiler by 10x would only mean hiring about 25 people. Compared to the size of the projects that are starting to depend on Rust, that's a rounding error.

And are you going to pay for the "rounding error" or just expecting someone to pay for millions a year for your idea?

[+] alfiedotwtf|5 years ago|reply
I’m not sure why everyone cares so much about compile times compared to the only thing that matters - runtime performance.

For the lifetime of a binary, compilation time is like 0.000001% of its existence.

I’m more than happy to wait longer compared to other languages (even zero time at all with scripting languages) while cargo does it’s thing.

[+] coldpie|5 years ago|reply
> increasing the number of people paid to work on the Rust compiler by 10x would only mean hiring about 25 people

I don't think there are 25 people qualified to do that work looking for jobs. It's not something you can just throw money at, you need really qualified people to do that kind of work. Those people are in very high demand.

[+] cxr|5 years ago|reply
> a Manhattan Project to build a fast Rust compiler

Or work on getting what Rust gives you in a different language that isn't Rust (maybe entirely new, maybe not[1]).

One underdiscussed thing about Rust is that it's so clearly a byproduct of the C++ world. And the most disappointing thing about Rust is that it probably won't ever be seen as a prototype that should be thrown away.

1. http://mbeddr.com/

[+] ComputerGuru|5 years ago|reply
These threads always devolve into "rust is too slow" written by developers (or enthusiasts) that have never written no_std code in production. I've written and shipped firmware for embedded devices written in rust, yes, still using cargo and external crates, and had zero issues with compile time because the nature of the dependencies in the deps tree is different and very carefully curated.

Anyway, I really just wanted to point out that from the mailing list we have Linus and Greg endorsing this experiment/effort from the Linux side and a commitment from Josh on behalf of the rust team to grow the language itself with the needs of the kernel in mind. That's quite impressive and more than I could have hoped for.

I've actually played with writing kernel code in rust - for Windows/NT, however - and it's quite weird to be able to use such high-level type constructs in code where you typically manually chases pointers and wouldn't be surprised to see statically allocated global variables used to monitor reference counts.

[+] ksec|5 years ago|reply
Linus, normally being C has its quirks but is great and Anti C++ in general is endorsing Rust?

Why is that? Did he ever gave out his reasons?

[+] pjmlp|5 years ago|reply
Why it is weird? It has been done before in Ada, Object Pascal, C++, Mesa/Cedar, Modula-2, Lisp, Oberon, Sing#, System C#, Objective-C,....
[+] rwmj|5 years ago|reply
The title is not very accurate, this is a thread about a discussion on this topic which will happen at the upcoming Linux Plumbers Conference in late August.
[+] littlestymaar|5 years ago|reply
I see what you mean. I just posted it here with the same title that was used on /r/Linux, and that I found accurate (for the same reasons chrismorgan exposed in a sibling comment) but now I agree with you that it could cause some confusion.

Maybe a moderator could rename the post to “discussion about Linux kernel in-tree support” or something like that?

[+] chrismorgan|5 years ago|reply
The title is perfectly accurate, it’s an email thread about Linux kernel in-tree Rust support. Sure, you could misconstrue such a title to be implying that the Linux kernel supports Rust in-tree already it if you wanted to, but half the titles on a site like this could be similarly misconstrued.
[+] xvilka|5 years ago|reply
Immunant folks wrote a blog post [1] about automating conversion of Linux kernel drivers from C to Rust by using their tool c2rust[2].

[1] https://immunant.com/blog/2020/06/kernel_modules/

[2] https://github.com/immunant/c2rust

[+] yjftsjthsd-h|5 years ago|reply
I don't see them discussing what I view as the biggest question of such conversion: is the converted code safer than C, or is it a direct translation with all the issues that we were trying to fix by using Rust? (This came up, IIRC, with automatic C to Go; it worked, but was only useful as a first step because it gave you unsafe unidiomatic Go code)
[+] pepijndevos|5 years ago|reply
Pleasantly surprised by Linus response. IIRC his attitude to C++ was that it should be refused if only to keep C++ programmers out.
[+] xvilka|5 years ago|reply
Rust is well designed modern and concise language with sound type system, while C++ isn't. The difference is huge and obvious.
[+] for_xyz|5 years ago|reply
His attitude towards C++ also changed when using it for his side project [1].

Initially he started with C and GTK+ and later migrated to C++ and QT Framework.

[1] https://subsurface-divelog.org/

[+] kevingadd|5 years ago|reply
Perhaps the possibility of rust improving kernel security/robustness makes the idea of rust integration seem like it carries its own weight, where C++ has more downsides (perceived or real) and fewer upsides.

Rust's history/origins - loosely, being designed to allow replacing Mozilla's C/C++ with safer Rust that performs well - feel like a good fit for kernel drivers even if the core kernel bits will always be C.

[+] buster|5 years ago|reply
Yes, very much so. Not even only in the context of Rust but the insight, to fail fast, integrate early and do work in the open, instead of some hidden work, failing after a long time, when revealed.
[+] mijoharas|5 years ago|reply
So this surprises me, obviously this is early discussion about a potential topic, but the general consensus seemed to be more positive than I thought.

I thought I'd remembered reading something (maybe from linus) that seemed very against having rust in the kernel, can anyone find a source for that, I searched a little and can't?

(caveat, I obviously realise that linus isn't supporting rust in the kernel, and is only saying something bounded that, if we have it, it shouldn't be completely hidden behind some config options, but it doesn't match my memory)

[+] asveikau|5 years ago|reply
Just stating something obvious since I don't see it noted here: Linus is a smart guy with this idea of not wanting it to be some niche feature that nobody enables and hence nobody cares about and sees breakage from.
[+] xondono|5 years ago|reply
I always find these email threads very hard to follow, specially on mobile.

Is there anyway to have them shown more friendly?

[+] rwmj|5 years ago|reply
Assuming this happened and some essential drivers were written in Rust, would this effectively exclude GCC from being used to compile the full kernel?
[+] est31|5 years ago|reply
GCC can still compile the components written in C. Only drivers for select hardware might be written in Rust in the future, optional components basically. And GCC won't be more disadvantaged than clang, as clang can't compile Rust code either. The only production compiler that can compile Rust is rustc.
[+] raverbashing|5 years ago|reply
I wonder if/when GCC will have a Rust frontend

There seems to be some discussions but not sure if it's a goal

[+] Jonnax|5 years ago|reply
So I don't know much about kernel development.

How many other languages apart from C are there in the kernel?

[+] rwmj|5 years ago|reply
Depends how you define "in the kernel". In terms of what is compiled into the kernel image, it's C plus small pieces of assembler (both inline and in separate files). This proposal (if it ever happens) would be about adding Rust to that. If you mean the tree used to build the kernel, you could say it includes shell script, Makefiles, the Kconfig descriptions, and more. If you mean all the bits you need to have a very minimally working system which boots, you'd want to include all the languages used for the necessary userspace tools like the init system and udev (although the vast majority of that is also C and shell scripts).
[+] correct_horse|5 years ago|reply
I'm a bit uninformed as well. Obviously it must use assembly in some places, it has makefiles for building and some scripts in perl. Here's what a cloc clone has to say.

    tokei
    ===============================================================================
    Language            Files        Lines         Code     Comments       Blanks
    ===============================================================================
    ASN.1                  11          484          343           33          108
    Assembly                3         2751         2450            0          301
    GNU Style Assembly   1296       370153       270691        56418        43044
    Autoconf                6          347          303           20           24
    Automake                3           31           23            3            5
    BASH                   54         1991         1438          321          232
    C                   28971     19846129     14634535      2351170      2860424
    C Header            21110      6056898      4465297      1010398       581203
    C++                     8         2313         2016           68          229
    C++ Header              2          125           59           55           11
    CSS                     1          137           80           29           28
    Device Tree          3351       900862       735380        54238       111244
    Gherkin (Cucumber)      1          252          174           50           28
    Happy                   9         5750         5061            0          689
    HEX                     1           86           86            0            0
    INI                     1            7            3            3            1
    JSON                  328       174872       174872            0            0
    LD Script              15          412          317           48           47
    Makefile             2587        64666        43896        10957         9813
    Module-Definition       2          124          109            0           15
    Perl                   53        40583        31126         3931         5526
    Python                118        36932        29072         2980         4880
    ReStructuredText     2621       548840       417105            0       131735
    Ruby                    1           29           25            0            4
    Shell                 556        85666        59725        10972        14969
    SVG                    59        39038        37555         1405           78
    Plain Text           3230       239573            0       197373        42200
    Unreal Script           5          716          446          162          108
    Vim script              1           42           33            6            3
    XSL                    15          300          183           78           39
    YAML                  921        91167        72962         3985        14220
    ===============================================================================
    Total               65340     28511276     20985365      3704703      3821208
    ===============================================================================
[+] BlackLotus89|5 years ago|reply
Assembler. Although there is a movement to port assembler to c.
[+] blondin|5 years ago|reply
what does linux kernel in-tree support mean? that drivers can be written in rust or something?
[+] golergka|5 years ago|reply
Tangential question: what would be a career path for a typical product developer to become one of the programming gods who get invited to such discussions? (Even if it's technically open mailing list, you know what I mean). How many years of experience and how much formal CS education would it take to acsend to that level?
[+] 29athrowaway|5 years ago|reply
The problem I perceive with Rust as a language for Linux, is not that Rust is not an excellent language. It is that it evolves rather quickly and code may break when you switch versions.
[+] megous|5 years ago|reply
Does rust/llvm even support all the CPU architectures Linux supports?

If not how will that work? Will some drivers be then limited to only some architectures?

[+] ausjke|5 years ago|reply
While I like Rust I still picked Modern C++(17) for my next project. All modern languages are converging these days.

Adopted C++ because of its libraries at large. Will still learn Rust on the side. For the moment, boring and verified for product especially when they're updating and remaining up to date.

[+] zelly|5 years ago|reply
To bootstrap Rustc you have to compile every single version. Say goodbye to reproducible builds. Say goodbye to civilians with cheap laptops being able to compile their kernel.
[+] Ericson2314|5 years ago|reply
Happy to see happening!

But to get the most value out of Rust, one needs to use the Cargo ecosystem too (even for kernel code!) and that will be a much bigger political quagmire.

[+] Shorel|5 years ago|reply
My only complaint about Rust is terminology.

"Immutable variable" what an oxymoron!

That's what is known as a constant!

A variable is mutable, a constant is not. Was it that hard to understand?