Skip to contentSkip to conversation

docs: rust: extend abstraction and binding documentation

PATCH Applied Dirk Behme 3 participants 3 messages Apr 18, 2024 – May 5, 2024

Subsystems (2)
lore
Share & tools
lore

Dirk Behme

author
#0 lore raw

PATCH v4 1/1 docs: rust: extend abstraction and binding documentation

Add some basics explained by Miguel in [1] to the documentation. And connect it with some hints where this is implemented in the kernel.

[1] https://www.linuxfoundation.org/webinars/rust-for-linux-writing-abstractions-and-drivers

---
 Documentation/rust/general-information.rst | 56 ++++++++++++++++++++++
 1 file changed, 56 insertions(+)

Changes in v4: Rebase against rust-dev v6.9-rc1 to re-start this topic. If
the reviewers are too busy and its not a good time for it just let me know :)

Documentation/rust/general-information.rst +56 -0
index 081397827a7e..6c6fb3a5e318 100644
@@ -64,6 +64,62 @@ but it is intended that coverage is expanded as time goes on. "Leaf" modules
 (e.g. drivers) should not use the C bindings directly. Instead, subsystems
 should provide as-safe-as-possible abstractions as needed.
 
+.. code-block::
+
+	                                                rust/bindings/
+                                                       (rust/helpers.c)
+
+                                                          include/ -----+ <-+
+                                                                        |   |
+         drivers/              rust/kernel/              +----------+ <-+   |
+           fs/                                           | bindgen  |       |
+          .../            +-------------------+          +----------+ --+   |
+                          |    Abstractions   |                         |   |
+       +---------+        | +------+ +------+ |          +----------+   |   |
+       | my_foo  | -----> | | foo  | | bar  | | -------> | Bindings | <-+   |
+       | driver  |  Safe  | | sub- | | sub- | |  Unsafe  |          |       |
+       +---------+        | |system| |system| |          | bindings | <-----+
+            |             | +------+ +------+ |          |  crate   |       |
+            |             |   kernel crate    |          +----------+       |
+            |             +-------------------+                             |
+            |                                                               |
+            +------------------# FORBIDDEN #--------------------------------+
+
+The main idea is to encapsulate all direct interaction with the kernel's C APIs
+into carefully reviewed and documented abstractions. These are then considered to
+be sound. The goal is that users of these abstractions ("my_foo driver") cannot
+introduce undefined behavior (UB) as long as:
+
+#. the abstractions are correct.
+#. they don't use ``unsafe{..}``. Or they uphold the preconditions of all unsafe
+   operations that they perform if they use ``unsafe{..}`` for performance optimizations
+   or to call unsafe abstractions.
+
+Bindings
+~~~~~~~~
+
+By including a C header from ``include/`` into ``rust/bindings/bindings_helper.h``
+the ``bindgen`` tool will auto-generate the bindings for the included subsystem.
+After building, see the ``*_generated.rs`` output files in the ``rust/bindings/``
+directory.
+
+For parts of the C header that ``bindgen`` doesn't auto generate, e.g. C ``inline``
+functions or macros, it is acceptable to add a small wrapper function
+to ``rust/helpers.c`` to make it available for the Rust side as well.
+
+Abstractions
+~~~~~~~~~~~~
+
+Abstractions are the layer between the bindings and the in-kernel users. They are
+located in ``rust/kernel/`` and their role is to encapsulate the unsafe access
+to the bindings into an as-safe-as-possible API that they expose to their users.
+Users of the abstractions include things like drivers or file systems written in Rust.
+
+Besides the safety aspect, the abstractions are supposed to be "ergonomic", in the
+sense that they turn the C interfaces into "idiomatic" Rust code. Basic examples are
+to turn the C resource acquisition and release into Rust constructors and destructors
+or C integer error codes into Rust's ``Result``.
+
 
 Conditional compilation
 -----------------------
Signature · 1 line
2.28.0
Reviewed-by on v4 1/1
#1 lore raw
Quoted 21 lines · Dirk Behme

Add some basics explained by Miguel in [1] to the documentation. And connect it with some hints where this is implemented in the kernel.

[1] https://www.linuxfoundation.org/webinars/rust-for-linux-writing-abstractions-and-drivers

---
 Documentation/rust/general-information.rst | 56 ++++++++++++++++++++++
 1 file changed, 56 insertions(+)

Changes in v4: Rebase against rust-dev v6.9-rc1 to re-start this topic. If
the reviewers are too busy and its not a good time for it just let me know :)

