Are there any advantages of using Rust instead of C in the Linux kernel?

3 years ago by BearPear to c/linux

Will there be performance and security improvements?

0v0 95 points 3 years ago

Memory safety would be the main advantage.

path: 0 2536215, hotness: undefined, score: 95, children: 0
ExLisper 56 points 3 years ago

It's a better, more modern language in general. It has way better tooling (better, more user friendly compiler, better package manager), really good set of modern features (null-safety, good error handling, type-classes, algebraic types), it's easier to modularize your code (workspaces, modules). Rust does a lot of things right and is fun to work with. That's why it's the most liked language overall. It's not hype, it really is that good. It will just make working on the kernel easier. And on top of that it offers some memory safety and concurrency features.

P.S. I forgot about amazing documentation. Again, way better then what you can find for C.

P.P.S Zero cost abstractions.

path: 0 2552016, hotness: undefined, score: 56, children: 4
duncesplayed 41 points 3 years ago

The "tooling" argument is kind of backwards when we're in the kernel. The package manager is not allowed to be used. Even the standard library is not allowed to be used. Writing code free of the standard library is kind of new in the Rust world and getting compiler support for it has been one of the major efforts to get Rust into the kernel. Needless to say tools around no-stdlib isn't as robust as in the user world.

path: 0 2552016 2557044, hotness: undefined, score: 41, children: 3
ExLisper -7 points 3 years ago path: 0 2552016 2557044 2563702, hotness: undefined, score: -7, children: 2
cmeerw 13 points 3 years ago

That link appears to be for a Windows driver.

path: 0 2552016 2557044 2563702 2566822, hotness: undefined, score: 13, children: 1
ExLisper -4 points 3 years ago

Ups, you're right. Still, better tooling attracts more devs to a language increasing the number or people that in the future may work on the kernel and in the future it could be possible to build Linux drivers using cargo. So yeah, today it's not that important but it still a benefit.

path: 0 2552016 2557044 2563702 2566822 2572128, hotness: undefined, score: -4, children: 0
ProtonBadger 54 points 3 years ago

I've been watching Asahi Lina develop a big GPU driver for Apple silicon and development was so much faster because a whole category of bugs were largely absent once the code compiled, and memory issues are notoriously difficult to fix. Also error handling is easier and much cleaner.

She wrote about it here and here.

path: 0 2547008, hotness: undefined, score: 54, children: 0
ShittyKopper 46 points 3 years ago

Security? Probably. I wouldn't expect any measurable improvements to performance but the with compiler being able to do more checks it might enable some clever optimization trickery that would be harder to maintain in C.

Still, Rust on the kernel probably won't leave the realm of drivers any time soon, so it all depends on if you have the hardware that will use a driver written in Rust.

path: 0 2536217, hotness: undefined, score: 46, children: 1
Daybowbow 32 points 3 years ago

Also to add: allowing Rust can bring in more developers to the kernel, given the growing popularity.

path: 0 2536217 2537524, hotness: undefined, score: 32, children: 0
apt_install_coffee 37 points 3 years ago

Memory safety is likely to prevent a lot of bugs. Not necessarily in the kernel proper, I honestly don't see it being used widely there for a while.

In third party drivers is where I see the largest benefit; there are plenty of manufacturers who will build a shitty driver for their device, say that it targets Linux 4.19, and then never support/update it. I have seen quite a few third party drivers for my work and I am not impressed; security flaws, memory leaks, disabling of sensible warnings. Having future drivers written in rust would force these companies to build a working driver that didn't require months of trawling through to fix issues.

Now that I think about it, in 10 years I'll probably be complaining about massive unsafe blocks everywhere...

path: 0 2543687, hotness: undefined, score: 37, children: 1
ozymandias117 11 points 3 years ago

Haha. At least you immediately caught exactly what they’ll end up doing if they bother to use Rust at all 😂

path: 0 2543687 2549594, hotness: undefined, score: 11, children: 0
Killercat103 31 points 3 years ago

