projects/linux·commit 8aebac82

Merge tag 'rust-v6.1-rc1' of https://github.com/Rust-for-Linux/linux

8aebac82 · Linus Torvalds <[email protected]> · 2022-10-03 16:39:37 -07:00

Pull Rust introductory support from Kees Cook:
 "The tree has a recent base, but has fundamentally been in linux-next
  for a year and a half[1]. It's been updated based on feedback from the
  Kernel Maintainer's Summit, and to gain recent Reviewed-by: tags.

  Miguel is the primary maintainer, with me helping where needed/wanted.
  Our plan is for the tree to switch to the standard non-rebasing
  practice once this initial infrastructure series lands.

  The contents are the absolute minimum to get Rust code building in the
  kernel, with many more interfaces[2] (and drivers - NVMe[3], 9p[4], M1
  GPU[5]) on the way.

  The initial support of Rust-for-Linux comes in roughly 4 areas:

   - Kernel internals (kallsyms expansion for Rust symbols, %pA format)

   - Kbuild infrastructure (Rust build rules and support scripts)

   - Rust crates and bindings for initial minimum viable build

   - Rust kernel documentation and samples

  Rust support has been in linux-next for a year and a half now, and the
  short log doesn't do justice to the number of people who have
  contributed both to the Linux kernel side but also to the upstream
  Rust side to support the kernel's needs. Thanks to these 173 people,
  and many more, who have been involved in all kinds of ways:

  Miguel Ojeda, Wedson Almeida Filho, Alex Gaynor, Boqun Feng, Gary Guo,
  Björn Roy Baron, Andreas Hindborg, Adam Bratschi-Kaye, Benno Lossin,
  Maciej Falkowski, Finn Behrens, Sven Van Asbroeck, Asahi Lina, FUJITA
  Tomonori, John Baublitz, Wei Liu, Geoffrey Thomas, Philip Herron,
  Arthur Cohen, David Faust, Antoni Boucher, Philip Li, Yujie Liu,
  Jonathan Corbet, Greg Kroah-Hartman, Paul E. McKenney, Josh Triplett,
  Kent Overstreet, David Gow, Alice Ryhl, Robin Randhawa, Kees Cook,
  Nick Desaulniers, Matthew Wilcox, Linus Walleij, Joe Perches, Michael
  Ellerman, Petr Mladek, Masahiro Yamada, Arnaldo Carvalho de Melo,
  Andrii Nakryiko, Konstantin Shelekhin, Rasmus Villemoes, Konstantin
  Ryabitsev, Stephen Rothwell, Andy Shevchenko, Sergey Senozhatsky, John
  Paul Adrian Glaubitz, David Laight, Nathan Chancellor, Jonathan
  Cameron, Daniel Latypov, Shuah Khan, Brendan Higgins, Julia Lawall,
  Laurent Pinchart, Geert Uytterhoeven, Akira Yokosawa, Pavel Machek,
  David S. Miller, John Hawley, James Bottomley, Arnd Bergmann,
  Christian Brauner, Dan Robertson, Nicholas Piggin, Zhouyi Zhou, Elena
  Zannoni, Jose E. Marchesi, Leon Romanovsky, Will Deacon, Richard
  Weinberger, Randy Dunlap, Paolo Bonzini, Roland Dreier, Mark Brown,
  Sasha Levin, Ted Ts'o, Steven Rostedt, Jarkko Sakkinen, Michal
  Kubecek, Marco Elver, Al Viro, Keith Busch, Johannes Berg, Jan Kara,
  David Sterba, Connor Kuehl, Andy Lutomirski, Andrew Lunn, Alexandre
  Belloni, Peter Zijlstra, Russell King, Eric W. Biederman, Willy
  Tarreau, Christoph Hellwig, Emilio Cobos Álvarez, Christian Poveda,
  Mark Rousskov, John Ericson, TennyZhuang, Xuanwo, Daniel Paoliello,
  Manish Goregaokar, comex, Josh Stone, Stephan Sokolow, Philipp Krones,
  Guillaume Gomez, Joshua Nelson, Mats Larsen, Marc Poulhiès, Samantha
  Miller, Esteban Blanc, Martin Schmidt, Martin Rodriguez Reboredo,
  Daniel Xu, Viresh Kumar, Bartosz Golaszewski, Vegard Nossum, Milan
  Landaverde, Dariusz Sosnowski, Yuki Okushi, Matthew Bakhtiari, Wu
  XiangCheng, Tiago Lam, Boris-Chengbiao Zhou, Sumera Priyadarsini,
  Viktor Garske, Niklas Mohrin, Nándor István Krácser, Morgan Bartlett,
  Miguel Cano, Léo Lanteri Thauvin, Julian Merkle, Andreas Reindl,
  Jiapeng Chong, Fox Chen, Douglas Su, Antonio Terceiro, SeongJae Park,
  Sergio González Collado, Ngo Iok Ui (Wu Yu Wei), Joshua Abraham,
  Milan, Daniel Kolsoi, ahomescu, Manas, Luis Gerhorst, Li Hongyu,
  Philipp Gesang, Russell Currey, Jalil David Salamé Messina, Jon Olson,
  Raghvender, Angelos, Kaviraj Kanagaraj, Paul Römer, Sladyn Nunes,
  Mauro Baladés, Hsiang-Cheng Yang, Abhik Jain, Hongyu Li, Sean Nash,
  Yuheng Su, Peng Hao, Anhad Singh, Roel Kluin, Sara Saa, Geert
  Stappers, Garrett LeSage, IFo Hancroft, and Linus Torvalds"

