Linux kernel 关于linux内核中的信号量up()和mutex_unlock()

Linux kernel 关于linux内核中的信号量up()和mutex_unlock(),linux-kernel,mutex,semaphore,Linux Kernel,Mutex,Semaphore,我想知道为什么我们可以在中断上下文中使用信号量up(),而在中断上下文中不能使用相同的互斥变量mutex_unlock()。 下面是来自内核的代码片段 /** * mutex_unlock - release the mutex * @lock: the mutex to be released * * Unlock a mutex that has been locked by this task previously. * * This function must not be used i

我想知道为什么我们可以在中断上下文中使用信号量up(),而在中断上下文中不能使用相同的互斥变量mutex_unlock()。 下面是来自内核的代码片段

/**
* mutex_unlock - release the mutex
* @lock: the mutex to be released
*
* Unlock a mutex that has been locked by this task previously.
*
* This function must not be used in interrupt context. Unlocking
* of a not locked mutex is not allowed.
*
* This function is similar to (but not equivalent to) up().
*/
void __sched mutex_unlock(struct mutex *lock)
{
  #ifndef CONFIG_DEBUG_LOCK_ALLOC
  if (__mutex_unlock_fast(lock))
    return;
#endif
__mutex_unlock_slowpath(lock, _RET_IP_);
}EXPORT_SYMBOL(mutex_unlock);


 /**
 * up - release the semaphore
* @sem: the semaphore to release
*
* Release the semaphore.  Unlike mutexes, up() may be called from any
* context and even by tasks which have never called down().
*/
void up(struct semaphore *sem)
{
  unsigned long flags;

  raw_spin_lock_irqsave(&sem->lock, flags);
  if (likely(list_empty(&sem->wait_list)))
    sem->count++;
  else
     __up(sem);
  raw_spin_unlock_irqrestore(&sem->lock, flags);
}
EXPORT_SYMBOL(up);

mutex\u unlock
需要一个-例如
raw\u spin\u lock
如果设置了
CONFIG\u PREEMPT\u RT
,可能会休眠-因此如果时间正确,你的
mutex\u unlock
在IRQ上下文中会死锁。

如果设置了CONFIG\u PREEMPT\u RT,raw\u spin\u lock可能会休眠
-如果我记得正确,函数
spin\u lock
只是不禁用中断,禁止从IRQ调用它(因为死锁,您是对的)。这里的配置
CONFIG\u PREEMPT\u RT
与此无关。
mutex\u unlock
通常不安全。我只提到一个例子。