Miguel Ojeda, the author of the Rust-for-Linux project, proposed an updated version of the components for developing device drivers in Rust for consideration by Linux kernel developers. Rust support is viewed as experimental but has already been agreed upon for inclusion in the linux-next branch. The new version addresses comments made during the discussion of the first patch version. Linus Torvalds has already joined the discussion and suggested changing the logic for processing certain bitwise operations.
As a reminder, the proposed changes allow Rust to be used as a secondary language for developing drivers and kernel modules. Rust support is presented as an option, not enabled by default, and does not lead to Rust being included among the mandatory build dependencies for the kernel. Using Rust for driver development will enable creating secure and higher-quality drivers with minimal effort, free from issues such as dereferencing freed memory, null pointer dereferencing, and buffer overflows.
Safe memory handling in Rust is ensured at compile time through reference checking, ownership tracking of objects, and considerations of object lifetimes (scope), as well as runtime checks for correctness in memory access. Rust also provides protections against integer overflows, requires variables to be initialized before use, manages errors more effectively in the standard library, employs immutability by default for references and variables, and offers strong static typing to minimize logical errors.
The most notable changes in the new patch version include:
- Code for memory allocation is now free from possible panic states when errors occur, such as memory shortages. Included is a version of the Rust library alloc, which has been reworked to handle failures, but the ultimate goal is to transfer all the capabilities needed for the kernel into the main edit of alloc (the changes have already been prepared and submitted to the standard library of Rust).
- Instead of nightly builds for compiling the kernel with Rust support, beta releases and stable releases of the rustc compiler can now be used. Currently, rustc 1.54-beta1 is used as the reference compiler, but after the 1.54 release at the end of the month, this version will be maintained as the benchmark.
- Support has been added for writing tests using the standard Rust attribute #[test] and the ability to apply doctests for documenting tests.
- Support for ARM32 and RISCV architectures has been added in addition to the previously supported x86_64 and ARM64.
- Improvements have been made to the GCC Rust implementations (the GCC frontend for Rust) and rustc_codegen_gcc (the backend of rustc for GCC), which now passes all basic tests.
- A new level of abstraction has been proposed for using kernel mechanisms written in C within programs in the Rust language, such as red-black trees, reference-counted objects, file descriptor creation, tasks, files, and input/output vectors.
- In driver development components, support for the 'file_operations' module, the 'module!' macro, macro registration, and rudimentary drivers (probe and remove) has been enhanced.
- Support for passing file descriptors and LSM hooks has been implemented in Binder.
- A more functional example of a Rust driver has been proposed — bcm2835-rng for the hardware random number generator on Raspberry Pi boards.
Additionally, projects from several companies related to the use of Rust in the kernel are mentioned:
- Microsoft has expressed interest in participating in the integration of Rust support into the Linux kernel and is ready to provide implementations of drivers for Hyper-V in Rust in the coming months.
- ARM is working on improving Rust support for systems based on ARM processors. Changes have already been proposed to the Rust project that elevate 64-bit ARM systems to tier-1 support platforms.
- Google is directly supporting the Rust for Linux project, developing a new implementation of the Binder inter-process communication mechanism in Rust, and considering the reworking of various drivers in Rust. Through ISRG (Internet Security Research Group), Google has provided funding for the integration of Rust support into the Linux kernel.
- IBM has implemented Rust support in the kernel for PowerPC systems.
- The LSE (Systems Research Laboratory) has developed an SPI driver in Rust.
Source: opennet.ru
