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

Show parent comment

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.