Linux Kernel Style logo

Linux Kernel Style

Community
happydog-intj
linux-kernel-style

Linux 内核编码风格 Skill。蒸馏自 Linux 内核源码、Documentation/process/coding-style.rst、 Linus Torvalds 的代码审查邮件(LKML)、Greg KH 的贡献者指南。 触发词:「Linux 内核风格」「像内核一样写 C」「kernel style」「写底层 C」。 适用:系统级 C 代码、驱动、嵌入式、底层库开发。

Overview

Publisherhappydog-intj
Repositoryluban-skill
Skill namelinux-kernel-style
Stars
32
Forks
3
Bundled files
Instructions only
Links
  • Markdown instructions

    A SKILL.md file the model loads on demand, so it only costs tokens when a request actually matches.

  • Works with any LLM

    AI skills are plain Markdown, not provider-specific code, so this works with GPT, Claude, Gemini, Grok, or a local model.

  • Self-contained

    Everything the model needs lives in the instructions — no extra files to sync.

  • Open source

    Published by happydog-intj on GitHub. Read the source before you install it.

Installation

Install the Linux Kernel Style AI skill in TypingMind to use it with any LLM, or drop it into another agent that reads SKILL.md.

1

Install in TypingMind

TypingMind installs a skill straight from its GitHub folder — it reads SKILL.md, bundles the resource files, and stores the result locally.

  1. Open the app and go to Plugins → Skills.
  2. Choose "Install from GitHub".
  3. Paste the skill folder URL below and confirm.
    https://github.com/happydog-intj/luban-skill/tree/master/skills/linux-kernel.skill
  4. Enable the skill in any chat where you want it available.
Plugins → Skills → Add skill → From GitHub URL, then paste the folder URL and press Continue.
2

Install in another agent

Any agent that reads the Agent Skills format can use this skill — copy the folder into that agent's skills directory.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/happydog-intj/luban-skill.git /tmp/luban-skill
mkdir -p .claude/skills
cp -r /tmp/luban-skill/skills/linux-kernel.skill .claude/skills/linux-kernel-style
Restart Claude Code after copying so it picks up the new skill.

Use it in TypingMind

Enable Linux Kernel Style in any TypingMind chat and the model takes it from there. Its name and description sit in the system prompt, and the moment a request matches, the model loads the full instructions itself — you never invoke it by hand, and it costs no tokens until it is actually used.

The model loads Linux Kernel Style on its own as soon as a request matches it.

Works with any AI model

AI skills are plain Markdown instructions rather than provider-specific code, so Linux Kernel Style is not tied to the model it was written for. Install it once in TypingMind and use it with GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama, or a local model you run yourself — all on your own API keys.

  • Loaded only when it is needed

    The system prompt carries just the name and description. The instructions are fetched on the first matching request, so an idle skill costs nothing.

  • Switch models mid-chat

    Because the skill is instructions rather than code, changing model does not break it — the next model reads the same SKILL.md.

Skill instructions

This is the SKILL.md content the model loads. Read it before installing — a skill is instructions your model will follow.

Linux Kernel · 编码 DNA

"God, I hate that style. I find it ugly as sin." — Linus,谈到他不喜欢的代码风格

角色定义

此 Skill 激活后,你写出的 C 代码应该让内核核心 maintainer 在 patch review 时点头, 而不是回复「Please follow the coding style guidelines.」

这意味着:代码不只是正确的,而是内核味道的。


命名 DNA

3 条直觉规则:

  1. 小写+下划线,永远 — 函数、变量、文件名全部 snake_case,没有例外

    c
    // ✅
    static int usb_device_init(struct usb_device *dev)
    // ❌
    static int usbDeviceInit(UsbDevice *dev)
  2. 名字要能自解释,但不啰嗦 — 局部变量可以短(i、ret、tmp), 但全局符号和函数名必须说清楚 是什么 + 属于哪个子系统

    c
    // ✅ 局部变量
    int i, ret;
    struct device *dev;
    // ✅ 全局函数
    int pci_enable_device(struct pci_dev *dev)
    // ❌ 过度缩写
    int pei_dev(struct pci_dev *d)
  3. 宏全大写,但不滥用宏 — 宏用 ALL_CAPS,避免用宏做函数能做的事

    c
    #define MAX_RETRIES 3            // ✅ 常量宏
    #define min(a, b) ((a) < (b) ? (a) : (b))  // ✅ 简单表达式
    // ❌ 宏替代函数(除非有充分的性能理由)

结构偏好

函数粒度:

  • 函数不超过 一屏(约 24 行),超了就是信号,需要拆
  • 每个函数只做一件事,名字能说清楚那件事
  • 函数参数不超过 5 个,否则考虑封结构体

缩进:

  • Tab,8 个空格宽 — 不是 4,就是 8。这是内核的宗教信仰
    c
    if (condition) {
    	do_something();  // 一个 tab
    	if (nested) {
    		do_nested();  // 两个 tab,天然逼你减少嵌套
    	}
    }
  • 8 个空格的 tab 让嵌套过深变得很丑 → 这是故意的,迫使你重构

错误处理:

  • 典型的 goto cleanup 模式,保持主线干净
    c
    int init_device(struct device *dev)
    {
    	int ret;
    
    	ret = alloc_buffer(dev);
    	if (ret)
    		goto err_alloc;
    
    	ret = register_irq(dev);
    	if (ret)
    		goto err_irq;
    
    	return 0;
    
    err_irq:
    	free_buffer(dev);
    err_alloc:
    	return ret;
    }
  • 返回值:成功返回 0,失败返回负的 errno(-EINVAL、-ENOMEM)

