Skip to contentSkip to conversation

rust: tty: introduce TTY subsystem abstractions and rttyprintk

RFC PATCH In review SeungJong Ha 4 participants 13 messages Jan 26

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover rust: tty: introduce TTY subsystem abstractions and rttyprintk

Hello,

This RFC patch series introduces Rust abstractions for the TTY subsystem and implements a sample driver, rttyprintk.

Currently, TTY abstractions are missing in the
Rust for Linux implementation. This series aims to fill that gap.

It consists of two main parts:
- Rust TTY abstractions
- The rttyprintk driver
rttyprintk serves as the first example of a Rust TTY driver. I chose to port ttyprintk because its simplicity makes it and ideal candidate for validating the new abstractions and demonstrating their usage.

Thank you!

---
SeungJong Ha (3):
rust: bindings: add TTY subsystem headers
rust: tty: add TTY subsystem abstractions
char: rttyprintk: add Rust TTY printk driver

 drivers/char/Kconfig            |  13 ++
 drivers/char/Makefile           |   1 +
 drivers/char/rttyprintk.rs      | 180 +++++++++++++++
 rust/bindings/bindings_helper.h |   3 +
 rust/kernel/lib.rs              |   2 +
 rust/kernel/tty.rs              | 173 +++++++++++++++
 rust/kernel/tty/driver.rs       | 478 ++++++++++++++++++++++++++++++++++++++++
 rust/kernel/tty/port.rs         | 148 +++++++++++++
 8 files changed, 998 insertions(+)

---
base-commit: e741e19d7691c5e6f5c2bbff980d835dccb86054
change-id: 20260126-rust-tty-printk-driver-ccdca3263d61

Best regards,

Signature · 1 line
SeungJong Ha <[email protected]>
#1 lore raw

RFC PATCH 1/3 rust: bindings: add TTY subsystem headers

Add bindings for the TTY subsystem by including the following headers:
- linux/tty.h
- linux/tty_driver.h
- linux/tty_port.h

These bindings are needed for the upcoming Rust TTY driver abstractions.

---
 rust/bindings/bindings_helper.h | 3 +++
 1 file changed, 3 insertions(+)
rust/bindings/bindings_helper.h +3 -0
index a067038b4b42..dc326eb84955 100644
@@ -81,6 +81,9 @@
 #include <linux/security.h>
 #include <linux/slab.h>
 #include <linux/task_work.h>
+#include <linux/tty.h>
+#include <linux/tty_driver.h>
+#include <linux/tty_port.h>
 #include <linux/tracepoint.h>
 #include <linux/usb.h>
 #include <linux/wait.h>
Signature · 1 line
2.43.0
#2 lore raw

RFC PATCH 2/3 rust: tty: add TTY subsystem abstractions

In reply to SeungJong Ha

Add Rust abstractions for the TTY subsystem, providing safe wrappers for tty_struct, tty_driver, and tty_port.

The abstractions are organized as follows:

- tty.rs: Core Tty<DriverData, DriverState> wrapper providing type-safe access to tty_struct with generic parameters for driver-specific data.

- tty/driver.rs: TtyDriverBuilder and TtyDriver for creating and registering TTY drivers. Includes:
- Operations trait for implementing TTY callbacks (open, close, write, write_room, hangup)
- Driver flags, termios output flags, and driver type constants

- tty/port.rs: DriverPort<Ops> combining tty_port with driver-specific data following the C pattern of embedding tty_port as the first field. Includes Operations trait for port callbacks (shutdown).

Key design decisions:
- Generic DriverData and DriverState types allow drivers to specify their own data types (typically Arc<T>) for per-tty and driver-wide state respectively.
- Pin-initialization is used throughout for safe handling of self-referential structures.
- The #[repr(C)] DriverPort layout enables container_of operations.

This provides the foundation for implementing TTY drivers in Rust.

