← 返回
AI智能
中文
Kernel
Avoid common Linux kernel mistakes — atomic context violations, allocation failures, and locking traps.
避免常见的Linux内核错误,包括原子上下文违规、内存分配失败和锁陷阱。
ivangdavila
AI智能
clawhub
v1.0.0 1 版本 100000 Key: 无需
#latest
概述
Atomic Context Traps
spin_lock held = cannot sleep — no kmalloc(GFP_KERNEL), no mutex_lock, no copy_from_user- Interrupt can take same spinlock — must use
spin_lock_irqsave, not plain spin_lock rcu_read_lock() section cannot sleep — no blocking calls inside RCU read-sidemight_sleep() annotation — add to functions that may sleep, catches bugs with CONFIG_DEBUG_ATOMIC_SLEEP
Allocation Failures
GFP_ATOMIC can return NULL — always check, don't assume successvmalloc memory not physically contiguous — cannot use for DMAkzalloc over kmalloc — uninitialized memory leaks kernel info to userspace- Allocation in loop risks OOM — preallocate or use memory pool
User Pointer Handling
copy_from_user returns bytes NOT copied — 0 means success, not failure- Never use
%s with user pointer in printk — kernel crash or info leak - User memory can change during syscall — copy to kernel buffer, validate the copy
__user annotation is documentation — doesn't enforce anything, you must use copy functions
Memory Ordering
READ_ONCE/WRITE_ONCE for lockless shared data — prevents compiler from caching/reordering- Spinlock release has implicit barrier — but check-then-act patterns still need care
smp_wmb() before publishing pointer — ensures data visible before pointer is
Module Error Paths
- Init fails midway — must undo everything already done
- Reverse order cleanup — unregister in opposite order of register
goto err_* pattern standard — cleaner than nested ifs- Check what's actually initialized — don't free/unregister what wasn't set up
Locking Mistakes
- Same lock acquired twice = deadlock — even in different functions
- Inconsistent lock ordering — document order, acquire in same sequence everywhere
mutex_trylock returns 1 on success — opposite of pthread_mutex_trylock- Reader-writer locks rarely worth it — contention overhead usually exceeds benefit
版本历史
共 1 个版本
-
v1.0.0
当前
2026-03-29 03:19 安全 安全
安全检测
腾讯云安全 (Sanbu)
安全,无风险
查看报告
🔗 相关推荐
ai-intelligence
halthelobster
将AI智能体从任务执行者升级为主动预判需求、持续优化的智能伙伴。集成WAL协议、工作缓冲区、自主定时任务及实战验证模式。Hal Stack核心组件 🦞
★ 834
📥 213,006
productivity
ivangdavila
创建、检查和编辑 Microsoft Word 文档及 DOCX 文件,支持样式、编号、修订记录、表格、分节符及兼容性检查等功能。
★ 438
📥 147,485
ai-intelligence
oswalpalash
类型化知识图谱,用于结构化智能体记忆与可组合技能。支持创建/查询实体(人员、项目、任务、事件、文档)及关联...
★ 711
📥 243,717