mutex_lock在不同编译选项下的一个坑

include/linux/mutex.h中声明了函数:

144 #ifdef CONFIG_DEBUG_LOCK_ALLOC
145 extern void mutex_lock_nested(struct mutex *lock, unsigned int subclass);
...
152 #define mutex_lock(lock) mutex_lock_nested(lock, 0)
...
162 #else
163 extern void mutex_lock(struct mutex *lock);
...
171 #endif

开启CONFIG_DEBUG_LOCK_ALLOC编译选项时,mutex_lock被define成了mutex_lock_nested
kernel/mutex.c中分别实现了这两个函数:

58  #ifndef CONFIG_DEBUG_LOCK_ALLOC
...
89  void __sched mutex_lock(struct mutex *lock)
...
100 EXPORT_SYMBOL(mutex_lock);
101 #endif


282 #ifdef CONFIG_DEBUG_LOCK_ALLOC
283 void __sched
284 mutex_lock_nested(struct mutex *lock, unsigned int subclass)
285 {
286     might_sleep();
287     __mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, subclass, NULL, _RET_IP_);
288 }
289
290 EXPORT_SYMBOL_GPL(mutex_lock_nested);
...
318 #endif

注意天坑出现!mutex_lock_nested是被GPL导出的,因此当开启了CONFIG_DEBUG_LOCK_ALLOC的编译选项时,非GPL的模块就不能使用mutex_lock了。

你可能感兴趣的:(linux,code)