---
 rust/kernel/lib.rs        |   2 +
 rust/kernel/tty.rs        | 173 +++++++++++++++++
 rust/kernel/tty/driver.rs | 478 ++++++++++++++++++++++++++++++++++++++++++++++
 rust/kernel/tty/port.rs   | 148 ++++++++++++++
 4 files changed, 801 insertions(+)
rust/kernel/lib.rs +2 -0
index f812cf120042..0160bfb54547 100644
@@ -147,6 +147,8 @@
 pub mod time;
 pub mod tracepoint;
 pub mod transmute;
+#[cfg(CONFIG_TTY)]
+pub mod tty;
 pub mod types;
 pub mod uaccess;
 #[cfg(CONFIG_USB = "y")]
rust/kernel/tty.rs new file +173 -0

173 lines not on this page · raw message

rust/kernel/tty/driver.rs new file +478 -0

478 lines not on this page · raw message

rust/kernel/tty/port.rs new file +148 -0

148 lines not on this page · raw message

Signature · 1 line
2.43.0
#6 lore raw

RFC PATCH 3/3 char: rttyprintk: add Rust TTY printk driver

In reply to SeungJong Ha

Add a Rust implementation of the ttyprintk driver, demonstrating the new TTY Rust abstractions.

This driver creates /dev/rttyprintk which allows user messages to be written to the kernel log via printk, similar to the existing C ttyprintk driver.

Features:
- Uses the new kernel::tty abstractions for type-safe TTY operations
- Implements TtyDevice with open/close/write/write_room/hangup callbacks
- Uses DriverPort with SpinLock-protected TpkState for thread-safe buffer management
- Buffers up to 508 bytes per line, flushing on newline or buffer full
- Messages are logged at KERN_INFO level with [U] prefix

The driver serves as a reference implementation for Rust TTY drivers and demonstrates:
- Pin-initialization patterns for TTY drivers and ports
- Arc-based shared state between driver callbacks
- Safe handling of driver_data and driver_state
- Integration with kernel printk for output

---
 drivers/char/Kconfig       |  13 ++++
 drivers/char/Makefile      |   1 +
 drivers/char/rttyprintk.rs | 180 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 194 insertions(+)
drivers/char/Kconfig +13 -0
index d2cfc584e202..66a482024ff4 100644
@@ -31,6 +31,19 @@ config TTY_PRINTK_LEVEL
 	help
 	  Printk log level to use for ttyprintk messages.
 
+config TTY_DEV_RUST_PRINTK
+	tristate "Rust TTY driver to output user messages via printk"
+	depends on RUST && TTY
+	default n
+	help
+	  If you say Y here, the support for writing user messages (i.e.
+	  console messages) via printk is available, implemented in Rust.
+
+	  This is the Rust implementation of the ttyprintk driver,
+	  demonstrating rkernel domain isolation for kernel modules.
+
+	  If unsure, say N.
+
 config PRINTER
 	tristate "Parallel printer support"
 	depends on PARPORT
drivers/char/Makefile +1 -0
index 1291369b9126..608bb6d724a0 100644
@@ -5,6 +5,7 @@
 
 obj-y				+= mem.o random.o
 obj-$(CONFIG_TTY_PRINTK)	+= ttyprintk.o
+obj-$(CONFIG_TTY_DEV_RUST_PRINTK)	+= rttyprintk.o
 obj-y				+= misc.o
 obj-$(CONFIG_TEST_MISC_MINOR)	+= misc_minor_kunit.o
 obj-$(CONFIG_ATARI_DSP56K)	+= dsp56k.o
drivers/char/rttyprintk.rs new file +180 -0

180 lines not on this page · raw message

Signature · 1 line
2.43.0
#9 lore raw
In reply to SeungJong Ha
Quoted 10 lines · On Mon, Jan 26, 2026 at 12:22:07PM +0000, SeungJong Ha via B4 Relay wrote:

Hello,

This RFC patch series introduces Rust abstractions for the TTY subsystem and implements a sample driver, rttyprintk.

