๐ค
Kernel
Avoid common Linux kernel mistakes โ atomic context violations, allocation failures, and locking traps.
ๅฎๅ
จ้่ฟ
ๆ่ฝ่ฏดๆ
name: Kernel description: Avoid common Linux kernel mistakes โ atomic context violations, allocation failures, and locking traps. metadata: {"clawdbot":{"emoji":"๐ง","os":["linux"]}}
Atomic Context Traps
spin_lockheld = cannot sleep โ nokmalloc(GFP_KERNEL), nomutex_lock, nocopy_from_user- Interrupt can take same spinlock โ must use
spin_lock_irqsave, not plainspin_lock rcu_read_lock()section cannot sleep โ no blocking calls inside RCU read-sidemight_sleep()annotation โ add to functions that may sleep, catches bugs withCONFIG_DEBUG_ATOMIC_SLEEP
Allocation Failures
GFP_ATOMICcan return NULL โ always check, don't assume successvmallocmemory not physically contiguous โ cannot use for DMAkzallocoverkmallocโ uninitialized memory leaks kernel info to userspace- Allocation in loop risks OOM โ preallocate or use memory pool
User Pointer Handling
copy_from_userreturns bytes NOT copied โ 0 means success, not failure- Never use
%swith user pointer in printk โ kernel crash or info leak - User memory can change during syscall โ copy to kernel buffer, validate the copy
__userannotation is documentation โ doesn't enforce anything, you must use copy functions
Memory Ordering
READ_ONCE/WRITE_ONCEfor 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_trylockreturns 1 on success โ opposite ofpthread_mutex_trylock- Reader-writer locks rarely worth it โ contention overhead usually exceeds benefit
ๅฆไฝไฝฟ็จใKernelใ๏ผ
- ๆๅผๅฐ้พ่พAI๏ผWeb ๆ iOS App๏ผ
- ็นๅปไธๆนใ็ซๅณไฝฟ็จใๆ้ฎ๏ผๆๅจๅฏน่ฏๆกไธญ่พๅ ฅไปปๅกๆ่ฟฐ
- ๅฐ้พ่พAI ไผ่ชๅจๅน้ ๅนถ่ฐ็จใKernelใๆ่ฝๅฎๆไปปๅก
- ็ปๆๅณๆถๅ็ฐ๏ผๆฏๆ็ปง็ปญๅฏน่ฏไผๅ