HomeVideos

Why Rust is coming to the Linux kernel

Now Playing

Why Rust is coming to the Linux kernel

Transcript

261 segments

0:00

Do do you think Linux at some point uh

0:02

might support Rust or and you know what

0:05

what are your your what is your thinking

0:07

of doing things outside of C?

0:09

>> So we have 25,000 lines of Rust in the

0:11

kernel already.

0:12

>> Oh, we do.

0:14

>> Okay, awesome.

0:14

>> Yeah. Um so most of that is just

0:16

bindings. There's no real functionality.

0:18

Um in the latest release, um if the

0:20

kernel crashes, it'll put up a QR code.

0:23

You can take a picture of it to get the

0:24

crash jump. That code was written in

0:25

Rust.

0:26

>> Oh, nice.

0:26

>> Um that's in Rest. Um so the rest the

0:30

perinx developers have been working for

0:31

a long time. A couple years ago they

0:32

came to us and said we think we have we

0:34

think we're ready to do this. Do you

0:36

want it? And we said yeah let's try this

0:38

experiment. You're willing to do the

0:40

work. Who am I to tell no to? Um I mean

0:44

it's classic.

0:45

>> Yeah. I mean it's it's it's now the

0:48

problem with Linux and Rust is

0:51

>> um it would be easier to write a core

0:53

piece of Linux and Rust than it would be

0:54

to write a driver. that drivers consume

0:56

from everywhere in the kernel.

0:58

>> So you want to talk to locking, you want

1:00

to talk input and output, you want to

1:02

talk talk to the driver model, talk to

1:04

the USB port, all this stuff. Drivers

1:05

have to can be really tiny because they

1:07

take resources from the rest of the

1:09

kernel. In Rust, you need to have a

1:11

binding between the C code and the Rust

1:13

code. There's an intermediate layer. The

1:15

C the kernel in C has these very

1:18

opinionated model ideas of how it

1:19

handles objects and how it does memory

1:21

and how it it has its memory model. Rust

1:24

has its very opinionated model of how it

1:26

does this type same idea. This meshing

1:28

is tough.

1:30

>> This meshing is also the most crazy

1:31

complex Rust code you've ever seen. So

1:34

from a new Rust developer like me, I can

1:37

barely read the bindings, but I trust

1:38

other people are doing it. Um, so yes,

1:41

so the trick is we now need to write a

1:44

binding for every different part of the

1:46

kernel in order to write a R scope, a

1:48

rush driver. If you want to do the QR

1:50

generator, that's simple. That was just

1:51

one function. you go.

1:53

>> So over the year, the past couple years,

1:55

people have been writ bindings to try

1:57

and do things. We've had a bunch of

1:59

example drivers like a new disc driver,

2:01

this writer driver in C versus Rust. It

2:03

turns out there are still some

2:04

performance issues with R code versus C

2:06

code because we can do some tricks in C

2:08

that they can't do yet in R.

2:09

>> Yeah.

2:10

>> Um that's and the tooling and the RS

2:12

developers are doing it. The core RS

2:14

developers that the language, some of

2:15

them are Linux kernel developers.

2:16

They've always wanted R to be working

2:18

for Linux. Um the rest model is good.

2:21

Memory safety at our level does not mean

2:23

that you can't crash the kernel. Uh you

2:25

can still overwrite things. It memory

2:27

safety in Rust just means the the memory

2:29

that you pass around you think you have

2:31

an ownership of or it isn't an ownership

2:33

of.

2:34

>> Y

2:34

>> and it when things are go out of scope,

2:36

they'll get cleaned up properly. So I've

2:38

seen every single kernel bug for the

2:40

past 18 years. Um

2:43

half of them will be fixed with Rust.

2:45

>> It's just it's just going to be fixed

2:47

with Rust. Um, it's the stupid oneoff

2:50

bugs. It's the I Oops, I overwrote an

2:52

array and I didn't realize it by one.

2:54

Oops, I um forgot to clean up this error

2:56

path.

2:57

>> Yeah,

2:57

>> I forgot to unlock this lock. It's I

2:59

It's stupid little things like that.

3:01

There's logic bugs. Of course, you can

3:03

write logic bugs in Rust.

3:04

>> You'll always have those,

3:05

>> right? So, but famously, the code the QR

3:08

code for that did in Rust that made a QR

3:10

C passed into the Rust code a pointer to

3:12

a buffer and the buffer size. the Rust

3:15

code forgot to look at how big the

3:17

buffer was and it scribbled right over

3:18

memory. So you can write memory unsafe

3:20

code in R just fine and you can crash

3:22

things in Rust. So memory safety here

3:25

means it's the safety of object life

3:28

cycles and things like that. It doesn't

3:29

mean it's going to remove all bugs. It's

3:31

not a golden um bullet or anything like

3:34

silver bullet. But I think yes I think

3:37

Rust needs to come in because it should

3:40

be easier to write drivers in this

3:42

stuff. We have a lot of issues with

3:44

lifetime rules of when you yank out a

3:47

device, devices are dynamic and dealing

3:49

with these reference counting of things

3:51

like that is very tricky to get right.

3:53

There's parts in the colonel we still do

3:54

not have it right and we know we don't

3:55

have it right.

3:56

>> Rust is forcing us to actually document

4:00

our C code better

4:01

>> and it's cleaning up. So if Rust

4:03

disappeared tomorrow, I've had to clean

4:04

up code in the driver core. It's like,

4:06

oh yeah, I guess we can do things better

4:08

and safer in the C code in order to make

4:10

Rust easier. Mhm.

4:11

>> And we have and so it's making us

4:13

rethink how we do a lot of our existing

4:15

code in the kernel. To be fair, a lot of

4:18

core kernel people are very resistant to

4:20