Currently, TTY abstractions are missing in the
Rust for Linux implementation. This series aims to fill that gap.

It consists of two main parts:
- Rust TTY abstractions

What type of new tty driver are you writing that you need these bindings? The need for new tty drivers is quite low based on the lack of new ones being added to the tree anymore. Are you sure you just don't want a serial port driver instead?

Quoted 5 lines

- The rttyprintk driver
rttyprintk serves as the first example of a Rust TTY driver. I chose to port ttyprintk because its simplicity makes it and ideal candidate for validating the new abstractions and demonstrating their usage.

I would want to see a real need for this before going any further. It's great that you created these bindings, but without a need, I don't see why this should even be reviewed.

thanks,

greg k-h

#7 lore raw

Re: [PATCH RFC 3/3] char: rttyprintk: add Rust TTY printk driver

In reply to SeungJong Ha

drivers/char/rttyprintk.rs line 156

Quoted 5 lines · On Mon, Jan 26, 2026 at 12:22:10PM +0000, SeungJong Ha via B4 Relay wrote:
drivers/char/rttyprintk.rs new file +5 -0
+        let opts = tty::Options {
+            driver_name: c_str!("rttyprintk"),
+            name: c_str!("rttyprintk"),
+            major: tty::TTYAUX_MAJOR,
+            minor_start: 4,

You are not documenting the "take over" of this minor number under this major number anywhere, which isn't ok for using a static minor number.

And while I understand the need for an example driver, the ttyprintk driver is a very trivial one overall, we don't need another one in the system as one is enough :)

thanks,

greg k-h

#3 lore raw

Re: [PATCH RFC 2/3] rust: tty: add TTY subsystem abstractions

In reply to SeungJong Ha

rust/kernel/tty/driver.rs line 54

Quoted 2 lines · On Mon, Jan 26, 2026 at 12:22:09PM +0000, SeungJong Ha via B4 Relay wrote:
rust/kernel/tty/driver.rs new file +2 -0
+/// Major device number for TTY aux devices.
+pub const TTYAUX_MAJOR: i32 = bindings::TTYAUX_MAJOR as i32;

This does not belong in the rust bindings as something to export, sorry. I see you wanting to use it in your example driver, but if you do need it elsewhere, just declare and use it there, the rust layer should not be the one exporting it.

You also are exporting lots of other stuff with these bindings that are not needed or used. Are you sure that's the right thing to do?

thanks,

greg k-h

author
#10 lore raw
In reply to Greg Kroah-Hartman

2026년 1월 26일 (월) PM 9:48, Greg Kroah-Hartman <[email protected]>님이 작성:

Quoted 10 lines · On Mon, Jan 26, 2026 at 12:22:07PM +0000, SeungJong Ha via B4 Relay wrote:
Quoted 8 lines

Hello,

This RFC patch series introduces Rust abstractions for the TTY subsystem and implements a sample driver, rttyprintk.

Currently, TTY abstractions are missing in the
Rust for Linux implementation. This series aims to fill that gap.

Quoted 8 lines
Quoted 2 lines

It consists of two main parts:
- Rust TTY abstractions

What type of new tty driver are you writing that you need these bindings? The need for new tty drivers is quite low based on the lack of new ones being added to the tree anymore. Are you sure you just don't want a serial port driver instead?

To answer your question directly: I do not have a plan to implement a specific serial port driver at this moment.
My motivation for this patch series was simply to fill the missing TTY subsystem gap in the Rust for Linux project. I believed that providing these abstractions would be valuable as foundational infrastructure, enabling future developers to write TTY-related drivers in Rust.

Quoted 6 lines
Quoted 5 lines

- The rttyprintk driver
rttyprintk serves as the first example of a Rust TTY driver. I chose to port ttyprintk because its simplicity makes it and ideal candidate for validating the new abstractions and demonstrating their usage.

Quoted 7 lines

I would want to see a real need for this before going any further. It's great that you created these bindings, but without a need, I don't see why this should even be reviewed.

thanks,

greg k-h

I understand your concern about adding code without a "real need" or an active user in the tree.

If you believe that these abstractions are premature or unnecessary without a concrete driver implementation to back them up, I fully accept that decision. In that case, I am content with leaving this work as a reference implementation for those who might be interested in the future.

Thanks,

SeungJong Ha

author
#4 lore raw

Re: [PATCH RFC 2/3] rust: tty: add TTY subsystem abstractions

In reply to Greg Kroah-Hartman

2026년 1월 26일 (월) PM 9:55, Greg Kroah-Hartman <[email protected]>님이 작성:

Quoted 10 lines · On Mon, Jan 26, 2026 at 12:22:09PM +0000, SeungJong Ha via B4 Relay wrote:

rust/kernel/tty/driver.rs line 54

Quoted 2 lines
rust/kernel/tty/driver.rs new file +2 -0
+/// Major device number for TTY aux devices.
+pub const TTYAUX_MAJOR: i32 = bindings::TTYAUX_MAJOR as i32;

This does not belong in the rust bindings as something to export, sorry. I see you wanting to use it in your example driver, but if you do need it elsewhere, just declare and use it there, the rust layer should not be the one exporting it.

You are right. TTYAUX_MAJOR should be defined locally in the driver.

Quoted 6 lines

You also are exporting lots of other stuff with these bindings that are not needed or used. Are you sure that's the right thing to do?

thanks,

greg k-h

I also agree that I should have restricted exports to only what is currently used.

Thanks,

SeungJong Ha

author
#8 lore raw

Re: [PATCH RFC 3/3] char: rttyprintk: add Rust TTY printk driver

In reply to Greg Kroah-Hartman

2026년 1월 26일 (월) PM 9:53, Greg Kroah-Hartman <[email protected]>님이 작성:

Quoted 11 lines · On Mon, Jan 26, 2026 at 12:22:10PM +0000, SeungJong Ha via B4 Relay wrote:

drivers/char/rttyprintk.rs line 156

Quoted 5 lines
drivers/char/rttyprintk.rs new file +5 -0
+        let opts = tty::Options {
+            driver_name: c_str!("rttyprintk"),
+            name: c_str!("rttyprintk"),
+            major: tty::TTYAUX_MAJOR,
+            minor_start: 4,

You are not documenting the "take over" of this minor number under this major number anywhere, which isn't ok for using a static minor number.

You are correct. I should have explicitly documented the takeover of the static minor number. I'll change it dynamic.

Quoted 7 lines

And while I understand the need for an example driver, the ttyprintk driver is a very trivial one overall, we don't need another one in the system as one is enough :)

thanks,

greg k-h

I also agree that a duplicate driver is unnecessary. It was selected only as the simplest candidate to validate the abstractions. I agree that a sample driver implementation (e.g. in samples/rust/) would have been more suitable for this purpose.

Thanks,

SeungJong Ha

#11 lore raw
In reply to 하승종
Quoted 7 lines · 하승종

To answer your question directly: I do not have a plan to implement a specific serial port driver at this moment.
My motivation for this patch series was simply to fill the missing TTY subsystem gap in the Rust for Linux project. I believed that providing these abstractions would be valuable as foundational infrastructure, enabling future developers to write TTY-related drivers in Rust.

Yeah, as Greg says, the kernel needs a user for new code.

In these two pages I explain a bit the usual rules involved and some general guidelines on how to proceed with new abstractions:

https://rust-for-linux.com/contributing#submitting-new-abstractions-and-modules
https://rust-for-linux.com/rust-reference-drivers

I hope that helps, and thanks for contributing -- a reference implementation is always good to have in the list :)

Cheers,
Miguel

author
#12 lore raw

2026년 1월 26일 (월) PM 10:25, Miguel Ojeda <[email protected]>님이 작성:

Quoted 15 lines · 하승종
Quoted 7 lines

To answer your question directly: I do not have a plan to implement a specific serial port driver at this moment.
My motivation for this patch series was simply to fill the missing TTY subsystem gap in the Rust for Linux project. I believed that providing these abstractions would be valuable as foundational infrastructure, enabling future developers to write TTY-related drivers in Rust.

Yeah, as Greg says, the kernel needs a user for new code.

In these two pages I explain a bit the usual rules involved and some general guidelines on how to proceed with new abstractions:

Quoted 8 lines

https://rust-for-linux.com/contributing#submitting-new-abstractions-and-modules
https://rust-for-linux.com/rust-reference-drivers

I hope that helps, and thanks for contributing -- a reference implementation is always good to have in the list :)