Documentation/rust/general-information.rst +0 -0
index 081397827a7e..6c6fb3a5e318 100644

Documentation/rust/general-information.rst but it is intended that coverage is expanded as time goes on. "Leaf" modules · line 70

Quoted 7 lines
Documentation/rust/general-information.rst +4 -0
@@ -64,6 +64,62 @@ but it is intended that coverage is expanded as time goes on. "Leaf" modules
 (e.g. drivers) should not use the C bindings directly. Instead, subsystems
 should provide as-safe-as-possible abstractions as needed.
 
+.. code-block::
+
+	                                                rust/bindings/
+                                                       (rust/helpers.c)

This needs updating when [1] lands, I don't know if Miguel wants to do it when picking this, or if you should do this before.

Quoted 28 lines
Documentation/rust/general-information.rst +28 -0
+
+                                                          include/ -----+ <-+
+                                                                        |   |
+         drivers/              rust/kernel/              +----------+ <-+   |
+           fs/                                           | bindgen  |       |
+          .../            +-------------------+          +----------+ --+   |
+                          |    Abstractions   |                         |   |
+       +---------+        | +------+ +------+ |          +----------+   |   |
+       | my_foo  | -----> | | foo  | | bar  | | -------> | Bindings | <-+   |
+       | driver  |  Safe  | | sub- | | sub- | |  Unsafe  |          |       |
+       +---------+        | |system| |system| |          | bindings | <-----+
+            |             | +------+ +------+ |          |  crate   |       |
+            |             |   kernel crate    |          +----------+       |
+            |             +-------------------+                             |
+            |                                                               |
+            +------------------# FORBIDDEN #--------------------------------+
+
+The main idea is to encapsulate all direct interaction with the kernel's C APIs
+into carefully reviewed and documented abstractions. These are then considered to
+be sound. The goal is that users of these abstractions ("my_foo driver") cannot
+introduce undefined behavior (UB) as long as:
+
+#. the abstractions are correct.
+#. they don't use ``unsafe{..}``. Or they uphold the preconditions of all unsafe
+   operations that they perform if they use ``unsafe{..}`` for performance optimizations
+   or to call unsafe abstractions.
+
+Bindings

Documentation/rust/general-information.rst but it is intended that coverage is expanded as time goes on. "Leaf" modules · line 108

Quoted 10 lines
Documentation/rust/general-information.rst +10 -0
+~~~~~~~~
+
+By including a C header from ``include/`` into ``rust/bindings/bindings_helper.h``
+the ``bindgen`` tool will auto-generate the bindings for the included subsystem.
+After building, see the ``*_generated.rs`` output files in the ``rust/bindings/``
+directory.
+
+For parts of the C header that ``bindgen`` doesn't auto generate, e.g. C ``inline``
+functions or macros, it is acceptable to add a small wrapper function
+to ``rust/helpers.c`` to make it available for the Rust side as well.

Also here.

Quoted 18 lines
Documentation/rust/general-information.rst +14 -1
+
+Abstractions
+~~~~~~~~~~~~
+
+Abstractions are the layer between the bindings and the in-kernel users. They are
+located in ``rust/kernel/`` and their role is to encapsulate the unsafe access
+to the bindings into an as-safe-as-possible API that they expose to their users.
+Users of the abstractions include things like drivers or file systems written in Rust.
+
+Besides the safety aspect, the abstractions are supposed to be "ergonomic", in the
+sense that they turn the C interfaces into "idiomatic" Rust code. Basic examples are
+to turn the C resource acquisition and release into Rust constructors and destructors
+or C integer error codes into Rust's ``Result``.
+
 
 Conditional compilation
 -----------------------
--
Quoted 3 lines

2.28.0

Otherwise, this looks good to me:

[1]: https://lore.kernel.org/rust-for-linux/[email protected]/

Signature · 2 lines
Cheers,
Benno
Applied
#2 lore raw
In reply to Dirk Behme
Quoted 9 lines · Dirk Behme

Add some basics explained by Miguel in [1] to the documentation. And connect it with some hints where this is implemented in the kernel.

[1] https://www.linuxfoundation.org/webinars/rust-for-linux-writing-abstractions-and-drivers

[ Reworded first section for better clarity and some minor nits. Changed link into Link tag, use tabs for code block indentation and wrap at 80. - Miguel ]

Dirk: please double-check that you are OK with the reword. Thanks!

Applied to rust-next -- thanks everyone!

Cheers,
Miguel