Performance? Not really no. I believe C is slightly faster with Rust and C++ competing for second place. The benefit is safer code as Rust is built with performance and safety in mind. It highlights what potential errors can be found where making human error way less common. Instead of potential null errors types are wrapped in an option enumerator which ensures you know there can be a lack of a value. Expections are also enumerators done similarly with a result object so you know which functions may fail. Instead of using memory and potentially forgetting to free it we have the ownership system.

path: 0 2536459, hotness: undefined, score: 31, children: 15
rikudou 8 points 3 years ago

How is C faster than C++? Unless you use virtual functions, it's as performant as C. And you definitely wouldn't use virtual functions in a kernel.

path: 0 2536459 2537417, hotness: undefined, score: 8, children: 14
skullgiver 25 points 3 years ago

[This comment has been deleted by an automated system]

path: 0 2536459 2537417 2539727, hotness: undefined, score: 25, children: 0
anti_antidote 23 points 3 years ago

C++ is only as fast as C if you use only the parts of C++ that are identical to C. In other words, C is faster than C++

path: 0 2536459 2537417 2537743, hotness: undefined, score: 23, children: 11
Haugerud 11 points 3 years ago

You can use compile time polymorphism in C++ without any runtime performance cost.

path: 0 2536459 2537417 2537743 2539814, hotness: undefined, score: 11, children: 4
PuppyOSAndCoffee 0 points 3 years ago

Compile time has got to be part of the convo esp when it comes to the kernel. The Linux kernel is one of the few bits where end-users are actively encouraged to compile from source. It is a feature!

Adding C++ compilitis is pain for what gain, from a kernel pov.

I am not a big fan of c++ overall however that is because other languages have emerged that are sweeter than C that gate some of the people issues with C++.

Anyone who has ever had a thing that was like a thing but not exactly the thing, in C, knows C ain’t great at that.

path: 0 2536459 2537417 2537743 2539814 2543098, hotness: undefined, score: 0, children: 0
anti_antidote -5 points 3 years ago path: 0 2536459 2537417 2537743 2539814 2544264, hotness: undefined, score: -5, children: 2
lloram239 6 points 3 years ago
path: 0 2536459 2537417 2537743 2564954, hotness: undefined, score: 6, children: 5
Cpo 2 points 3 years ago

Aside from that I think C is more performant than C++ (indeed when you use the bells and whistles that C++ offers), you are comparing the libraries with each other.