Cheers,
Miguel

Thanks for good comments!

I will make sure to read the guidelines carefully to align with the community standards for
future contributions. I am glad that this series can serve as a reference for others.

Best regards,

SeungJong Ha

#5 lore raw

Re: [PATCH RFC 2/3] rust: tty: add TTY subsystem abstractions

In reply to SeungJong Ha
kernel test robot noticed the following build errors:

Hi SeungJong,

[This is a private test report for your RFC patch.] kernel test robot noticed the following build errors:

[auto build test ERROR on e741e19d7691c5e6f5c2bbff980d835dccb86054]

url:    https://github.com/intel-lab-lkp/linux/commits/SeungJong-Ha-via-B4-Relay/rust-bindings-add-TTY-subsystem-headers/20260126-202713
base:   e741e19d7691c5e6f5c2bbff980d835dccb86054
patch link:    https://lore.kernel.org/r/20260126-rust-tty-printk-driver-v1-2-28604e7e100e%40gmail.com
patch subject: [PATCH RFC 2/3] rust: tty: add TTY subsystem abstractions
config: x86_64-rhel-9.4-rust (https://download.01.org/0day-ci/archive/20260126/[email protected]/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
rustc: rustc 1.88.0 (6b00bc388 2025-06-23)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260126/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

PATH=/opt/cross/clang-20/bin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
INFO PATH=/opt/cross/rustc-1.88.0-bindgen-0.72.1/cargo/bin:/opt/cross/clang-20/bin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
/usr/bin/timeout -k 100 12h /usr/bin/make KCFLAGS= -fno-crash-diagnostics -Wno-error=return-type -Wreturn-type -funsigned-char -Wundef -falign-functions=64 W=1 --keep-going LLVM=1 -j32 -C source O=/kbuild/obj/consumer/x86_64-rhel-9.4-rust ARCH=x86_64 SHELL=/bin/bash rustfmtcheck
make: Entering directory '/kbuild/src/consumer'
make[1]: Entering directory '/kbuild/obj/consumer/x86_64-rhel-9.4-rust'

>> Diff in rust/kernel/tty/port.rs:9:
    use pin_init::PinInit;

-use crate::{

   -    bindings,
   -    error::VTABLE_DEFAULT_ERROR,
   -    prelude::*,
   -    types::Opaque,
   -};
   +use crate::{bindings, error::VTABLE_DEFAULT_ERROR, prelude::*, types::Opaque};
    
    /// A combined TTY port and driver data structure.
    ///
>> Diff in rust/kernel/tty/driver.rs:6:
    use core::marker::PhantomData;

-use super::{

   -    DriverPort,
   -    PortOperations,
   -    Tty,
   -};
   +use super::{DriverPort, PortOperations, Tty};
    use crate::{
        bindings,
   -    error::{
   -        Error,
   -        Result,
   -        VTABLE_DEFAULT_ERROR,
   -    },
   +    error::{Error, Result, VTABLE_DEFAULT_ERROR},
        prelude::*,
        sync::Arc,
        types::Opaque,
>> Diff in rust/kernel/tty/driver.rs:6:
    use core::marker::PhantomData;

-use super::{

   -    DriverPort,
   -    PortOperations,
   -    Tty,
   -};
   +use super::{DriverPort, PortOperations, Tty};
    use crate::{
        bindings,
   -    error::{
   -        Error,
   -        Result,
   -        VTABLE_DEFAULT_ERROR,
   -    },
   +    error::{Error, Result, VTABLE_DEFAULT_ERROR},
        prelude::*,
        sync::Arc,
        types::Opaque,
>> Diff in rust/kernel/tty/port.rs:9:
    use pin_init::PinInit;

-use crate::{

   -    bindings,
   -    error::VTABLE_DEFAULT_ERROR,
   -    prelude::*,
   -    types::Opaque,
   -};
   +use crate::{bindings, error::VTABLE_DEFAULT_ERROR, prelude::*, types::Opaque};
    
    /// A combined TTY port and driver data structure.
    ///
>> Diff in rust/kernel/tty.rs:14:
    use core::marker::PhantomData;
    
    pub use driver::{
   -    flags,
   -    oflag,
   -    DriverType,
   -    Operations,
   -    Options,
   -    TtyDriver,
   -    TtyDriverBuilder,
   -    TTYAUX_MAJOR,
   +    flags, oflag, DriverType, Operations, Options, TtyDriver, TtyDriverBuilder, TTYAUX_MAJOR,
    };

-pub use port::{
- DriverPort,
- Operations as PortOperations,
-};
+pub use port::{DriverPort, Operations as PortOperations};

-use crate::{
- bindings,
- sync::Arc,
-};

   +use crate::{bindings, sync::Arc};
    
    /// TTY struct wrapper, generic over driver data and driver state types.
    ///
>> Diff in rust/kernel/tty/driver.rs:6:
    use core::marker::PhantomData;

-use super::{

   -    DriverPort,
   -    PortOperations,
   -    Tty,
   -};
   +use super::{DriverPort, PortOperations, Tty};
    use crate::{
        bindings,
   -    error::{
   -        Error,
   -        Result,
   -        VTABLE_DEFAULT_ERROR,
   -    },
   +    error::{Error, Result, VTABLE_DEFAULT_ERROR},
        prelude::*,
        sync::Arc,
        types::Opaque,
>> Diff in rust/kernel/tty/port.rs:9:
    use pin_init::PinInit;

-use crate::{

   -    bindings,
   -    error::VTABLE_DEFAULT_ERROR,
   -    prelude::*,
   -    types::Opaque,
   -};
   +use crate::{bindings, error::VTABLE_DEFAULT_ERROR, prelude::*, types::Opaque};
    
    /// A combined TTY port and driver data structure.
    ///
>> Diff in rust/kernel/tty.rs:14:
    use core::marker::PhantomData;
    
    pub use driver::{
   -    flags,
   -    oflag,
   -    DriverType,
   -    Operations,
   -    Options,
   -    TtyDriver,
   -    TtyDriverBuilder,
   -    TTYAUX_MAJOR,
   +    flags, oflag, DriverType, Operations, Options, TtyDriver, TtyDriverBuilder, TTYAUX_MAJOR,
    };

-pub use port::{
- DriverPort,
- Operations as PortOperations,
-};
+pub use port::{DriverPort, Operations as PortOperations};

-use crate::{
- bindings,
- sync::Arc,
-};

   +use crate::{bindings, sync::Arc};
    
    /// TTY struct wrapper, generic over driver data and driver state types.
    ///

make[2]: *** [Makefile:1871: rustfmt] Error 123
make[2]: Target 'rustfmtcheck' not remade because of errors. make[1]: Leaving directory '/kbuild/obj/consumer/x86_64-rhel-9.4-rust' make[1]: *** [Makefile:248: __sub-make] Error 2
make[1]: Target 'rustfmtcheck' not remade because of errors.
make: *** [Makefile:248: __sub-make] Error 2
make: Target 'rustfmtcheck' not remade because of errors.
make: Leaving directory '/kbuild/src/consumer'

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki