Skip to contentSkip to conversation

docs: rust: extend abstraction and binding documentation

PATCH Superseded Dirk Behme 3 participants 3 messages Feb 9, 2024 – Feb 11, 2024

Subsystems (2)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Dirk Behme

author
#0 lore raw

PATCH v2 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

---
Changes in v2: Try to incorporate the v1 comments from Valentin and Miguel.

 Documentation/rust/general-information.rst | 48 ++++++++++++++++++++++
 1 file changed, 48 insertions(+)
Documentation/rust/general-information.rst +48 -0
index 081397827a7e..91a5a69c7c74 100644
@@ -64,6 +64,54 @@ 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 unsafe handling in carefully reviewed
+and documented abstractions. These are then considered to be sound. With this model
+it is ensured that users of the abstractions ("my_foo driver") can't do anything
+unsound if
+
+#. the abstractions are sound
+#. they don't use ``unsafe()``
+
+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.
+See the ``*_generated.rs`` output files in the ``rust/bindings/`` directory.
+
+For parts of the C header ``bindgen`` doesn't auto generate, e.g. C ``inline``
+functions or macros, there is the option 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. For example
+the drivers or file systems written in Rust. They are located in ``rust/kernel/``
+and their role is to encapsulate the unsafe access to the bindings into a safe API
+that they expose to their users.
+
 
 Conditional compilation
 -----------------------
Signature · 1 line
2.28.0
reviewer
#1 lore raw
Quoted 33 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

---
Changes in v2: Try to incorporate the v1 comments from Valentin and Miguel.

 Documentation/rust/general-information.rst | 48 ++++++++++++++++++++++
 1 file changed, 48 insertions(+)
Documentation/rust/general-information.rst +10 -0
index 081397827a7e..91a5a69c7c74 100644
@@ -64,6 +64,54 @@ 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  |       |
+          .../            +-------------------+          +----------+ --+   |

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

Quoted 10 lines
Documentation/rust/general-information.rst +10 -0
+                          |    Abstractions   |                         |   |
+       +---------+        | +------+ +------+ |          +----------+   |   |
+       | my_foo  | -----> | | foo  | | bar  | | -------> | Bindings | <-+   |
+       | driver  |  Safe  | | sub- | | sub- | |  Unsafe  |          |       |
+       +---------+        | |system| |system| |          | bindings | <-----+
+            |             | +------+ +------+ |          |  crate   |       |
+            |             |   kernel crate    |          +----------+       |
+            |             +-------------------+                             |
+            |                                                               |
+            +------------------# FORBIDDEN #--------------------------------+

Awesome ascii flowchart :)

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

Quoted 1 line
Documentation/rust/general-information.rst +1 -0
+The main idea is to encapsulate all unsafe handling in carefully reviewed

in -> into

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

Quoted 3 lines
Documentation/rust/general-information.rst +3 -0
+and documented abstractions. These are then considered to be sound. With this model
+it is ensured that users of the abstractions ("my_foo driver") can't do anything
+unsound if

"unsound if" -> "unsound if:"

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

Quoted 3 lines
Documentation/rust/general-information.rst +3 -0
+
+#. the abstractions are sound
+#. they don't use ``unsafe()``

Technically there is no unsafe() at this time, only unsafe { ... } :) (and unsafe impl)

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

Quoted 6 lines
Documentation/rust/general-information.rst +6 -0
+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.
+See the ``*_generated.rs`` output files in the ``rust/bindings/`` directory.

Worth mentioning rust/bindings/*_generated is only available after building

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

Quoted 1 line
Documentation/rust/general-information.rst +1 -0
+For parts of the C header ``bindgen`` doesn't auto generate, e.g. C ``inline``

".. C header +that ``bindgen`` doesn't autogenerate"

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

Quoted 2 lines
Documentation/rust/general-information.rst +2 -0
+functions or macros, there is the option to add a small wrapper function
+to ``rust/helpers.c`` to make it available for the Rust side as well.

I would say "it is acceptable to add a small wrapper function". "there is an option to ..." sounds a bit like there's a kconfig option to enable.

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

Quoted 7 lines
Documentation/rust/general-information.rst +7 -0
+Abstractions
+~~~~~~~~~~~~
+
+Abstractions are the layer between the bindings and the in-kernel users. For example
+the drivers or file systems written in Rust. They are located in ``rust/kernel/``
+and their role is to encapsulate the unsafe access to the bindings into a safe API
+that they expose to their users.

I would move the "For example ... in Rust" to the end as something like "Users of abstractions include things like drivers or file systems written in Rust" to make it more clear what is in rust/kernel.

This looks great, the diagram adds a lot to making the organization more clear.

Thanks,
Trevor

#2 lore raw
In reply to Dirk Behme
Quoted 18 lines

+.. code-block::

Documentation/rust/general-information.rst +17 -0
+
+	                                                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    |          +----------+       |
+            |             +-------------------+                             |

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

Quoted 10 lines
Documentation/rust/general-information.rst +10 -0
+            |                                                               |
+            +------------------# FORBIDDEN #--------------------------------+
+
+The main idea is to encapsulate all unsafe handling in carefully reviewed
+and documented abstractions. These are then considered to be sound. With this model
+it is ensured that users of the abstractions ("my_foo driver") can't do anything
+unsound if
+
+#. the abstractions are sound
+#. they don't use ``unsafe()``

I'd like to follow up on some of the things that Miguel mentioned in his earlier message:

- To make it clearer what is meant by "unsafe handling": One could say "... encapsulate all direct interaction with the kernel's C APIs ...". This requires using unsafe Rust, but also leaves the door open for drivers to use unsafe Rust, e.g., for performance optimizations or to call unsafe abstractions.
- To avoid saying that "... [abstractions] are considered to be sound [after review] ...": One could say "The goal must be that users of these abstractions cannot introduce undefined behavior as long as:
1. The abstractions are correct, & 2. they uphold the preconditions of all unsafe operations that they perform."

Quoted 9 lines
Documentation/rust/general-information.rst +9 -0
+
+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.
+See the ``*_generated.rs`` output files in the ``rust/bindings/`` directory.
+
+For parts of the C header ``bindgen`` doesn't auto generate, e.g. C ``inline``

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

Quoted 10 lines
Documentation/rust/general-information.rst +10 -0
+functions or macros, there is the option 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. For example
+the drivers or file systems written in Rust. They are located in ``rust/kernel/``
+and their role is to encapsulate the unsafe access to the bindings into a safe API
+that they expose to their users.

I'd think one should keep the "as-safe-as-possible" aspect here, as there will always be cases where it is not possible to provide safe abstractions.

Another aspect about these APIs that I don't see mentioned here is that they should be "ergonomic", in the sense that they turn "C-ish" interfaces into something that allows you to write "idiomatic" Rust code. Basic examples would be to turn resource acquisition and release into constructors and destructors or integer error codes into Results, and so on. I'm not entirely sure to which extend this is covered by the safety aspect (they have a non-zero overlap), but perhaps it is something that could be mentioned here.

- Best Valentin

Quoted 1 line

+