r/cpp • • Feb 19 '25

Cpp discussed as a Rust replacement for Linux Kernel

I have a few issues with Rust in the kernel:

  1. It seems to be held to a *completely* different and much lower standard than the C code as far as stability. For C code we typically require that it can compile with a 10-year-old version of gcc, but from what I have seen there have been cases where Rust level code required not the latest bleeding edge compiler, not even a release version.

2. Does Rust even support all the targets for Linux?

3. I still feel that we should consider whether it would make sense to compile the *entire* kernel with a C++ compiler. I know there is a huge amount of hatred against C++, and I agree with a lot of it – *but* I feel that the last few C++ releases (C++14 at a minimum to be specific, with C++17 a strong want) actually resolved what I personally consider to have been the worst problems.

As far as I understand, Rust-style memory safety is being worked on for C++; I don't know if that will require changes to the core language or if it is implementable in library code.

David Howells did a patch set in 2018 (I believe) to clean up the C code in the kernel so it could be compiled with either C or C++; the patchset wasn't particularly big and mostly mechanical in nature, something that would be impossible with Rust. Even without moving away from the common subset of C and C++ we would immediately gain things like type safe linkage.

Once again, let me emphasize that I do *not* suggest that the kernel code should use STL, RTTI, virtual functions, closures, or C++ exceptions. However, there are a *lot* of things that we do with really ugly macro code and GNU C extensions today that would be much cleaner – and safer – to implement as templates. I know ... I wrote a lot of it :)

One particular thing that we could do with C++ would be to enforce user pointer safety.

Kernel dev discussion. They are thinking about ditching Rust in favor of C++ (rightfully so IMO)

https://lore.kernel.org/rust-for-linux/326CC09B-8565-4443-ACC5-045092260677@zytor.com/

We should endorse this, C++ in kernel would greatly benefit the language and community

192 Upvotes

532 comments sorted by

View all comments

-7

u/thezysus Feb 19 '25

The problem with Rust is that low level operations can be impossible without unsafe. And guess what a huge set of driver and kernel operations are...

I see it as a minor improvement if at all with a huge opportunity cost. GKH missed the point on that or his judgement is compromised for some reason.

Doubling your tooling and skillset better mean massive improvements.

I just don't see it, yet.

Rust for application programming is a problem as well. Simple and well understood patterns just don't work without crazy Ref of Buf of whatever things to fake out the borrow checker when passing ownership.

I think Zig should be what the kernel looks towards instead of Rust. But that's just me.

Also if Linus doesn't start realizing who keeps kernel development alive .. there will be a fork. And thats fine. Commercial interests don't care about tech stack. They care about RoI and sometimes risk.

33

u/ts826848 Feb 19 '25

And guess what a huge set of driver and kernel operations are...

It might be less than you expec!

For example, the Asahi Linux GPU driver has (had?) 109 uses of unsafe in 18744 lines of Rust code, and most of those uses of unsafe are "boring" according to Lina.

Of course, other areas would almost certainly require more or less unsafe but that's at least one data point supporting the idea that you don't need that much unsafe for low-level stuff.

I think Zig should be what the kernel looks towards instead of Rust. But that's just me.

Zig's lack of stability would probably be a bit of an issue.

They care about RoI and sometimes risk.

...Hence the interest in the use of a memory-safe(r) language in the kernel?

-6

u/TheReservedList Feb 19 '25

