Miguel Ojeda, the author of the Rust-for-Linux project, has proposed a third version of components for developing device drivers in Rust for consideration by the Linux kernel developers. Rust support is considered experimental but has already been approved for inclusion in the linux-next branch. The development is funded by Google and the Internet Security Research Group (ISRG), which is the founder of the Let’s Encrypt project and promotes HTTPS and the development of technologies to enhance internet security.
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.
In the new version of the patches, the removal of comments made during the discussions of the first and second versions of the patches continues. The most notable changes include:
- The transition to using the stable release of Rust 1.57 as the reference compiler has been made, and it is now linked to the stabilized edition of Rust 2021. Previously, the patches were tied to the beta branch of Rust and utilized certain features of the language that fell into the unstable category. Switching to the Rust 2021 specification has initiated efforts to eliminate the use of unstable features in the patches, such as const_fn_transmute, const_panic, const_unreachable_unchecked, core_panic, and try_reserve.
- The development of the alloc variant of the Rust library included in the patches has continued, modified to eliminate memory allocation functions from potentially generating a 'panic' state when errors occur, such as memory shortages. The new version implements options 'no_rc' and 'no_sync' to disable functionality not utilized in the kernel's Rust code, making the library more modular. Work continues with the core developers of alloc aimed at integrating necessary changes for the kernel into the main library. The core Rust library has incorporated the 'no_fp_fmt_parse' option, essential for the library's operation at the kernel level.
- Code cleanup has been conducted to eliminate potential compiler warnings when building the kernel in CONFIG_WERROR mode. Additional diagnostic modes and Clippy linter warnings have been included when compiling the Rust code.
- Abstractions have been proposed for use in Rust code for seqlock (sequence locks), callback calls for power management, I/O Memory (readX/writeX), interrupt handlers and threads, GPIO, device access, drivers, and credentials.
- The tools for driver development have been expanded with movable mutexes, bit iterators, simplified wrappers over pointers, improved failure diagnostics, and infrastructure independent of the data bus.
- The handling of references has been improved with a simplified Ref type based on the refcount_t backend, which uses the corresponding kernel API for reference counting. Support for the Arc and Rc types provided in the standard library alloc has been removed and is not available in kernel-level code (options have been prepared to disable these types for the library itself).
- A variant of the PL061 GPIO driver has been added to the patches, rewritten in Rust. The feature of the driver is that its implementation closely follows the existing GPIO driver in C, line by line. For developers who want to learn how to create drivers in Rust, a line-by-line comparison has been prepared to understand how C code is transformed into Rust constructs.
- The main Rust codebase has accepted rustc_codegen_gcc, which is the rustc backend for GCC that implements the ability for Ahead-Of-Time (AOT) compilation using the libgccjit library. With proper development of the backend, it will allow compiling the Rust code used in the kernel using GCC.
- In addition to companies like ARM, Google, and Microsoft, Red Hat has also expressed interest in using the Rust language in the Linux kernel. It should be noted that Google directly supports the Rust for Linux project, develops a new implementation of the Binder inter-process communication mechanism in Rust, and is considering rewriting various drivers in Rust. Microsoft has started implementing Hyper-V drivers in Rust. ARM is working on enhancing Rust support for systems based on ARM processors. IBM has implemented Rust support in the kernel for PowerPC systems.
Source: opennet.ru
