rust-for-linux
БесплатноБез исполняемых скриптовНе проверенRust-for-Linux skill for writing Linux kernel modules and drivers in Rust. Use when setting up a kernel Rust toolchain, writing a module with the module!…
от mohitmishra786★ 307 звёзд на GitHubОб этом скилле
Rust for Linux
Purpose
Guide agents through writing Linux kernel code in Rust (examples checked against Linux 7.3): toolchain setup, the kernel crate, pin-init, out-of-tree modules, misc and platform drivers, and testing with KUnit.
Triggers
- "How do I write a Linux kernel module in Rust?"
- "
make rustavailablesays Rust is not available" - "How does pin-init /
try_pin_init!work for a kernel mutex?" - "How do I build an out-of-tree Rust kernel module?"
- "Is there a Rust abstraction for this subsystem yet?"
- "How do I test kernel Rust code with KUnit?"
Workflow
1. Status and when to use Rust
Rust stopped being experimental in Linux 7.0 (April 2026). In-tree users include the Android Binder driver, the Nova (NVIDIA) and Tyr (Arm Mali) GPU drivers, the null block driver, several PHY drivers and the DRM panic QR generator. Rust is still not enabled by default.
New driver?
├── Subsystem has Rust abstractions (rust/kernel/<subsystem>.rs)?
│ ├── Yes → Rust is a good fit; follow samples/rust/ for the pattern
│ └── No → C driver, or write the abstraction first (upstream review needed)
└── Touching an existing C driver → stay in C; Rust is per-module, not per-function
Abstractions change between releases. Check rust/kernel/ and samples/rust/ in the tree you
build against rather than copying code written for another version.
2. Toolchain setup
| Tool | Minimum for 7.3 (scripts/min-tool-version.sh) |
|---|---|
| rustc | 1.85.0 (1.95 powerpc, 1.96 s390) |
| bindgen | 0.71.1 (bindgen-cli) |
| LLVM/Clang | 17.0.1 |
# Rust toolchain plus the standard library source (core is rebuilt for the kernel)
rustup component add rust-src
cargo install --locked bindgen-cli
# Clang/LLVM build of the kernel (mixed GCC + Rust builds are still experimental)
make LLVM=1 rustavailable # explains exactly what is missing
# Enable Rust: General setup → Rust support (CONFIG_RUST=y)
make LLVM=1 menuconfig
make LLVM=1 -j"$(nproc)"
# Editor support: writes rust-project.json for rust-analyzer
make LLVM=1 rust-analyzer
CONFIG_RUST cannot be enabled together with RANDSTRUCT/GCC_PLUGIN_RANDSTRUCT, or with
MODVERSIONS unless GENDWARFKSYMS is set. If the option does not show up in menuconfig, run
rustavailable and check those dependencies.
3. A minimal module
// hello_rust.rs
// SPDX-License-Identifier: GPL-2.0
//! Out-of-tree Rust module example.
use kernel::prelude::*;
module! {
type: HelloRust,
name: "hello_rust",
authors: ["Example Author"],
description: "Out-of-tree Rust module",
license: "GPL",
params: {
count: u32 {
default: 3,
description: "Number of values to allocate",
},
},
}
struct HelloRust {
values: KVec<u32>,
}
impl kernel::Module for HelloRust {
fn init(_module: &'static ThisModule) -> Result<Self> {
let n = module_parameters::count.value();
// Every allocation takes GFP flags and can fail: no hidden panics
let mut values = KVec::with_capacity(n as usize, GFP_KERNEL)?;
for i in 0..n {
values.push(i * i, GFP_KERNEL)?;
}
pr_info!("loaded with {} values\n", values.len());
Ok(HelloRust { values })
}
}
impl Drop for HelloRust {
// Drop is the module exit path
fn drop(&mut self) {
pr_info!("unloading, values = {:?}\n", self.values);
}
}
# Kbuild
obj-m := hello_rust.o
# Makefile
KDIR ?= /lib/modules/$(shell uname -r)/build
default:
$(MAKE) LLVM=1 -C $(KDIR) M=$$PWD modules
clean:
$(MAKE) LLVM=1 -C $(KDIR) M=$$PWD clean
make KDIR=~/src/linux # tree built with CONFIG_RUST=y and the same toolchain
sudo insmod hello_rust.ko count=4
sudo rmmod hello_rust
The kernel must be built with CONFIG_RUST=y and the same rustc version as the module; the
.rmeta files of the kernel crate are not compatible across compiler versions.
4. Kernel crate essentials
| Userspace Rust | Kernel Rust | Notes |
|---|---|---|
Vec<T>, Box<T> |
KVec<T>, KBox<T>, VVec/KVVec |
Every allocation takes GFP_KERNEL/GFP_ATOMIC and returns Result |
println! |
pr_info!, pr_err!, dev_info!(dev, ...) |
pr_*! prefix the module name; end messages with \n |
std::io::Error |
kernel::error::Error (EINVAL, ENOMEM, ENOTTY, ...) |
Result<T> = Result<T, Error>; errors map to -errno for C |
&CStr from bytes |
c"name" literals |
Passed straight to C APIs |
std::sync::Mutex |
kernel::sync::Mutex, SpinLock, Arc |
Must be pinned (section 5) |
unwrap() |
? |
A panic is a kernel BUG; avoid unwrap/expect/indexing that can panic |
5. Pin-init: locks and registrations
Kernel locks, list heads and device registrations must not move after initialization. Rust
expresses this with Pin and the pin-init m
Установить rust-for-linux в Claude Code и Claude Desktop
Зарегайся, чтобы установить скилл
Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.
- Открой команду установки в одну строку
- Сохраняй скиллы в синхронизируемую библиотеку
- Уведомления, когда скиллы обновляются
Разрешённые инструменты
Инструменты, которые скиллу разрешено вызывать.
Без ограничений — скилл может использовать любой инструмент.
FAQ
Что делает скилл rust-for-linux?
Rust-for-Linux skill for writing Linux kernel modules and drivers in Rust. Use when setting up a kernel Rust toolchain, writing a module with the module! macro, using pin-init for locks and registrations, building out-of-tree Rust modules, writing misc or platform drivers in Rust, or testing kernel Rust code with KUnit. Activates on queries about Rust for Linux, CONFIG_RUST, kernel crate, module! macro, pin_init, try_pin_init!, KBox/KVec, rustavailable, or Rust kernel drivers.
Как установить скилл rust-for-linux?
Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.
Скилл rust-for-linux запускает скрипты?
Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.
Похожие скиллы
doc-coauthoring
Guide users through a structured workflow for co-authoring documentation. Use when user wants to write documentation, proposals, technical specs, decision…
от Anthropicacademy-guide
Stop and check this skill before finishing any reply to a question about how to use Claude or a Claude product — it recommends matching courses, tutorials…
от Anthropicinternal-comms
A set of resources to help me write all kinds of internal communications, using the formats that my company likes to use. Claude should use this skill…
от Anthropicskill-development
This skill should be used when the user wants to "create a skill", "add a skill to plugin", "write a new skill", "improve skill description", "organize skill…
от Anthropic