Link: https://lwn.net/Articles/849849/ [1]
Link: https://github.com/Rust-for-Linux/linux/commits/rust [2]
Link: https://github.com/metaspace/rust-linux/commit/d88c3744d6cbdf11767e08bad56cbfb67c4c96d0 [3]
Link: https://github.com/wedsonaf/linux/commit/9367032607f7670de0ba1537cf09ab0f4365a338 [4]
Link: https://github.com/AsahiLinux/linux/commits/gpu/rust-wip [5]

* tag 'rust-v6.1-rc1' of https://github.com/Rust-for-Linux/linux: (27 commits)
  MAINTAINERS: Rust
  samples: add first Rust examples
  x86: enable initial Rust support
  docs: add Rust documentation
  Kbuild: add Rust support
  rust: add `.rustfmt.toml`
  scripts: add `is_rust_module.sh`
  scripts: add `rust_is_available.sh`
  scripts: add `generate_rust_target.rs`
  scripts: add `generate_rust_analyzer.py`
  scripts: decode_stacktrace: demangle Rust symbols
  scripts: checkpatch: enable language-independent checks for Rust
  scripts: checkpatch: diagnose uses of `%pA` in the C side as errors
  vsprintf: add new `%pA` format specifier
  rust: export generated symbols
  rust: add `kernel` crate
  rust: add `bindings` crate
  rust: add `macros` crate
  rust: add `compiler_builtins` crate
  rust: adapt `alloc` crate to the kernel
  ...
Commit
8aebac82933ff1a7c8eede18cab11e1115e2062b
Parents
a5088ee7 · 615131b8

Merge commit. Changes below are compared with the first parent: a5088ee7.

Changes

89 files changed · +12552 −51

FileStatusAddDelContent
.gitignoremodified+6−0View file
.rustfmt.tomladded+12−0View file
Documentation/core-api/printk-formats.rstmodified+10−0View file
Documentation/doc-guide/kernel-doc.rstmodified+3−0View file
Documentation/index.rstmodified+1−0View file
Documentation/kbuild/kbuild.rstmodified+17−0View file
Documentation/kbuild/makefiles.rstmodified+46−4View file
Documentation/process/changes.rstmodified+41−0View file
Documentation/rust/arch-support.rstadded+19−0View file
Documentation/rust/coding-guidelines.rstadded+216−0View file
Documentation/rust/general-information.rstadded+79−0View file
Documentation/rust/index.rstadded+22−0View file
Documentation/rust/quick-start.rstadded+232−0View file
MAINTAINERSmodified+18−0View file
Makefilemodified+163−9View file
arch/Kconfigmodified+6−0View file
arch/x86/Kconfigmodified+1−0View file
arch/x86/Makefilemodified+10−0View file
include/linux/compiler_types.hmodified+5−1View file
include/linux/kallsyms.hmodified+1−1View file
init/Kconfigmodified+45−1View file
kernel/configs/rust.configadded+1−0View file
kernel/kallsyms.cmodified+22−4View file
kernel/livepatch/core.cmodified+2−2View file
lib/Kconfig.debugmodified+34−0View file
lib/vsprintf.cmodified+13−0View file
rust/.gitignoreadded+8−0View file
rust/Makefileadded+381−0View file
rust/alloc/README.mdadded+33−0View file
rust/alloc/alloc.rsadded+440−0View file
rust/alloc/borrow.rsadded+498−0View file
rust/alloc/boxed.rsadded+2028−0View file
rust/alloc/collections/mod.rsadded+156−0View file
rust/alloc/lib.rsadded+244−0View file
rust/alloc/raw_vec.rsadded+527−0View file
rust/alloc/slice.rsadded+1204−0View file
rust/alloc/vec/drain.rsadded+186−0View file
rust/alloc/vec/drain_filter.rsadded+145−0View file
rust/alloc/vec/into_iter.rsadded+366−0View file
rust/alloc/vec/is_zero.rsadded+120−0View file
rust/alloc/vec/mod.rsadded+3140−0View file
rust/alloc/vec/partial_eq.rsadded+49−0View file
rust/bindgen_parametersadded+21−0View file
rust/bindings/bindings_helper.hadded+13−0View file
rust/bindings/lib.rsadded+53−0View file
rust/compiler_builtins.rsadded+63−0View file
rust/exports.cadded+21−0View file
rust/helpers.cadded+51−0View file
rust/kernel/allocator.rsadded+64−0View file
rust/kernel/error.rsadded+59−0View file
rust/kernel/lib.rsadded+78−0View file
rust/kernel/prelude.rsadded+20−0View file
rust/kernel/print.rsadded+198−0View file
rust/kernel/str.rsadded+72−0View file
rust/macros/helpers.rsadded+51−0View file
rust/macros/lib.rsadded+72−0View file
rust/macros/module.rsadded+282−0View file
samples/Kconfigmodified+2−0View file
samples/Makefilemodified+1−0View file
samples/rust/Kconfigadded+30−0View file
samples/rust/Makefileadded+5−0View file
samples/rust/hostprogs/.gitignoreadded+3−0View file
samples/rust/hostprogs/Makefileadded+5−0View file
samples/rust/hostprogs/a.rsadded+7−0View file
samples/rust/hostprogs/b.rsadded+5−0View file
samples/rust/hostprogs/single.rsadded+12−0View file
samples/rust/rust_minimal.rsadded+38−0View file
scripts/.gitignoremodified+1−0View file
scripts/Kconfig.includemodified+3−3View file
scripts/Makefilemodified+3−0View file
scripts/Makefile.buildmodified+60−0View file
scripts/Makefile.debugmodified+8−0View file
scripts/Makefile.hostmodified+31−3View file
scripts/Makefile.libmodified+12−0View file
scripts/Makefile.modfinalmodified+5−3View file
scripts/cc-version.shmodified+6−6View file
scripts/checkpatch.plmodified+8−4View file
scripts/decode_stacktrace.shmodified+14−0View file
scripts/generate_rust_analyzer.pyadded+135−0View file
scripts/generate_rust_target.rsadded+182−0View file
scripts/is_rust_module.shadded+16−0View file
scripts/kallsyms.cmodified+46−7View file
scripts/kconfig/confdata.cmodified+75−0View file
scripts/min-tool-version.shmodified+6−0View file
scripts/rust_is_available.shadded+160−0View file
scripts/rust_is_available_bindgen_libclang.hadded+2−0View file
tools/include/linux/kallsyms.hmodified+1−1View file
tools/lib/perf/include/perf/event.hmodified+1−1View file
tools/lib/symbol/kallsyms.hmodified+1−1View file

