Re: [TECH TOPIC] Rust for Linux

Roland Dreier
2021-07-06T20:00:46Z

On Mon, Jul 5, 2021 at 4:51 PM Linus Walleij <linus.walleij@linaro.org> wrote:

> I also repeat my challenge from the mailing list, which is that
> while I understand that leaf drivers is a convenient place to start
> doing Rust code, the question is if that is where Rust as a language
> will pay off. My suggestion is to investigate git logs for the kind of
> problems that Rust solves best and then introduce Rust where
> those problems are. I believe that this would make be the
> Perfect(TM) selling point for the language in the strict
> hypothetico-deductive sense:

One area I see where Rust could make a big improvement for drivers is
in using RAII for error paths. Drivers often have a lot of code like

if (something())
return err;

if (something_else())
goto undo_something;

if (a_third_thing())
goto undo_two_things;

and so on, which is difficult to get right in the first place and even
harder to keep correct in the face of changes.

That leads to a lot of bugs in error paths, which maybe affect no one
until they become exploitable security bugs, and also means that
maintainers get a lot of patches like
https://lore.kernel.org/linux-rdma/20200523030457.16160-1-wu000273@umn.edu/
that need careful review.

"devres" / devm_xxx was an attempt to deal with this in C, but it only
solves some cases of this and has not received a lot of adoption (we
can argue about the reasons). Better language-level support for
ownership / lifetime tied to scope would probably lead to driver code
that is better in several dimensions. Even in C++, the RAII idiom is
quite nice and I often miss it in kernel code.

- R.