general-information.rst 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. .. SPDX-License-Identifier: GPL-2.0
  2. General Information
  3. ===================
  4. This document contains useful information to know when working with
  5. the Rust support in the kernel.
  6. ``no_std``
  7. ----------
  8. The Rust support in the kernel can link only `core <https://doc.rust-lang.org/core/>`_,
  9. but not `std <https://doc.rust-lang.org/std/>`_. Crates for use in the
  10. kernel must opt into this behavior using the ``#![no_std]`` attribute.
  11. .. _rust_code_documentation:
  12. Code documentation
  13. ------------------
  14. Rust kernel code is documented using ``rustdoc``, its built-in documentation
  15. generator.
  16. The generated HTML docs include integrated search, linked items (e.g. types,
  17. functions, constants), source code, etc. They may be read at:
  18. https://rust.docs.kernel.org
  19. For linux-next, please see:
  20. https://rust.docs.kernel.org/next/
  21. There are also tags for each main release, e.g.:
  22. https://rust.docs.kernel.org/6.10/
  23. The docs can also be easily generated and read locally. This is quite fast
  24. (same order as compiling the code itself) and no special tools or environment
  25. are needed. This has the added advantage that they will be tailored to
  26. the particular kernel configuration used. To generate them, use the ``rustdoc``
  27. target with the same invocation used for compilation, e.g.::
  28. make LLVM=1 rustdoc
  29. To read the docs locally in your web browser, run e.g.::
  30. xdg-open Documentation/output/rust/rustdoc/kernel/index.html
  31. To learn about how to write the documentation, please see coding-guidelines.rst.
  32. Extra lints
  33. -----------
  34. While ``rustc`` is a very helpful compiler, some extra lints and analyses are
  35. available via ``clippy``, a Rust linter. To enable it, pass ``CLIPPY=1`` to
  36. the same invocation used for compilation, e.g.::
  37. make LLVM=1 CLIPPY=1
  38. Please note that Clippy may change code generation, thus it should not be
  39. enabled while building a production kernel.
  40. Abstractions vs. bindings
  41. -------------------------
  42. Abstractions are Rust code wrapping kernel functionality from the C side.
  43. In order to use functions and types from the C side, bindings are created.
  44. Bindings are the declarations for Rust of those functions and types from
  45. the C side.
  46. For instance, one may write a ``Mutex`` abstraction in Rust which wraps
  47. a ``struct mutex`` from the C side and calls its functions through the bindings.
  48. Abstractions are not available for all the kernel internal APIs and concepts,
  49. but it is intended that coverage is expanded as time goes on. "Leaf" modules
  50. (e.g. drivers) should not use the C bindings directly. Instead, subsystems
  51. should provide as-safe-as-possible abstractions as needed.
  52. .. code-block::
  53. rust/bindings/
  54. (rust/helpers/)
  55. include/ -----+ <-+
  56. | |
  57. drivers/ rust/kernel/ +----------+ <-+ |
  58. fs/ | bindgen | |
  59. .../ +-------------------+ +----------+ --+ |
  60. | Abstractions | | |
  61. +---------+ | +------+ +------+ | +----------+ | |
  62. | my_foo | -----> | | foo | | bar | | -------> | Bindings | <-+ |
  63. | driver | Safe | | sub- | | sub- | | Unsafe | | |
  64. +---------+ | |system| |system| | | bindings | <-----+
  65. | | +------+ +------+ | | crate | |
  66. | | kernel crate | +----------+ |
  67. | +-------------------+ |
  68. | |
  69. +------------------# FORBIDDEN #--------------------------------+
  70. The main idea is to encapsulate all direct interaction with the kernel's C APIs
  71. into carefully reviewed and documented abstractions. Then users of these
  72. abstractions cannot introduce undefined behavior (UB) as long as:
  73. #. The abstractions are correct ("sound").
  74. #. Any ``unsafe`` blocks respect the safety contract necessary to call the
  75. operations inside the block. Similarly, any ``unsafe impl``\ s respect the
  76. safety contract necessary to implement the trait.
  77. Bindings
  78. ~~~~~~~~
  79. By including a C header from ``include/`` into
  80. ``rust/bindings/bindings_helper.h``, the ``bindgen`` tool will auto-generate the
  81. bindings for the included subsystem. After building, see the ``*_generated.rs``
  82. output files in the ``rust/bindings/`` directory.
  83. For parts of the C header that ``bindgen`` does not auto generate, e.g. C
  84. ``inline`` functions or non-trivial macros, it is acceptable to add a small
  85. wrapper function to ``rust/helpers/`` to make it available for the Rust side as
  86. well.
  87. Abstractions
  88. ~~~~~~~~~~~~
  89. Abstractions are the layer between the bindings and the in-kernel users. They
  90. are located in ``rust/kernel/`` and their role is to encapsulate the unsafe
  91. access to the bindings into an as-safe-as-possible API that they expose to their
  92. users. Users of the abstractions include things like drivers or file systems
  93. written in Rust.
  94. Besides the safety aspect, the abstractions are supposed to be "ergonomic", in
  95. the sense that they turn the C interfaces into "idiomatic" Rust code. Basic
  96. examples are to turn the C resource acquisition and release into Rust
  97. constructors and destructors or C integer error codes into Rust's ``Result``\ s.
  98. Conditional compilation
  99. -----------------------
  100. Rust code has access to conditional compilation based on the kernel
  101. configuration:
  102. .. code-block:: rust
  103. #[cfg(CONFIG_X)] // Enabled (`y` or `m`)
  104. #[cfg(CONFIG_X="y")] // Enabled as a built-in (`y`)
  105. #[cfg(CONFIG_X="m")] // Enabled as a module (`m`)
  106. #[cfg(not(CONFIG_X))] // Disabled
  107. For other predicates that Rust's ``cfg`` does not support, e.g. expressions with
  108. numerical comparisons, one may define a new Kconfig symbol:
  109. .. code-block:: kconfig
  110. config RUSTC_VERSION_MIN_107900
  111. def_bool y if RUSTC_VERSION >= 107900