Only the first 600 diff lines are shown. Download the complete patch.

.gitignore

View file
diff --git a/.gitignore b/.gitignore
index 2659595..5da0048 100644
--- a/.gitignore
+++ b/.gitignore
@@ -37,6 +37,8 @@
*.o
*.o.*
*.patch
+*.rmeta
+*.rsi
*.s
*.so
*.so.dbg
@@ -97,6 +99,7 @@ modules.order
!.gitattributes
!.gitignore
!.mailmap
+!.rustfmt.toml
#
# Generated include files
@@ -162,3 +165,6 @@ x509.genkey
# Documentation toolchain
sphinx_*/
+
+# Rust analyzer configuration
+/rust-project.json

.rustfmt.toml

View file
diff --git a/.rustfmt.toml b/.rustfmt.toml
new file mode 100644
index 0000000..3de5cc4
--- /dev/null
+++ b/.rustfmt.toml
@@ -0,0 +1,12 @@
+edition = "2021"
+newline_style = "Unix"
+
+# Unstable options that help catching some mistakes in formatting and that we may want to enable
+# when they become stable.
+#
+# They are kept here since they are useful to run from time to time.
+#format_code_in_doc_comments = true
+#reorder_impl_items = true
+#comment_width = 100
+#wrap_comments = true
+#normalize_comments = true

Documentation/core-api/printk-formats.rst

View file
diff --git a/Documentation/core-api/printk-formats.rst b/Documentation/core-api/printk-formats.rst
index 5e89497..dbe1aac 100644
--- a/Documentation/core-api/printk-formats.rst
+++ b/Documentation/core-api/printk-formats.rst
@@ -625,6 +625,16 @@ Examples::
%p4cc Y10 little-endian (0x20303159)
%p4cc NV12 big-endian (0xb231564e)
+Rust
+----
+
+::
+
+ %pA
+
+Only intended to be used from Rust code to format ``core::fmt::Arguments``.
+Do *not* use it from C.
+
Thanks
======

Documentation/doc-guide/kernel-doc.rst

View file
diff --git a/Documentation/doc-guide/kernel-doc.rst b/Documentation/doc-guide/kernel-doc.rst
index 9c779bd..1dcbd73 100644
--- a/Documentation/doc-guide/kernel-doc.rst
+++ b/Documentation/doc-guide/kernel-doc.rst
@@ -14,6 +14,9 @@ when it is embedded in source files.
reasons. The kernel source contains tens of thousands of kernel-doc
comments. Please stick to the style described here.
+.. note:: kernel-doc does not cover Rust code: please see
+ Documentation/rust/general-information.rst instead.
+
The kernel-doc structure is extracted from the comments, and proper
`Sphinx C Domain`_ function and type descriptions with anchors are
generated from them. The descriptions are filtered for special kernel-doc