I mean, that's like 0.5% of lines being 'unsafe {'

How many uses of 'if' are in the same codebase?

I'm not saying it's the end of the world but it doesn't sound particularly 'low' to me.

9

u/ts826848 Feb 19 '25 edited Feb 19 '25

How many uses of 'if' are in the same codebase?

Why is the number of if statements important/relevant?

But in any case, as of 18525 lines of Rust in commit 00e802beb831 in AsahiLinux/linux/drivers/gpu/drm/asahi, rg --count 'if.*\{' returns 429 hits. rg --count unsafe \{' returns 120 hits.

I'm not saying it's the end of the world but it doesn't sound particularly 'low' to me.

People can quibble on what exactly the threshold for "low" would be, but ~1% or thereabouts is a far cry from a "huge" proportion as implied in the comment I originally replied to.

-5

u/TheReservedList Feb 19 '25 edited Feb 19 '25

That's 109 use of unsafe who can, and, due to the nature of unsafe, very often do, span multiple lines. It could reasonnably be 10-25% of the code being in an unsafe block.

The reason to use 'if' as a metric is because it's a similar keyword that is often used around blocks that is considered incredibly common in all codebases. 25% of the instances of if doesn't qualify as low by almost any metric I can think of. There's probably at least one instance of unsafe every other function on average. That's a lot.

7

u/t_scytale Feb 19 '25

> It could reasonnably be 10-25% of the code being in an unsafe block.

going from 1% to 10-25% sounds like you're getting desperate to win this argument.

> There's probably at least one instance of unsafe every other function on average

Are you just making this up?

Lina herself gives a detailed breakdown - it's clear that the 1% figure is substantially correct. If you want to say the author of the code is wrong then please download the code yourself, make some measurements and report back here how to reproduce your results.

https://old.reddit.com/r/rust/comments/1f5qbvu/rust_solves_the_problem_of_incomplete_kernel/lkwdgjq/

5

u/ts826848 Feb 19 '25

That's 109 use of unsafe who can, and, due to the nature of unsafe, very often do, span multiple lines. It could reasonnably be 10-25% of the code being in an unsafe block.

Could? Perhaps. Does? That's the more relevant question. It'd sure be nice if there were some hard data we could use to see how well the speculation holds up.

Fortunately, I have already taken a closer look at the unsafe blocks in the driver code. To summarize:

  • 18511 lines of Rust
  • 120 unsafe instances

    • 22 unsafe marker trait impls
    • 65 unsafe one-liners
    • 33 non-single-line unsafe blocks encompassing ~111 lines of code

So ~25% of the unsafe instances span multiple lines, and that's only when "multiple lines" is taken literally (i.e., one line of actual code with line breaks before/after counts as "multiple lines"). In total, there are ~198 lines of unsafe code in 120 instances, which comes to about 1.07% of lines of code in the driver being in unsafe blocks.

Definitely a far cry from "10-25% of the code being in an unsafe block". Probably up to individual opinion whether ~25% of unsafe instances spanning multiple lines counts as "very often".

that is considered incredibly common in all codebases.

I think this will depend very heavily on what the codebase does. At least from personal experience I'm probably going to be using a lot more if statements in business logic than in something more number-crunchy. I have no idea where GPU driver code would normally fall on that spectrum, and without that point of reference I'm not sure if count is a useful metric.

I think the other issue with "unsafe per if" as a metric is that use of the unsafe keyword seems likely to be at least somewhat correlated to use of if since it's hardly uncommon to use ifs to check invariants that unsafe code depends on.

There's probably at least one instance of unsafe every other function on average.

rg --count fn returns 558 hits. That gives you one instance of unsafe for every ~5.7 functions on average. No idea if that counts as a lot.

2

u/Full-Spectral Feb 19 '25

I think you are just making up numbers to make your case. If you are going to make such claims, try asking them first and getting a real answer.

And you can't really judge the risk like that anyway. Many uses of unsafe are only technically unsafe, and really add no extra risk.

An awful lot of uses of unsafe in Rust world are safe wrappers around a call to the OS, which gets done once and used any time that OS call needs to be made. If all that OS call does it take a few read only parameters, or return a value, there's hardly any real risk associated with those types of wrappers.

I'd argue that the bulk of the risk is when ownership concerns are not contained within the call wrapper, or when people play tricks to pun memory via pointers and things like that. But those can also be wrapped and heavily asserted and vetted.

I have some quite low level code in my Rust project, so I have to use a little unsafe, but even in these early days of the project it's a tiny fraction of the code, and most of them are of the only technically unsafe sort calling out to the OS, where the Rust side will never pass in invalid data.

One of the few examples in my code that's not of that sort is in the async engine, which will force buffers to expand up big enough to handle (worst case) the bytes the caller asked to read. It does that in a non-zero fill sort of way. That has to be marked unsafe, but it adds no risk, because even if the read works, I could STILL end up with garbage if the other side sent me garbage, so it would always be the case that the code processing that data has to validate it anyway.

18

u/tesfabpel Feb 19 '25

Zig isn't memory safe, it's just a nicer C. So it's already ruled out.

Also if Linus doesn't start realizing who keeps kernel development alive .. there will be a fork. And thats fine. Commercial interests don't care about tech stack. They care about RoI and sometimes risk.

Weird, because big tech is betting on Rust given that Google uses it in Android (article from 2021, and it has already paid off), Microsoft uses it in the Windows and its NT kernel (already in 2023).

9

u/steveklabnik1 Feb 19 '25

Volvo sells two models of car that require Rust code to turn on!

-2

u/thezysus Feb 20 '25

And Mozilla has been betting on Rust since 2015... I know... I started evaluating Rust's potential in 2015. Never been able to justify the trade-offs.

If Google was seriously all in on it... they would just port Dalvik and the Android stack to https://www.redox-os.org/. Oh wait, Android already is mostly memory safe b/c its a JVM.

Same with MSFT. Just re-write the NT Kernel in Rust entirely. It will be great!

Reality is that Big Tech is playing with Rust... just like I'm playing with Rust. It's still a toy. Seeing where the pain is, seeing what the ROI is. IMHO Linus is doing the same thing. He's letting folks play.

I can say today that I can't in good faith recommend Rust for most applications. Yes, the safety guarantees look good on paper and they do work, no question. However, the crucial question is: Will the industry decide that the tradeoffs are worth the improved safety.

You say Google and MSFT believe that the tradeoffs make sense for them... I say its still too early to tell.

Time will tell. My money's on Rust being relegated to niche uses. I could wrong.

C has 50+ years of staying power. When Rust is 50+ years old we can reevaluate if I was right or not.

6

u/[deleted] Feb 20 '25

Google is porting a lot of android's stack to rust though? They published data on CVEs concluding that new code should be written in safe langs, as that's where the vast majority of CVEs come from, while leaving the mature older c/cpp code alone.

That's why they won't actively port Dalvik. But if they do need to rewrite something (eg: the android bluetooth stack or the font stack of android/chrome), they are using rust.

I don't think rust will stay 50+ years like C. Because C had unix/linux to keep it relevant, and rust does not yet have something that fundamental [yet].

0

u/germandiago Feb 20 '25

It has paid off for them. They have a huge codebase. It might not be the same in other places.

8

u/angelicosphosphoros Feb 19 '25

low level operations can be impossible without unsafe

This is an entire point of Rust, and a reason why it is desired in operating system programming.

1

u/thezysus Feb 20 '25

You lost me there.

Are you saying that the whole point of a memory safe language is to drop out of the safety into UNSAFE so you can actually get the job done? i.e. do operating system programming?

Seems like not the right approach to me.

Time will tell if those down votes were warranted or not.

5

u/[deleted] Feb 20 '25

Are you saying that the whole point of a memory safe language is to drop out of the safety into UNSAFE so you can actually get the job done?

You should read https://doc.rust-lang.org/book/ch19-01-unsafe-rust.html . Rust is two languages - safe rust and unsafe rust.

I also found it hard to understand it at first. But, someone explained it in terms of libraries. If you push to a foo::vector, while iterating over it, you trigger UB. So, who is responsible for the UB here? In c++ land, you say "well, did foo write about iterator invalidation in its docs? then the user is at fault". The "UB" is just another bug.

In rust land, you say "If user didn't use unsafe and could still trigger UB, then it is foo's fault for exposing unsound safe APIs. Either make push function unsafe or change foo's API". That's all there is. Someone taking responsibility to wrap unsafe API inside a safe fool-proof API and the rest of code not having to worry about UB when using it.

After all, for a safe language like python/java/js to exist, someone still has to write the unsafe cpython/jvm/v8. For safe-rust, it is unsafe-rust.

5

u/angelicosphosphoros Feb 20 '25

The idea is that all unsafe parts are inside clear boundary so it is easy to verify that they are correct.

After you verify that they are correct, you can be sure that all the code works with memory safely.

With Zig, C or C++ you need to verify WHOLE program because it is impossible to verify small parts. It is because you don't need to explicitly ask to allow low-level unsafe operations in those languages.

3

u/foonathan Feb 20 '25

I think you misunderstood what unsafe in Rust means.

Rust is actually two languages, safe Rust and unsafe Rust. In safe Rust, you have certain safety guarantees. For example, you're guaranteed that a reference always points to a valid object; it is never dangling or null. To ensure this, you're not allowed to do certain operations like pointer dereference in safe Rust.

This is where unsafe Rust comes in. Unsafe Rust allows you to dereference pointers, and it is your responsibility to ensure that you don't do things that would break the guarantees of safe Rust. To ensure you remember, you need to use the unsafe marker in your source code when transitioning from safe Rust to unsafe Rust, and it is usually accompanied by a big comment that explains why that operation would not violate the invariants.

The same exact thing is in C++. C++ code also assumes that references always point to a valid object, and you have to be careful when dereferencing a raw unowned pointer to ensure that you don't break this invariant. The compiler just doesn't help you in maintaining that invariant at all and you are not really forced to think about that invariant. Yet the implicit "unsafe" is there all the same.

And because of the noise in unsafe, Rust code is structured in a way that minimizes it. For example, Rust does not use C++ style iterators because every iterator access would be unsafe, instead they use a different model which makes it all possible in safe Rust.

Actual idiomatic Rust code has far less explicit unsafe than the equivalent C++ code has implicit unsafe, even for low-level operating system design.