that. They don't like change, don't like

4:22

different languages. Um, one core kernel

4:24

developer said, "I don't like working

4:25

with a project that has um multiple

4:27

languages in it just because it's tricky

4:30

and they are free to do that." Um, we're

4:32

not stepping on they're not stepping on

4:34

anybody's toes. Um, a lot of it's

4:35

miscommunication and a lot of it comes

4:37

down to people. Again,

4:38

>> famously in this binding, I wrote the

4:42

driver core many, many years ago of how

4:43

drivers worked in the s in the kernel.

4:45

>> There had to be a binding for that in

4:47

Rust.

4:48

>> I this code I saw I said, "This is

4:51

horrible. This isn't going to work at

4:52

all. It's miserable." And um I went and

4:55

actually met with the developers and we

4:57

had there's a Rust Linux conference. We

4:59

sat down. I think they gave a whole

5:00

presentation just for me. Um turns out I

5:03

was wrong and they were wrong. we both

5:04

were wrong. And they were doing crazy

5:06

things like they had a thousand lines of

5:08

C Rust code that that I do in two lines

5:10

of CC code. I'm like, well, why? They're

5:12

like, well, we didn't want to change the

5:13

CC code. I'm like, we can change the C

5:15

code because I just did that because it

5:16

was easy in C, but if I change that, you

5:18

get rid of a thousand lines of R. Let's

5:19

do that. And again, it comes down to,

5:21

okay, understanding what your problems

5:23

are, understanding what my problems are,

5:24

and let's work together. And now we have

5:26

bindings in the kernel that you can

5:27

actually write some drivers with. And

5:29

the Red Hat developers are starting to

5:31

write the new Nvidia GPU drivers in Rust

5:33

and they're starting putting the

5:35

proposals out there. The Apple um um GPU

5:38

drivers are for the Apple MacBooks are

5:40

written in Rust. Those patches are not

5:42

merged, but they're written in Rust and

5:44

Prove on on a fork. Um that works great.

5:46

Um there's a whole bunch of crazy object

5:48

life cycle issues with graphics drivers

5:50

and Rust makes it a lot easier for them

5:52

to do. Um, I think you'll see a lot more

5:54

of the driver simple stupid drivers for

5:57

hardware devices being written in Rust

5:59

because all you want to do is read and

6:00

write to some random memory bits and

6:02

it's really easy to do that in Rust and

6:03

you can do it in actually less code than

6:04

you can do it in C code.

6:06

>> Yeah.

6:06

>> And I think that's we now have the

6:08

infrastructure in there. So I think

6:09

we've hit the tipping point. We'll start

6:11

seeing new stuff in there and we need to

6:12

do that. I mean there's mandates from

6:13

governments that you can't use memory

6:16

unsafe languages like C and products.

6:18

>> Yeah. And if I want to see Linux to

6:19

succeed, which I do, we're going to have

6:22

to change. And I can say going forward,

6:24

if you want to write in rest, you can

6:26

write in rest. Now, that being said, we

6:27

still have 40 million lines of C code.

6:28

Yeah.

6:29

>> So, we have some very, very good

6:30

developers out there working on

6:31

mitigating the problems we have in C. We

6:33

now have bounds checking for our stuff.

6:35

We now have other, we call them seat

6:37

belts and airbags that protect your C

6:40

code from doing stupid things. And we

6:42

working with the compiler authors to add

6:43

new extensions to C and make things

6:45

safer for the C code because we want to

6:48

protect the code that we have today

6:49

because you're not going to rewrite code

6:50

in Rust. Don't worry about that. Um

6:53

Google famously published something

6:54

recently saying over the past couple

6:56

years we've written our new code in Rust

6:58

and we got uh overwhelmingly more secure

7:00

because we didn't touch the old code and

7:03

bugs degrade over time. There's still

7:05

going to be bugs in the older stuff, but

7:06

most bugs happen in your new code, not

7:08

in your old code.

7:09

>> That's awesome. I'm I'm I'm sensing

7:10

you're you're excited about Russ and I I

7:12

it's also just nice to see the

7:14

evolution.

7:14

>> Yeah, it's evolution and see what

7:16

happens and if it fails tomorrow, we can

7:17

rip it out and what but we have

7:19

developers willing to do this work for

7:20

us.

7:21

>> It's not intruding on other people's

7:22

stuff.

7:23

>> Well, then I I I think it does go back

7:24

to what you said earlier is is it's feel

7:27

I understand that a big part of Linux is

7:30

like show the work like if if if it

7:32

works and and same thing, you know, it

7:33

sound like that's how Rust started and

7:35

how it's also how it's progressing.

7:36

People are showing that it works.

7:37

They're proving that it works, it solves

7:40

their problem. Yeah. It maybe even works

7:42

better for them. And then, you know,

7:43

step by step.

7:44

>> Yeah. Like people are like, well, why

7:44

not Zigg or Hair? Those are other good

7:46

languages. Um, I'm like, that's great,

7:48

but nobody's proposed.

7:49

>> Yeah. [laughter]

7:50

>> So, yeah, they want to do that. And to

7:52

be fair, I think those developers who

7:53

work on those languages don't care about

7:54

Linux, which is fine. They'll not you.

Interactive Summary

The transcript discusses the integration of the Rust programming language into the Linux kernel as an experiment to improve memory safety and code quality. The speaker explains that while Linux remains primarily C-based with 40 million lines of code, Rust is being introduced for drivers and new functionality, such as crash-dump QR codes. The conversation covers the technical difficulties of creating bindings between C and Rust's differing memory models, the potential for Rust to eliminate common types of bugs, and how this effort is actually improving existing C code by forcing better documentation and cleaner structures.

Suggested questions

5 ready-made prompts

Recently Distilled

Videos recently processed by our community