The fact that the implementation of one random std::Sort is faster than the implementation of qsort() is comparing libraries, not the languages. You are comparing the algorithm of the Rust Sort with quicksort (which is obviously the qsort you are referring to.

I am certain there are sort implementations in C which outperform Rust.

Having said that, I immensely enjoy Rust because it forces me to think about the error handling and it does not give me the quirks of C/C++ (index out of bounds, memory corruption).

path: 0 2536459 2537417 2537743 2564954 2567348, hotness: undefined, score: 2, children: 4
Zatujit 2 points 3 years ago

Since C++ has Turing complete compiling, I guess technically it can go infinite compilation time

path: 0 2536459 2537417 2597390, hotness: undefined, score: 2, children: 0
dinosaurdynasty 11 points 3 years ago

Potentially stability improvements as well (for the same reasons as the security improvements), especially for lesser used drivers and stuff.

path: 0 2542016, hotness: undefined, score: 11, children: 0
YaBoyMax 10 points 3 years ago

Yes, enhanced security is pretty much the entire pitch of Rust. There wouldn't be any reason for it to result in performance enhancements, though.

path: 0 2536127, hotness: undefined, score: 10, children: 5
skullgiver 10 points 3 years ago

[This comment has been deleted by an automated system]

path: 0 2536127 2539810, hotness: undefined, score: 10, children: 4
nitefox 8 points 3 years ago

Not all guarantees are gone, even with unsafe

path: 0 2536127 2539810 2543389, hotness: undefined, score: 8, children: 0
ProtonBadger 6 points 3 years ago

Well, it largely removes an attack surface for memory bugs, which is a huge thing. If we're writing a big driver (see the Rust driver for the Apple GPU) then suddenly waving hands incoherently 90% or more of the driver (depending) is likely to be much more memory safe and stable. As has been demonstrated with that particular driver already.

I was watching the streams and when it compiled Asahi Lina usually only had to deal with logical type errors, not memory issues, it was basically a great showcase for Rust and memory safely. Unsafe is perfectly fine Rust, but it's a contract where the developer says to the compiler: "I know you can't guarantee this block is safe, so I'll keep a special eye on that, peer review more, test, etc. while you keep an eye on all the other code I can't fit in my head". In the case of Linux an Unsafe blocks means "we'll trust the Linux kernel code we connect to, though review it carefully".

So saying all safety goes out the window is wrong, see it as a vastly reduced potential for memory problems, better error handling and more stable drivers, as demonstrated by the Apple GPU driver.

path: 0 2536127 2539810 2547968, hotness: undefined, score: 6, children: 1
skullgiver 4 points 3 years ago

[This comment has been deleted by an automated system]

path: 0 2536127 2539810 2547968 2550795, hotness: undefined, score: 4, children: 0
staticlifetime 4 points 3 years ago

It just depends on how isolated that part of the kernel is. Unsafe code should be done only in interop, and so it still theoretically has a memory safety benefit over C in that sense.

In terms of how much interop code needs to be written for Rust at this point is another discussion though.

path: 0 2536127 2539810 2542722, hotness: undefined, score: 4, children: 0
fugepe 1 point 3 years ago

Omnipresent forced memory safety.

path: 0 2540501, hotness: undefined, score: 1, children: 0
PuppyOSAndCoffee -6 points 3 years ago

I would choose zig over rust; while zig isn’t as safe as rust (nor is that zig’s aim) zig also is also safer than C. Zig still has a bit to go.

Also rust compile times … in the kernel … that might matter more?

path: 0 2542884, hotness: undefined, score: -6, children: 6
nitefox 9 points 3 years ago

Then why would you choose zig?

path: 0 2542884 2543297, hotness: undefined, score: 9, children: 5
PuppyOSAndCoffee 0 points 3 years ago

multi-arch compile that is getting faster by the release, sweeter & safer than C — it isn’t unsafe, mind you — why wouldn’t you pick zig for systems development? And it’s compiled bins sizzle!

Plus, there is no C interop; C just plugs in.

The grammar is still in the oven, but once it bakes, I predict zig takes over C as a perf layer in script pkgs (python, node, etc).

Compile times are crisp but some foundational elements (incremental compile etc) will help it positively fly. Already it’s smoking rust however we expect more.

The grammar is why it is one to watch.

path: 0 2542884 2543297 2543772, hotness: undefined, score: 0, children: 4
JulianRR 13 points 3 years ago

So. Compile times?

I'm willing to have slower compile times for more stable software.

path: 0 2542884 2543297 2543772 2545651, hotness: undefined, score: 13, children: 3
PuppyOSAndCoffee 0 points 3 years ago

Makes for a nice call out on a slide but in reality? Are we sure? Why not both?

path: 0 2542884 2543297 2543772 2545651 2569227, hotness: undefined, score: 0, children: 2
linux
linux

@lemmy.ml

login for more options
67083
9205
4151

From Wikipedia, the free encyclopedia

Linux is a family of open source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991 by Linus Torvalds. Linux is typically packaged in a Linux distribution (or distro for short).

Distributions include the Linux kernel and supporting system software and libraries, many of which are provided by the GNU Project. Many Linux distributions use the word "Linux" in their name, but the Free Software Foundation uses the name GNU/Linux to emphasize the importance of GNU software, causing some controversy.

Rules

  • Posts must be relevant to operating systems running the Linux kernel. GNU/Linux or otherwise.
  • No misinformation
  • No NSFW content
  • No hate speech, bigotry, etc

Related Communities

Community icon by Alpár-Etele Méder, licensed under CC BY 3.0

go to feed...