Documentation/index.rst

View file
diff --git a/Documentation/index.rst b/Documentation/index.rst
index 85eab6e..bf6aa68 100644
--- a/Documentation/index.rst
+++ b/Documentation/index.rst
@@ -58,6 +58,7 @@ Various other manuals with useful information for all kernel developers.
trace/index
fault-injection/index
livepatch/index
+ rust/index
User-oriented documentation

Documentation/kbuild/kbuild.rst

View file
diff --git a/Documentation/kbuild/kbuild.rst b/Documentation/kbuild/kbuild.rst
index ef19b9c..08f575e 100644
--- a/Documentation/kbuild/kbuild.rst
+++ b/Documentation/kbuild/kbuild.rst
@@ -48,6 +48,10 @@ KCFLAGS
-------
Additional options to the C compiler (for built-in and modules).
+KRUSTFLAGS
+----------
+Additional options to the Rust compiler (for built-in and modules).
+
CFLAGS_KERNEL
-------------
Additional options for $(CC) when used to compile
@@ -57,6 +61,15 @@ CFLAGS_MODULE
-------------
Additional module specific options to use for $(CC).
+RUSTFLAGS_KERNEL
+----------------
+Additional options for $(RUSTC) when used to compile
+code that is compiled as built-in.
+
+RUSTFLAGS_MODULE
+----------------
+Additional module specific options to use for $(RUSTC).
+
LDFLAGS_MODULE
--------------
Additional options used for $(LD) when linking modules.
@@ -69,6 +82,10 @@ HOSTCXXFLAGS
------------
Additional flags to be passed to $(HOSTCXX) when building host programs.
+HOSTRUSTFLAGS
+-------------
+Additional flags to be passed to $(HOSTRUSTC) when building host programs.
+
HOSTLDFLAGS
-----------
Additional flags to be passed when linking host programs.

Documentation/kbuild/makefiles.rst

View file
diff --git a/Documentation/kbuild/makefiles.rst b/Documentation/kbuild/makefiles.rst
index 11a296e..5ea1e72 100644
--- a/Documentation/kbuild/makefiles.rst
+++ b/Documentation/kbuild/makefiles.rst
@@ -29,8 +29,9 @@ This document describes the Linux kernel Makefiles.
--- 4.1 Simple Host Program
--- 4.2 Composite Host Programs
--- 4.3 Using C++ for host programs
- --- 4.4 Controlling compiler options for host programs
- --- 4.5 When host programs are actually built
+ --- 4.4 Using Rust for host programs
+ --- 4.5 Controlling compiler options for host programs
+ --- 4.6 When host programs are actually built
=== 5 Userspace Program support
--- 5.1 Simple Userspace Program
@@ -835,7 +836,24 @@ Both possibilities are described in the following.
qconf-cxxobjs := qconf.o
qconf-objs := check.o
-4.4 Controlling compiler options for host programs
+4.4 Using Rust for host programs
+--------------------------------
+
+ Kbuild offers support for host programs written in Rust. However,
+ since a Rust toolchain is not mandatory for kernel compilation,
+ it may only be used in scenarios where Rust is required to be
+ available (e.g. when ``CONFIG_RUST`` is enabled).
+
+ Example::
+
+ hostprogs := target
+ target-rust := y
+
+ Kbuild will compile ``target`` using ``target.rs`` as the crate root,
+ located in the same directory as the ``Makefile``. The crate may
+ consist of several source files (see ``samples/rust/hostprogs``).
+
+4.5 Controlling compiler options for host programs
--------------------------------------------------
When compiling host programs, it is possible to set specific flags.
@@ -867,7 +885,7 @@ Both possibilities are described in the following.
When linking qconf, it will be passed the extra option
"-L$(QTDIR)/lib".
-4.5 When host programs are actually built
+4.6 When host programs are actually built
-----------------------------------------
Kbuild will only build host-programs when they are referenced
@@ -1181,6 +1199,17 @@ When kbuild executes, the following steps are followed (roughly):
The first example utilises the trick that a config option expands
to 'y' when selected.
+ KBUILD_RUSTFLAGS
+ $(RUSTC) compiler flags
+
+ Default value - see top level Makefile
+ Append or modify as required per architecture.
+
+ Often, the KBUILD_RUSTFLAGS variable depends on the configuration.
+
+ Note that target specification file generation (for ``--target``)
+ is handled in ``scripts/generate_rust_target.rs``.
+
KBUILD_AFLAGS_KERNEL
Assembler options specific for built-in
@@ -1208,6 +1237,19 @@ When kbuild executes, the following steps are followed (roughly):
are used for $(CC).
From commandline CFLAGS_MODULE shall be used (see kbuild.rst).
+ KBUILD_RUSTFLAGS_KERNEL
+ $(RUSTC) options specific for built-in
+
+ $(KBUILD_RUSTFLAGS_KERNEL) contains extra Rust compiler flags used to
+ compile resident kernel code.
+
+ KBUILD_RUSTFLAGS_MODULE
+ Options for $(RUSTC) when building modules
+
+ $(KBUILD_RUSTFLAGS_MODULE) is used to add arch-specific options that
+ are used for $(RUSTC).
+ From commandline RUSTFLAGS_MODULE shall be used (see kbuild.rst).
+
KBUILD_LDFLAGS_MODULE
Options for $(LD) when linking modules

