Skip to contentSkip to conversation

rust: console: add abstraction for kernel console drivers

PATCH In review Matthew Wood 3 participants 4 messages Feb 27 – Feb 28 Rust

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rust: console: add abstraction for kernel console drivers

Add a safe Rust abstraction for the kernel's struct console, enabling console drivers to be implemented in Rust that provides:

- ConsoleOps trait with a required write callback and an optional setup callback, mirroring the C console_operations interface. The trait requires Send + Sync to reflect that console write may be called from any context including IRQ.

- Console<T> struct that wraps struct console using pin-init and Opaque<T>, with automatic unregistration via PinnedDrop. Registration is performed through a pin-initializer returned by Console::register(), which uses pin_chain to set the data pointer and call register_console() after the struct is pinned.

- ConsoleOpsAdapter<T> that provides the extern "C" callbacks bridging from the kernel's function pointers to the Rust trait methods. The #[vtable] attribute on ConsoleOps enables compile-time detection of whether setup is implemented via T::HAS_SETUP.

- Console flag constants re-exported from the C enum cons_flags.

C helper functions are added for register_console(), unregister_console(), and console_is_registered() as these are either inlines or macros that cannot be called directly from Rust through bindgen.

This abstraction is a dependency for a Rust netconsole implementation I am working on.

---
 rust/bindings/bindings_helper.h |   1 +
 rust/helpers/console.c          |  22 +++
 rust/helpers/helpers.c          |   1 +
 rust/kernel/console.rs          | 230 ++++++++++++++++++++++++++++++++
 rust/kernel/lib.rs              |   1 +
 5 files changed, 255 insertions(+)
 create mode 100644 rust/helpers/console.c
 create mode 100644 rust/kernel/console.rs
rust/bindings/bindings_helper.h +1 -0
index 083cc44aa952..eeddf2374f00 100644
@@ -43,6 +43,7 @@
 #include <linux/clk.h>
 #include <linux/completion.h>
 #include <linux/configfs.h>
+#include <linux/console.h>
 #include <linux/cpu.h>
 #include <linux/cpufreq.h>
 #include <linux/cpumask.h>
rust/helpers/console.c new file +22 -0
new file mode 100644
index 000000000000..b101c6c749fd
@@ -0,0 +1,22 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * Rust helpers for console.
+ */
+
+#include <linux/console.h>
+
+void rust_helper_register_console(struct console *console)
+{
+	register_console(console);
+}
+
+int rust_helper_unregister_console(struct console *console)
+{
+	return unregister_console(console);
+}
+
+bool rust_helper_console_is_registered(struct console *console)
+{
+	return console_is_registered(console);
+}
rust/helpers/helpers.c +1 -0
index a3c42e51f00a..2b818126ce02 100644
@@ -22,6 +22,7 @@
 #include "build_bug.c"
 #include "clk.c"
 #include "completion.c"
+#include "console.c"
 #include "cpu.c"
 #include "cpufreq.c"
 #include "cpumask.c"
rust/kernel/console.rs new file +230 -0

230 lines not on this page · raw message

rust/kernel/lib.rs +1 -0
index 3da92f18f4ee..6381225cbe9c 100644
@@ -78,6 +78,7 @@
 pub mod clk;
 #[cfg(CONFIG_CONFIGFS_FS)]
 pub mod configfs;
+pub mod console;
 pub mod cpu;
 #[cfg(CONFIG_CPU_FREQ)]
 pub mod cpufreq;
Signature · 1 line
2.52.0
#1 lore raw
Quoted 22 lines · Matthew Wood

Add a safe Rust abstraction for the kernel's struct console, enabling console drivers to be implemented in Rust that provides:

- ConsoleOps trait with a required write callback and an optional setup callback, mirroring the C console_operations interface. The trait requires Send + Sync to reflect that console write may be called from any context including IRQ.

- Console<T> struct that wraps struct console using pin-init and Opaque<T>, with automatic unregistration via PinnedDrop. Registration is performed through a pin-initializer returned by Console::register(), which uses pin_chain to set the data pointer and call register_console() after the struct is pinned.

- ConsoleOpsAdapter<T> that provides the extern "C" callbacks bridging from the kernel's function pointers to the Rust trait methods. The #[vtable] attribute on ConsoleOps enables compile-time detection of whether setup is implemented via T::HAS_SETUP.

- Console flag constants re-exported from the C enum cons_flags.

Quoted 7 lines

C helper functions are added for register_console(), unregister_console(), and console_is_registered() as these are either inlines or macros that cannot be called directly from Rust through bindgen.

This abstraction is a dependency for a Rust netconsole implementation I am working on.

I'd prefer to see it then, with the user, before even reviewing this one, sorry. That way we "know" it at least works for you.

thanks,

greg k-h

#2 lore raw
Quoted 6 lines · Greg Kroah-Hartman
Quoted 2 lines

This abstraction is a dependency for a Rust netconsole implementation I am working on.

I'd prefer to see it then, with the user, before even reviewing this one, sorry. That way we "know" it at least works for you.

Thanks for the reply Greg, that's a very fair request. Since the complete netconsole Rust implementation crosses several subsytems (module-support, net, configfs, console) and I haven't yet submitted such a large series before:

1) Should I submit the complete implementation w/ dependencies as one series?

2) If I create and include a minimum-viable functional example using this console abstraction, would that suffice?

Regards,
Matthew

#3 lore raw
Quoted 3 lines · Matthew Wood

1) Should I submit the complete implementation w/ dependencies as one series?

It depends a bit on the case, but yes, one option is sending the entire set. It doesn't hurt if it is not very large, at least once, possibly as an RFC, and then you can use it as a reference, e.g. via a link later on, when you submit the subsets.

Another common approach is to provide a link to a branch with the entire set for reference. That is also useful on its own -- some people prefer to fetch a fully prepared branch to test. So I would recommend doing that even if you send the entire series once.

Quoted 2 lines

2) If I create and include a minimum-viable functional example using this console abstraction, would that suffice?

That definitely helps reviewers, please do so. Sometimes people add a sample in samples/rust/ for that as an extra patch.

Nevertheless, maintainers will likely want to have a "real user" as the goal.

I hope that helps!

Cheers,
Miguel