Rust vs C Kernel Modules: Safety, Ergonomics and Trade-offs
Compare one Linux 6.18 module in C and Rust for safety, error handling, build cost and API coverage, then pick the language for your next kernel module.

You can write a kernel module in either language today, and the choice is not obvious. Rust removes a class of bugs the compiler can see and adds a toolchain, an API layer and a smaller pool of reviewers. C is what every subsystem already speaks, and it leaves every safety rule to you. This post puts the same small module in both languages on a Linux 6.18 tree, compares what each one prevents and costs, and ends with a table to decide by. The scope is deliberate: it compares writing a new module, not whether to rewrite an existing one, and it takes its facts from the kernel's Rust documentation and the sources. For the background, see why the kernel accepted a second language.
The same module twice
Both versions load, log two greetings, and log a count when unloaded. The Rust version, from the first module post, is:
use kernel::prelude::*;
module! {
type: Hello,
name: "rust_hello",
authors: ["Example Author"],
description: "First Rust kernel module",
license: "GPL",
}
struct Hello {
greetings: KVec<&'static str>,
}
impl kernel::Module for Hello {
fn init(_module: &'static ThisModule) -> Result<Self> {
pr_info!("loading\n");
let mut greetings = KVec::new();
greetings.push("hello", GFP_KERNEL)?;
greetings.push("kernel", GFP_KERNEL)?;
for greeting in greetings.iter() {
pr_info!("greeting: {}\n", greeting);
}
Ok(Hello { greetings })
}
}
impl Drop for Hello {
fn drop(&mut self) {
pr_info!("unloading, {} greetings dropped\n", self.greetings.len());
}
}The C equivalent keeps its state in globals and frees by hand:
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/module.h>
#include <linux/slab.h>
static const char **greetings;
static size_t count;
static int __init hello_init(void)
{
size_t i;
pr_info("loading\n");
greetings = kmalloc_array(2, sizeof(*greetings), GFP_KERNEL);
if (!greetings)
return -ENOMEM;
greetings[0] = "hello";
greetings[1] = "kernel";
count = 2;
for (i = 0; i < count; i++)
pr_info("greeting: %s\n", greetings[i]);
return 0;
}
static void __exit hello_exit(void)
{
pr_info("unloading, %zu greetings dropped\n", count);
kfree(greetings);
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_DESCRIPTION("First C kernel module");
MODULE_LICENSE("GPL");On a kernel with each loaded and removed in turn, the log for the C module reads the same as the Rust one, apart from the module name in the prefix:
hello: loading
hello: greeting: hello
hello: greeting: kernel
hello: unloading, 2 greetings droppedThe behavior is identical, which is the point of the comparison: the two languages differ in what the compiler checks and what the programmer carries in their head.
What the compiler checks in Rust
Ownership and cleanup. In the C version, greetings is freed in hello_exit, and an error path in hello_init after the allocation would have to free it before returning. Add a third step that can fail, and every later return needs its own cleanup or a goto ladder. In the Rust version the vector is a local that is dropped on any early return, and the stored copy is dropped when the module is unloaded. A leak on an error path is not something you can write by accident.
Use after free and double free. In C, kfree(greetings) followed by greetings[0] compiles. In Rust, once a value is moved or dropped the compiler rejects any later use, with errors such as E0382, "borrow of moved value". The compiler enforces that for every build, not only the paths a test happens to exercise.
Locks tied to data. The Mutex type in the kernel crate owns the data it protects, so the data cannot be reached without the lock, and the lock is released when the guard goes out of scope. In C, the connection between a struct mutex and the fields it guards exists only in a comment.
Data races across threads. Rust's Send and Sync traits say which types can move or be shared across threads. A type that is not safe to share cannot be placed in a global that other contexts reach, and the compiler stops you. C has no equivalent, and a race is found by testing or by KCSAN at run time.
Integer overflow. With CONFIG_RUST_OVERFLOW_CHECKS, which is on by default, arithmetic overflow panics, and slice indexing is bounds checked. These are runtime checks and a panic is serious, but they turn silent corruption into a loud failure. In C, signed overflow is undefined behavior and an out-of-range index just reads or writes memory.
Not every bug class goes away. Logic errors, wrong lock ordering, deadlocks, resource leaks through reference cycles and incorrect hardware programming are all still yours, and any unsafe block, whether in your code or in an abstraction, is a place where the compiler trusts you.
What C does better today
API coverage. C can call every kernel API. Rust can call only those that have an abstraction in the kernel crate, or you write one with unsafe. The documentation says that Rust abstractions are not available for all the kernel's internal APIs and concepts, and that coverage is meant to expand over time. Check the generated rustdoc for your tree before you pick Rust for a driver: if the subsystem you need is not there, the work includes writing the abstraction, with the review that implies.
Build and tooling. A C module needs the kernel's normal toolchain. A Rust module needs rustc of a supported version, bindgen and libclang, a build with LLVM=1 for the best-supported setup, and a kernel built with CONFIG_RUST. The Rust build is slower, and symbol names in a debugger need a recent GDB or Binutils for Rust's v0 mangling.
Reviewers and maintainers. Most kernel maintainers read C fluently. The set who can review Rust well, and the abstractions it relies on, is smaller. A Rust driver depending on a C subsystem also depends on that subsystem's maintainers accepting the Rust abstraction.
Stability of the API. The kernel crate moves fast: type names such as KVec and KBox are recent, and abstractions are reworked as understanding improves. A C module written against stable exported interfaces changes less often. The Rust documentation itself describes the support as in development.
Production status. The 6.18 documentation says there are no in-tree drivers suitable or intended for production use. That is a statement of the project's position that you should weigh against what you plan to ship.
What does not differ
Performance is not the reason to choose either. Rust compiles through LLVM to code of the same kind as Clang does for C, and the abstractions are designed to compile down to the same calls. The checks that cost something at run time, overflow and bounds checks, are the ones listed above and can be configured. Do not accept or quote a speed claim for either side without a measurement on your hardware.
How to decide
| Situation | Better fit | Why |
|---|---|---|
| New driver in a subsystem that has Rust abstractions (misc, platform, PCI, PHY) | Rust | The safe layer exists and the compiler checks the ownership rules |
| New driver in a subsystem with no abstraction | C, or Rust if you will write the abstraction | Writing the abstraction is the real work |
| Patch to an existing C driver | C | Mixing languages in one driver is not what the tree does |
| Module for a distribution kernel you do not build | C | Needs a Rust-enabled kernel and a matching toolchain |
| Code that handles untrusted input or complex lifetimes | Rust | Ownership and bounds checks remove classic failure modes |
| A one-off experiment or a quick probe | C or a BPF program | The build and API overhead is not worth it |
| Team has no Rust experience and a deadline | C | The learning curve is real |
Where the comparison stops
This compares one small module. A real driver adds interrupts, DMA, power management and a user-space interface, and the gap between the two languages depends on how much of that has abstractions. The right answer today for your subsystem may not be the right answer in two kernel releases. Check the Rust abstractions that exist in the version you target before deciding.


