logo

drewdevault.com

[mirror] blog and personal website of Drew DeVault git clone https://hacktivis.me/git/mirror/drewdevault.com.git

Does-Rust-belong-in-Linux.md (10120B)


  1. ---
  2. title: Does Rust belong in the Linux kernel?
  3. date: 2022-10-03
  4. ---
  5. I am known to be a bit of a polemic when it comes to Rust. I will be forthright
  6. with the fact that I don't particularly care for Rust, and that my public
  7. criticisms of it might set up many readers with a reluctance to endure yet
  8. another Rust Hot Take from my blog. My answer to the question posed in the title
  9. is, of course, "no". However, let me assuage some of your fears by answering
  10. a different question first: does Hare belong in the Linux kernel?
  11. If I should owe my allegiance to any programming language, it would be
  12. [Hare](https://harelang.org). Not only is it a systems programming language that
  13. I designed myself, but I am using it [to write a kernel][helios]. [Like
  14. Rust][redox], Hare is demonstrably useful for writing kernels with. One might
  15. even go so far as to suggest that I consider it superior to C for this purpose,
  16. given that I chose to to write Helios in Hare rather than C, despite my
  17. extensive background in C. But the question remains: does Hare belong in the
  18. Linux kernel?
  19. [helios]: https://git.sr.ht/~sircmpwn/helios
  20. [redox]: https://www.redox-os.org/
  21. In my opinion, Hare does not belong in the Linux kernel, and neither does Rust.
  22. Some of the reasoning behind this answer is common to both, and some is unique
  23. to each, but I will be focusing on Rust today because Rust is the language which
  24. is actually making its way towards mainline Linux. I have no illusions about
  25. this blog post changing that, either: I simply find it an interesting case-study
  26. in software engineering decision-making in a major project, and that's worth
  27. talking about.
  28. Each change in software requires sufficient supporting rationale. What are the
  29. reasons to bring Rust into Linux? A kernel hacker thinks about these questions
  30. differently than a typical developer in userspace. One could espouse the
  31. advantages of Cargo, generics, whatever, but these concerns matter relatively
  32. little to kernel hackers. Kernels operate in a heavily constrained design space
  33. and a language has to fit into that design space. This is the first and foremost
  34. concern, and if it's awkward to mold a language to fit into these constraints
  35. then it will be a poor fit.
  36. Some common problems that a programming language designed for userspace will run
  37. into when being considered for kernelspace are:
  38. - Strict constraints on memory allocation
  39. - Strict constraints on stack usage
  40. - Strict constraints on recursion
  41. - No use of floating point arithmetic
  42. - Necessary evils, such as unsafe memory use patterns or integer overflow
  43. - The absence of a standard library, runtime, third-party libraries, or other
  44. conveniences typically afforded to userspace
  45. Most languages can overcome these constraints with some work, but their
  46. suitability for kernel use is mainly defined by how well they adapt to them
  47. — there's a reason that kernels written in Go, C#, Java, Python, etc, are
  48. limited to being research curiosities and are left out of production systems.
  49. As Linus recently put it, "kernel needs trump any Rust needs". The kernel is
  50. simply not an environment which will bend to accommodate a language; it must go
  51. the other way around. These constraints have posed, and will continue to pose, a
  52. major challenge for Rust in Linux, but on the whole, I think that it will be
  53. able to rise to meet them, though perhaps not with as much grace as I would
  54. like.
  55. If Rust is able to work within these constraints, then it satisfies the ground
  56. rules for playing in ring 0. The question then becomes: what advantages can Rust
  57. bring to the kernel? Based on what I've seen, these essentially break down to
  58. two points:[^benefits]
  59. 1. Memory safety
  60. 2. Trendiness
  61. [^benefits]: There are some other arguable benefits which mainly boil down to
  62. finding Rust to have a superior language design to C or to be more enjoyable
  63. to use. These are subjective and generally are not the most important traits
  64. a kernel hacker has to consider when choosing a language, so I'm leaving them
  65. aside for now.
  66. I would prefer not to re-open the memory safety flamewar, so we will simply move
  67. forward with the (dubious) assumptions that memory safety is (1) unconditionally
  68. desirable, (2) compatible with the kernel's requirements, and (3) sufficiently
  69. provided for by Rust. I will offer this quote from an unnamed kernel hacker,
  70. though:
  71. > There are possibly some well-designed and written parts which have not
  72. > suffered a memory safety issue in many years. It's insulting to present this
  73. > as an improvement over what was achieved by those doing all this hard work.
  74. Regarding "trendiness", I admit that this is a somewhat unforgiving turn of
  75. phrase. In this respect I refer to the goal of expanding the kernel's developer
  76. base from a bunch of aging curmudgeons writing C[^ageism] towards a more
  77. inclusive developer pool from a younger up-and-coming language community like
  78. Rust. C is boring[^boring] — it hasn't really excited anyone in decades.
  79. Rust is exciting, and its community enjoys a huge pool of developers building
  80. their brave new world with it. Introducing Rust to the kernel will certainly
  81. appeal to a broader audience of potential contributors.
  82. [^boring]: A trait which, I will briefly note, is actually desirable for a
  83. production kernel implementation.
  84. [^ageism]: A portrayal which, though it may have a grain of truth, is largely false
  85. and offensive to my sensibilities as a 29-year-old kernel hacker. For the
  86. record.
  87. But there is an underlying assumption to this argument which is worth
  88. questioning: is the supply of Linux developers dwindling, and, if so, is it to
  89. such and extent that it demands radical change?
  90. Well, no. Linux has consistently enjoyed a tremendous amount of attention from
  91. the software development community. This week's release of Linux 6.0, one of the
  92. largest Linux releases ever, boasted more than 78,000 commits by almost 5,000
  93. different authors since 5.15. Linux has a broad developer base reaching from
  94. many different industry stakeholders and independent contributors working on the
  95. careful development and maintenance of its hundreds of subsystems. The scale of
  96. Linux development is on a level unmatched by any other software project —
  97. free software or otherwise.
  98. Getting Rust working in Linux is certainly an exciting project, and I'm all for
  99. developers having fun. However, it's not likely to infuse Linux with a
  100. much-needed boost in its contributor base, because Linux has no such need.
  101. What's more, Linux's portability requirements prevent Rust from being used in
  102. most of the kernel in the first place. Most work on Rust in Linux is simply
  103. working on getting the systems to cooperate with each other or writing drivers
  104. which are redundant with existing C drivers, but cannot replace them due to
  105. Rust's limited selection of targets.[^gcc] Few to none of the efforts from the
  106. Rust-in-Linux team are likely to support the kernel's broader goals for some
  107. time.
  108. [^gcc]: Rust in GCC will help with this problem, but it will likely take several
  109. years to materialize and several more years to become stable. Even when this
  110. is addressed, rewriting drivers wholesale will be labor intensive and is
  111. likely to introduce more problems than solutions — rewrites always
  112. introduce bugs.
  113. We are thus left with memory safety as the main benefit offered by Rust to
  114. Linux, and for the purpose of this article we're going to take it at face value.
  115. So, with the ground rules set and the advantages enumerated, what are some of
  116. the problems that Rust might face in Linux?
  117. There are a few problems which could be argued over, such as substantial
  118. complexity of Rust compared to C, the inevitable doubling of Linux's build time,
  119. the significant shift in design sensibilities required to support an idiomatic
  120. Rust design, the fragile interface which will develop on the boundaries between
  121. Rust and C code, or the challenges the kernel's established base of C developers
  122. will endure when learning and adapting to a new language. To avoid letting this
  123. post become too subjective or lengthy, I'll refrain from expanding on these.
  124. Instead, allow me to simply illuminate these issues as risk factors.
  125. Linux is, on the whole, a conservative project. It is deployed worldwide in
  126. billions of devices and its reliability is depended on by a majority of Earth's
  127. population. Risks are carefully evaluated in Linux as such. Every change
  128. presents risks and offers advantages, which must be weighed against each other
  129. to justify the change. Rust is one of the riskiest bets Linux has ever
  130. considered, and, in my opinion, the advantages may not weigh up. I think that
  131. the main reason we're going to see Rust in the kernel is not due to a careful
  132. balancing of risk and reward, but because the Rust community wants Rust in
  133. Linux, and they're large and loud enough to not be worth the cost of arguing
  134. with.
  135. I don't think that changes on this scale are appropriate for most projects. I
  136. prefer to encourage people to write new software to replace established
  137. software, rather than rewriting the established software. Some projects, such as
  138. [Redox][redox], are doing just that with Rust. However, operating systems are in
  139. a difficult spot in this respect. Writing an operating system is difficult work
  140. with a huge scope — few projects can hope to challenge Linux on driver
  141. support, for example. The major players have been entrenched for decades, and
  142. any project seeking to displace them will have decades of hard work ahead of
  143. them and will require a considerable amount of luck to succeed. Though I think
  144. that new innovations in kernels are badly overdue, I must acknowledge that
  145. there is some truth to the argument that we're stuck with Linux. In this
  146. framing, if you want Rust to succeed in a kernel, getting it into Linux is the
  147. best strategy.
  148. But, on the whole, my opinion is that the benefits of Rust in Linux are
  149. negligible and the costs are not. That said, it's going to happen, and the
  150. impact to me is likely to be, at worst, a nuisance. Though I would have chosen
  151. differently, I wish them the best of luck and hope to see them succeed.