Linux kernel.skill

From happydog-intj/luban-skill · View interactive version

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

How to use this skill

Skill file

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)

```

  1. 名字要能自解释,但不啰嗦 — 局部变量可以短(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)

```

  1. 宏全大写,但不滥用宏 — 宏用 ALL_CAPS,避免用宏做函数能做的事

```c

#define MAX_RETRIES 3 // ✅ 常量宏

#define min(a, b) ((a) < (b) ? (a) : (b)) // ✅ 简单表达式

// ❌ 宏替代函数(除非有充分的性能理由)

```


结构偏好

函数粒度:

缩进:

```c

if (condition) {

do_something(); // 一个 tab

if (nested) {

do_nested(); // 两个 tab,天然逼你减少嵌套

}

}

```

错误处理:

```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;

}

```

花括号:

```c

if (x) { // ✅

...

}

if (x)

{ // ❌ 不是 Allman 风格

...

}

```

```c

if (condition)

return -EINVAL; // ✅

```


注释哲学

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

/* ✅ 好注释:解释为什么这么做,而不是代码在做什么 */
/*
 * 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++;

注释格式:

/**
 * 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 { ... }; // ✅

```

  1. 匈牙利命名法 — pDevice、iCount、bFlag 这种前缀污染

```c

struct device *pDevice; // ❌

struct device *dev; // ✅

```

  1. 超长函数 — 500 行函数是 bug,不是设计
  2. 神奇数字 — 直接写 0x40,不如定义 #define CTRL_REG_READY 0x40
  3. 三目运算符嵌套 — a ? b ? c : d : e 是在考验 reviewer 的耐心
  4. void * 到处传 — 能用具体类型就用具体类型
  5. printk 乱用 — 调试完记得删,pr_debug 可以被编译掉

校验测试

写完代码,问自己:

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

来源

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.