Documentation/process/changes.rst

View file
diff --git a/Documentation/process/changes.rst b/Documentation/process/changes.rst
index 19c286c..9a90197 100644
--- a/Documentation/process/changes.rst
+++ b/Documentation/process/changes.rst
@@ -31,6 +31,8 @@ you probably needn't concern yourself with pcmciautils.
====================== =============== ========================================
GNU C 5.1 gcc --version
Clang/LLVM (optional) 11.0.0 clang --version
+Rust (optional) 1.62.0 rustc --version
+bindgen (optional) 0.56.0 bindgen --version
GNU make 3.81 make --version
bash 4.2 bash --version
binutils 2.23 ld -v
@@ -80,6 +82,29 @@ kernels. Older releases aren't guaranteed to work, and we may drop workarounds
from the kernel that were used to support older versions. Please see additional
docs on :ref:`Building Linux with Clang/LLVM <kbuild_llvm>`.
+Rust (optional)
+---------------
+
+A particular version of the Rust toolchain is required. Newer versions may or
+may not work because the kernel depends on some unstable Rust features, for
+the moment.
+
+Each Rust toolchain comes with several "components", some of which are required
+(like ``rustc``) and some that are optional. The ``rust-src`` component (which
+is optional) needs to be installed to build the kernel. Other components are
+useful for developing.
+
+Please see Documentation/rust/quick-start.rst for instructions on how to
+satisfy the build requirements of Rust support. In particular, the ``Makefile``
+target ``rustavailable`` is useful to check why the Rust toolchain may not
+be detected.
+
+bindgen (optional)
+------------------
+
+``bindgen`` is used to generate the Rust bindings to the C side of the kernel.
+It depends on ``libclang``.
+
Make
----
@@ -348,6 +373,12 @@ Sphinx
Please see :ref:`sphinx_install` in :ref:`Documentation/doc-guide/sphinx.rst <sphinxdoc>`
for details about Sphinx requirements.
+rustdoc
+-------
+
+``rustdoc`` is used to generate the documentation for Rust code. Please see
+Documentation/rust/general-information.rst for more information.
+
Getting updated software
========================
@@ -364,6 +395,16 @@ Clang/LLVM
- :ref:`Getting LLVM <getting_llvm>`.
+Rust
+----
+
+- Documentation/rust/quick-start.rst.
+
+bindgen
+-------
+
+- Documentation/rust/quick-start.rst.
+
Make
----

Documentation/rust/arch-support.rst

View file
diff --git a/Documentation/rust/arch-support.rst b/Documentation/rust/arch-support.rst
new file mode 100644
index 0000000..6982b63
--- /dev/null
+++ b/Documentation/rust/arch-support.rst
@@ -0,0 +1,19 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+Arch Support
+============
+
+Currently, the Rust compiler (``rustc``) uses LLVM for code generation,
+which limits the supported architectures that can be targeted. In addition,
+support for building the kernel with LLVM/Clang varies (please see
+Documentation/kbuild/llvm.rst). This support is needed for ``bindgen``
+which uses ``libclang``.
+
+Below is a general summary of architectures that currently work. Level of
+support corresponds to ``S`` values in the ``MAINTAINERS`` file.
+
+============ ================ ==============================================
+Architecture Level of support Constraints
+============ ================ ==============================================
+``x86`` Maintained ``x86_64`` only.
+============ ================ ==============================================

Documentation/rust/coding-guidelines.rst