花括号:

  • 左括号跟着语句,不换行(K&R 风格)
    c
    if (x) {         // ✅
    	...
    }
    
    if (x)
    {                // ❌ 不是 Allman 风格
    	...
    }
  • 单行 if 不需要括号(但多行必须加)
    c
    if (condition)
    	return -EINVAL;  // ✅

注释哲学

核心原则:解释「为什么」,不解释「是什么」

c
/* ✅ 好注释:解释为什么这么做,而不是代码在做什么 */
/*
 * We need to disable interrupts here because the DMA controller
 * may fire an IRQ between the status check and the actual transfer,
 * causing a race condition on SMP systems.
 */
spin_lock_irqsave(&dev->lock, flags);

/* ❌ 废话注释 */
/* increment i by 1 */
i++;

注释格式:

  • 块注释用 /* */,对齐成矩形
  • 单行解释用 /* ... */(不用 //,内核 C89 时代遗留)
  • 函数/结构体注释用 /** */(kernel-doc 格式)
c
/**
 * usb_alloc_urb - allocate a URB for a USB driver
 * @iso_packets: number of iso packets for this URB
 * @mem_flags: allocation flags
 *
 * Returns a pointer to the newly allocated URB, or NULL on failure.
 */
struct urb *usb_alloc_urb(int iso_packets, gfp_t mem_flags)

反模式(绝不这样写)

  1. typedef 结构体 — 内核不 typedef 结构体,永远用 struct foo *

    c
    typedef struct { ... } MyStruct;  // ❌ 被 Linus 骂过无数次
    struct my_struct { ... };         // ✅
  2. 匈牙利命名法 — pDevice、iCount、bFlag 这种前缀污染

    c
    struct device *pDevice;  // ❌
    struct device *dev;      // ✅
  3. 超长函数 — 500 行函数是 bug,不是设计

  4. 神奇数字 — 直接写 0x40,不如定义 #define CTRL_REG_READY 0x40

  5. 三目运算符嵌套 — a ? b ? c : d : e 是在考验 reviewer 的耐心

  6. void * 到处传 — 能用具体类型就用具体类型

  7. printk 乱用 — 调试完记得删,pr_debug 可以被编译掉


校验测试

写完代码,问自己:

  1. Tab 检查:grep -P "^ " file.c 有输出吗?有就是用了空格缩进 → 不对
  2. 函数长度:每个函数 wc -l 超过 30 行吗?超了就是信号
  3. 错误路径检查:每个可能失败的调用都检查返回值了吗?

来源

  • Linux 内核源码:Documentation/process/coding-style.rst
  • Linus Torvalds LKML 邮件列表评审记录
  • Greg Kroah-Hartman 贡献者指南
  • Linux Kernel Teaching 项目

Frequently asked questions

What does the Linux Kernel Style AI skill do?

Linux 内核编码风格 Skill。蒸馏自 Linux 内核源码、Documentation/process/coding-style.rst、 Linus Torvalds 的代码审查邮件(LKML)、Greg KH 的贡献者指南。 触发词:「Linux 内核风格」「像内核一样写 C」「kernel style」「写底层 C」。 适用:系统级 C 代码、驱动、嵌入式、底层库开发。

Why use Linux Kernel Style on TypingMind?

Because you install it once and use it with any model. Linux Kernel Style is plain Markdown rather than provider-specific code, so the same skill runs on GPT-5, Claude, Gemini, Grok, or a local model — and you can switch model mid-chat without it breaking. TypingMind runs on your own API keys, so you pay providers directly instead of a per-seat subscription, and your skills and chats stay in your own storage.

How do I install Linux Kernel Style in TypingMind?

Open Plugins → Skills → Install from GitHub in TypingMind and paste https://github.com/happydog-intj/luban-skill/tree/master/skills/linux-kernel.skill. TypingMind reads its SKILL.md and installs it as a skill you can enable per chat.

Which AI models can use Linux Kernel Style?

Any model you connect in TypingMind. AI skills are plain Markdown instructions rather than provider-specific code, so GPT, Claude, Gemini, Grok, and local models can all load this skill when a request matches it.

How many AI models can I use with Linux Kernel Style?

As many as you like. As long as a model supports skills, you can use Linux Kernel Style with it — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama and more — all on TypingMind with your own API keys.

Is the Linux Kernel Style AI skill free?

It is published on GitHub by happydog-intj. Check the repository for licensing terms. You only pay your own AI provider for the tokens you use.

What are AI skills?

An AI skill is a reusable instruction bundle that teaches an AI model how to do one specific task. It follows the open Agent Skills format: a SKILL.md file with a name and description, plus any scripts, templates or reference files the model may need. The model reads the instructions only when your request matches the skill, so an installed skill costs nothing until it is used.

How are AI skills different from plugins or MCP servers?

A plugin or MCP server gives a model new tools to call — code that runs somewhere and returns a result. An AI skill gives the model knowledge and process instead: how to approach a task, which steps to follow, what good output looks like. Skills are plain Markdown, so they need no server, no API key and no runtime, and they work with any model.

View all

Set up your own AI workspace now

Get notified about new features and future giveaways by subscribing to our newsletter 👇