View file
diff --git a/Documentation/rust/coding-guidelines.rst b/Documentation/rust/coding-guidelines.rst
new file mode 100644
index 0000000..aa8ed08
--- /dev/null
+++ b/Documentation/rust/coding-guidelines.rst
@@ -0,0 +1,216 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+Coding Guidelines
+=================
+
+This document describes how to write Rust code in the kernel.
+
+
+Style & formatting
+------------------
+
+The code should be formatted using ``rustfmt``. In this way, a person
+contributing from time to time to the kernel does not need to learn and
+remember one more style guide. More importantly, reviewers and maintainers
+do not need to spend time pointing out style issues anymore, and thus
+less patch roundtrips may be needed to land a change.
+
+.. note:: Conventions on comments and documentation are not checked by
+ ``rustfmt``. Thus those are still needed to be taken care of.
+
+The default settings of ``rustfmt`` are used. This means the idiomatic Rust
+style is followed. For instance, 4 spaces are used for indentation rather
+than tabs.
+
+It is convenient to instruct editors/IDEs to format while typing,
+when saving or at commit time. However, if for some reason reformatting
+the entire kernel Rust sources is needed at some point, the following can be
+run::
+
+ make LLVM=1 rustfmt
+
+It is also possible to check if everything is formatted (printing a diff
+otherwise), for instance for a CI, with::
+
+ make LLVM=1 rustfmtcheck
+
+Like ``clang-format`` for the rest of the kernel, ``rustfmt`` works on
+individual files, and does not require a kernel configuration. Sometimes it may
+even work with broken code.
+
+
+Comments
+--------
+
+"Normal" comments (i.e. ``//``, rather than code documentation which starts
+with ``///`` or ``//!``) are written in Markdown the same way as documentation
+comments are, even though they will not be rendered. This improves consistency,
+simplifies the rules and allows to move content between the two kinds of
+comments more easily. For instance:
+
+.. code-block:: rust
+
+ // `object` is ready to be handled now.
+ f(object);
+
+Furthermore, just like documentation, comments are capitalized at the beginning
+of a sentence and ended with a period (even if it is a single sentence). This
+includes ``// SAFETY:``, ``// TODO:`` and other "tagged" comments, e.g.:
+
+.. code-block:: rust
+
+ // FIXME: The error should be handled properly.
+
+Comments should not be used for documentation purposes: comments are intended
+for implementation details, not users. This distinction is useful even if the
+reader of the source file is both an implementor and a user of an API. In fact,
+sometimes it is useful to use both comments and documentation at the same time.
+For instance, for a ``TODO`` list or to comment on the documentation itself.
+For the latter case, comments can be inserted in the middle; that is, closer to
+the line of documentation to be commented. For any other case, comments are
+written after the documentation, e.g.:
+
+.. code-block:: rust
+
+ /// Returns a new [`Foo`].
+ ///
+ /// # Examples
+ ///
+ // TODO: Find a better example.
+ /// ```
+ /// let foo = f(42);
+ /// ```
+ // FIXME: Use fallible approach.
+ pub fn f(x: i32) -> Foo {
+ // ...
+ }
+
+One special kind of comments are the ``// SAFETY:`` comments. These must appear
+before every ``unsafe`` block, and they explain why the code inside the block is
+correct/sound, i.e. why it cannot trigger undefined behavior in any case, e.g.:
+
+.. code-block:: rust
+
+ // SAFETY: `p` is valid by the safety requirements.
+ unsafe { *p = 0; }
+
+``// SAFETY:`` comments are not to be confused with the ``# Safety`` sections
+in code documentation. ``# Safety`` sections specify the contract that callers
+(for functions) or implementors (for traits) need to abide by. ``// SAFETY:``
+comments show why a call (for functions) or implementation (for traits) actually
+respects the preconditions stated in a ``# Safety`` section or the language
+reference.
+
+
+Code documentation
+------------------
+
+Rust kernel code is not documented like C kernel code (i.e. via kernel-doc).
+Instead, the usual system for documenting Rust code is used: the ``rustdoc``
+tool, which uses Markdown (a lightweight markup language).
+
+To learn Markdown, there are many guides available out there. For instance,
+the one at:
+
+ https://commonmark.org/help/
+
+This is how a well-documented Rust function may look like:
+
+.. code-block:: rust
+
+ /// Returns the contained [`Some`] value, consuming the `self` value,
+ /// without checking that the value is not [`None`].
+ ///
+ /// # Safety
+ ///
+ /// Calling this method on [`None`] is *[undefined behavior]*.
+ ///
+ /// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let x = Some("air");
+ /// assert_eq!(unsafe { x.unwrap_unchecked() }, "air");
+ /// ```
+ pub unsafe fn unwrap_unchecked(self) -> T {
+ match self {
+ Some(val) => val,
+
+ // SAFETY: The safety contract must be upheld by the caller.
+ None => unsafe { hint::unreachable_unchecked() },
+ }
+ }
+
+This example showcases a few ``rustdoc`` features and some conventions followed
+in the kernel:
+
+ - The first paragraph must be a single sentence briefly describing what
+ the documented item does. Further explanations must go in extra paragraphs.
+
+ - Unsafe functions must document their safety preconditions under
+ a ``# Safety`` section.
+
+ - While not shown here, if a function may panic, the conditions under which
+ that happens must be described under a ``# Panics`` section.
+
+ Please note that panicking should be very rare and used only with a good
+ reason. In almost all cases, a fallible approach should be used, typically
+ returning a ``Result``.
+
+ - If providing examples of usage would help readers, they must be written in
+ a section called ``# Examples``.
+
+ - Rust items (functions, types, constants...) must be linked appropriately
+ (``rustdoc`` will create a link automatically).
+
+ - Any ``unsafe`` block must be preceded by a ``// SAFETY:`` comment
+ describing why the code inside is sound.
+
+ While sometimes the reason might look trivial and therefore unneeded,
+ writing these comments is not just a good way of documenting what has been
+ taken into account, but most importantly, it provides a way to know that
+ there are no *extra* implicit constraints.
+
+To learn more about how to write documentation for Rust and extra features,
+please take a look at the ``rustdoc`` book at:
+
+ https://doc.rust-lang.org/rustdoc/how-to-write-documentation.html
+
+
+Naming
+------
+
+Rust kernel code follows the usual Rust naming conventions:
+
+ https://rust-lang.github.io/api-guidelines/naming.html
+
+When existing C concepts (e.g. macros, functions, objects...) are wrapped into
+a Rust abstraction, a name as close as reasonably possible to the C side should
+be used in order to avoid confusion and to improve readability when switching
+back and forth between the C and Rust sides. For instance, macros such as
+``pr_info`` from C are named the same in the Rust side.
+
+Having said that, casing should be adjusted to follow the Rust naming
+conventions, and namespacing introduced by modules and types should not be
+repeated in the item names. For instance, when wrapping constants like:
+
+.. code-block:: c
+
+ #define GPIO_LINE_DIRECTION_IN 0
+ #define GPIO_LINE_DIRECTION_OUT 1
+
+The equivalent in Rust may look like (ignoring documentation):
+
+.. code-block:: rust
+
+ pub mod gpio {
+ pub enum LineDirection {
+ In = bindings::GPIO_LINE_DIRECTION_IN as _,
+ Out = bindings::GPIO_LINE_DIRECTION_OUT as _,
+ }
+ }
+
+That is, the equivalent of ``GPIO_LINE_DIRECTION_IN`` would be referred to as
+``gpio::LineDirection::In``. In particular, it should not be named
+``gpio::gpio_line_direction::GPIO_LINE_DIRECTION_IN``.

Documentation/rust/general-information.rst

View file
diff --git a/Documentation/rust/general-information.rst b/Documentation/rust/general-information.rst
new file mode 100644
index 0000000..49029ee
--- /dev/null
+++ b/Documentation/rust/general-information.rst
@@ -0,0 +1,79 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+General Information
+===================
+
+This document contains useful information to know when working with
+the Rust support in the kernel.
+
+
+Code documentation
+------------------
+
+Rust kernel code is documented using ``rustdoc``, its built-in documentation
+generator.
+
+The generated HTML docs include integrated search, linked items (e.g. types,
+functions, constants), source code, etc. They may be read at (TODO: link when
+in mainline and generated alongside the rest of the documentation):
+
+ http://kernel.org/
+
+The docs can also be easily generated and read locally. This is quite fast
+(same order as compiling the code itself) and no special tools or environment
+are needed. This has the added advantage that they will be tailored to
+the particular kernel configuration used. To generate them, use the ``rustdoc``
+target with the same invocation used for compilation, e.g.::
+
+ make LLVM=1 rustdoc
+
+To read the docs locally in your web browser, run e.g.::
+
+ xdg-open rust/doc/kernel/index.html
+
+To learn about how to write the documentation, please see coding-guidelines.rst.
+
+
+Extra lints
+-----------
+
+While ``rustc`` is a very helpful compiler, some extra lints and analyses are
+available via ``clippy``, a Rust linter. To enable it, pass ``CLIPPY=1`` to
+the same invocation used for compilation, e.g.::
+
+ make LLVM=1 CLIPPY=1
+
+Please note that Clippy may change code generation, thus it should not be
+enabled while building a production kernel.
+
+
+Abstractions vs. bindings

This diff is incomplete. Download the complete patch.

Documentation/rust/index.rst

View file

This diff is incomplete. Download the complete patch.

Documentation/rust/quick-start.rst

View file

This diff is incomplete. Download the complete patch.

MAINTAINERS

View file

This diff is incomplete. Download the complete patch.

Makefile

View file

This diff is incomplete. Download the complete patch.

arch/Kconfig

View file

This diff is incomplete. Download the complete patch.

arch/x86/Kconfig

View file

This diff is incomplete. Download the complete patch.

arch/x86/Makefile

View file

This diff is incomplete. Download the complete patch.

include/linux/compiler_types.h

View file

This diff is incomplete. Download the complete patch.

include/linux/kallsyms.h

View file

This diff is incomplete. Download the complete patch.

init/Kconfig

View file

This diff is incomplete. Download the complete patch.

kernel/configs/rust.config

View file

This diff is incomplete. Download the complete patch.

kernel/kallsyms.c

View file

This diff is incomplete. Download the complete patch.

kernel/livepatch/core.c

View file

This diff is incomplete. Download the complete patch.

lib/Kconfig.debug

View file

This diff is incomplete. Download the complete patch.

lib/vsprintf.c

View file

This diff is incomplete. Download the complete patch.

rust/.gitignore

View file

This diff is incomplete. Download the complete patch.

rust/Makefile

View file

This diff is incomplete. Download the complete patch.

rust/alloc/README.md

View file

This diff is incomplete. Download the complete patch.

rust/alloc/alloc.rs

View file

This diff is incomplete. Download the complete patch.

rust/alloc/borrow.rs

View file

This diff is incomplete. Download the complete patch.

rust/alloc/boxed.rs

View file

This diff is incomplete. Download the complete patch.

rust/alloc/collections/mod.rs

View file

This diff is incomplete. Download the complete patch.

rust/alloc/lib.rs

View file

This diff is incomplete. Download the complete patch.

rust/alloc/raw_vec.rs

View file

This diff is incomplete. Download the complete patch.

rust/alloc/slice.rs

View file

This diff is incomplete. Download the complete patch.

rust/alloc/vec/drain.rs

View file

This diff is incomplete. Download the complete patch.

rust/alloc/vec/drain_filter.rs

View file

This diff is incomplete. Download the complete patch.

rust/alloc/vec/into_iter.rs

View file

This diff is incomplete. Download the complete patch.

rust/alloc/vec/is_zero.rs

View file

This diff is incomplete. Download the complete patch.

rust/alloc/vec/mod.rs

View file

This diff is incomplete. Download the complete patch.

rust/alloc/vec/partial_eq.rs

View file

This diff is incomplete. Download the complete patch.

rust/bindgen_parameters

View file

This diff is incomplete. Download the complete patch.

rust/bindings/bindings_helper.h

View file

This diff is incomplete. Download the complete patch.

rust/bindings/lib.rs

View file

This diff is incomplete. Download the complete patch.

rust/compiler_builtins.rs

View file

This diff is incomplete. Download the complete patch.

rust/exports.c

View file

This diff is incomplete. Download the complete patch.

rust/helpers.c

View file

This diff is incomplete. Download the complete patch.

rust/kernel/allocator.rs

View file

This diff is incomplete. Download the complete patch.

rust/kernel/error.rs

View file

This diff is incomplete. Download the complete patch.

rust/kernel/lib.rs

View file

This diff is incomplete. Download the complete patch.

rust/kernel/prelude.rs

View file

This diff is incomplete. Download the complete patch.

rust/kernel/print.rs

View file

This diff is incomplete. Download the complete patch.

rust/kernel/str.rs

View file

This diff is incomplete. Download the complete patch.

rust/macros/helpers.rs

View file

This diff is incomplete. Download the complete patch.

rust/macros/lib.rs

View file

This diff is incomplete. Download the complete patch.

rust/macros/module.rs

View file

This diff is incomplete. Download the complete patch.

samples/Kconfig

View file

This diff is incomplete. Download the complete patch.

samples/Makefile

View file

This diff is incomplete. Download the complete patch.

samples/rust/Kconfig

View file

This diff is incomplete. Download the complete patch.

samples/rust/Makefile

View file

This diff is incomplete. Download the complete patch.

samples/rust/hostprogs/.gitignore

View file

This diff is incomplete. Download the complete patch.

samples/rust/hostprogs/Makefile

View file

This diff is incomplete. Download the complete patch.

samples/rust/hostprogs/a.rs

View file

This diff is incomplete. Download the complete patch.

samples/rust/hostprogs/b.rs

View file

This diff is incomplete. Download the complete patch.

samples/rust/hostprogs/single.rs

View file

This diff is incomplete. Download the complete patch.

samples/rust/rust_minimal.rs

View file

This diff is incomplete. Download the complete patch.

scripts/.gitignore

View file

This diff is incomplete. Download the complete patch.

scripts/Kconfig.include

View file

This diff is incomplete. Download the complete patch.

scripts/Makefile

View file

This diff is incomplete. Download the complete patch.

scripts/Makefile.build

View file

This diff is incomplete. Download the complete patch.

scripts/Makefile.debug

View file

This diff is incomplete. Download the complete patch.

scripts/Makefile.host

View file

This diff is incomplete. Download the complete patch.

scripts/Makefile.lib

View file

This diff is incomplete. Download the complete patch.

scripts/Makefile.modfinal

View file

This diff is incomplete. Download the complete patch.

scripts/cc-version.sh

View file

This diff is incomplete. Download the complete patch.

scripts/checkpatch.pl

View file

This diff is incomplete. Download the complete patch.

scripts/decode_stacktrace.sh

View file

This diff is incomplete. Download the complete patch.

scripts/generate_rust_analyzer.py

View file

This diff is incomplete. Download the complete patch.

scripts/generate_rust_target.rs

View file

This diff is incomplete. Download the complete patch.

scripts/is_rust_module.sh

View file

This diff is incomplete. Download the complete patch.

scripts/kallsyms.c

View file

This diff is incomplete. Download the complete patch.

scripts/kconfig/confdata.c

View file

This diff is incomplete. Download the complete patch.

scripts/min-tool-version.sh

View file

This diff is incomplete. Download the complete patch.

scripts/rust_is_available.sh

View file

This diff is incomplete. Download the complete patch.

scripts/rust_is_available_bindgen_libclang.h

View file

This diff is incomplete. Download the complete patch.

tools/include/linux/kallsyms.h

View file

This diff is incomplete. Download the complete patch.

tools/lib/perf/include/perf/event.h

View file

This diff is incomplete. Download the complete patch.

tools/lib/symbol/kallsyms.h

View file

This diff is incomplete. Download the complete patch.