Linux 锁 (2) - mutex 锁

文章目录

  • [1. 前言](#1. 前言)
  • [2. mutex 的实现](#2. mutex 的实现)
    • [2.1 mutex 锁的初始化](#2.1 mutex 锁的初始化)
    • [2.2 上锁和下锁](#2.2 上锁和下锁)
    • [2.3 小结](#2.3 小结)

1. 前言

2. mutex 的实现

mutex 锁基于 spinlock 实现:mutex 锁内置的 spinlock 用来保护 mutex 锁数据结构的并发访问,而其它数据结构主要用来管理占锁的进程信息、以及等待锁的进程信息。

mutex 锁有如下主要特点:

  • 任何时候,只会有一个进程持有 mutex 锁
  • 只有锁的持有者才能释放 mutex 锁
  • mutex 锁的重复 unlock 操作的不允许的
  • mutex 锁不允许递归使用
  • mutex 锁必须通过系统提供的 API 进行初始化
  • 持有 mutex 锁的进程无法退出
  • mutex 锁不能用于 软、硬中断上下文,如 tasklet 和 timer 回调中(因为它可能导致睡眠)

看一下 mutex 锁的数据结构:

c 复制代码
// include/linux/mutex.h

struct mutex {
	/*
	 * - 低 3-bit 用于 MUTEX_FLAGS, 快速路径拿锁置为 0, 慢速路径置为其它非 0 值;
	 * - 其余 bit 存储当前拿锁进程的 task_struct 指针.
	 * 0 值表示锁空闲.
	 */
	atomic_long_t		owner;
	spinlock_t		wait_lock; /* 自旋锁 用来保护 mutex 锁自身的数据结构访问 */
#ifdef CONFIG_MUTEX_SPIN_ON_OWNER
	struct optimistic_spin_queue osq; /* Spinner MCS lock */
#endif
	struct list_head	wait_list; /* 等待锁进程列表 */
	// 其它调试用途数据
	...
};

2.1 mutex 锁的初始化

初始化 API 主要有 DEFINE_MUTEX()mutex_init()

c 复制代码
#define __MUTEX_INITIALIZER(lockname) \
		{ .owner = ATOMIC_LONG_INIT(0) \
		, .wait_lock = __SPIN_LOCK_UNLOCKED(lockname.wait_lock) \
		, .wait_list = LIST_HEAD_INIT(lockname.wait_list) \
		__DEBUG_MUTEX_INITIALIZER(lockname) \
		__DEP_MAP_MUTEX_INITIALIZER(lockname) }

#define DEFINE_MUTEX(mutexname) \
	struct mutex mutexname = __MUTEX_INITIALIZER(mutexname)
c 复制代码
#define mutex_init(mutex)						\
do {									\
	static struct lock_class_key __key;				\
									\
	__mutex_init((mutex), #mutex, &__key);				\
} while (0)

void
__mutex_init(struct mutex *lock, const char *name, struct lock_class_key *key)
{
	atomic_long_set(&lock->owner, 0);
	spin_lock_init(&lock->wait_lock);
	INIT_LIST_HEAD(&lock->wait_list);
#ifdef CONFIG_MUTEX_SPIN_ON_OWNER
	osq_lock_init(&lock->osq);
#endif

	...
}
EXPORT_SYMBOL(__mutex_init);

2.2 上锁和下锁

mutex_lock()上锁的主要 API:

c 复制代码
void __sched mutex_lock(struct mutex *lock)
{
	might_sleep(); /* 不能用在不能睡眠的上下文 */

	if (!__mutex_trylock_fast(lock)) /* 快速取锁路径 */
		__mutex_lock_slowpath(lock); /* 慢速取锁路径 */
}

static __always_inline bool __mutex_trylock_fast(struct mutex *lock)
{
	unsigned long curr = (unsigned long)current;

	/*
	 * lock->owner == 0 表示锁空闲, 则取锁成功, 将 lock->owner = curr.
	 * 此快速路径 lock->owner 低 3 位 flgas 值设为 0, 这意味着锁当前
	 * 没有竞争者, 如果这个状态(lock->owner 低 3 位 flgas 值为 0) 一直
	 * 维持到锁释放的快速路径调用 __mutex_unlock_fast(), 则所释放时不需
	 * 做唤醒操作, 因为锁释放时没有等待进程。
	 */
	if (!atomic_long_cmpxchg_acquire(&lock->owner, 0UL, curr))
		return true;

	return false;
}

static noinline void __sched
__mutex_lock_slowpath(struct mutex *lock)
{
	__mutex_lock(lock, TASK_UNINTERRUPTIBLE, 0, NULL, _RET_IP_);
}

static int __sched
__mutex_lock(struct mutex *lock, long state, unsigned int subclass,
	     struct lockdep_map *nest_lock, unsigned long ip)
{
	return __mutex_lock_common(lock, state, subclass, nest_lock, ip, NULL, false);
}
c 复制代码
// kernel/locking/mutex.c

/* mutex 锁等待者 */
struct mutex_waiter {
	struct list_head	list; /* 等待时 用于挂接到 mutex 锁等待队列 mutex::wait_list */
	struct task_struct	*task; /* 等待的进程 */
	struct ww_acquire_ctx	*ww_ctx;
	...
};

/*
 * 返回锁当前旧的持有者.
 * 返回 NULL 表示锁当前空闲, 同时 锁持有者 已更新为 当前进程.
 */
static inline struct task_struct *__mutex_trylock_or_owner(struct mutex *lock)
{
	unsigned long owner, curr = (unsigned long)current;

	owner = atomic_long_read(&lock->owner);
	for (;;) { /* must loop, can race against a flag */
		unsigned long old, flags = __owner_flags(owner);
		unsigned long task = owner & ~MUTEX_FLAGS;

		if (task) { /* 锁当前被 @task 持有 */
			if (likely(task != curr)) /* 当前进程 不是 锁持有者 */
				break;

			if (likely(!(flags & MUTEX_FLAG_PICKUP)))
				break;

			flags &= ~MUTEX_FLAG_PICKUP;
		} else { /* 锁当前空闲 */
#ifdef CONFIG_DEBUG_MUTEXES
			DEBUG_LOCKS_WARN_ON(flags & MUTEX_FLAG_PICKUP);
#endif
		}

		/*
		 * We set the HANDOFF bit, we must make sure it doesn't live
		 * past the point where we acquire it. This would be possible
		 * if we (accidentally) set the bit on an unlocked mutex.
		 */
		flags &= ~MUTEX_FLAG_HANDOFF;

		old = atomic_long_cmpxchg_acquire(&lock->owner, owner, curr | flags);
		if (old == owner) /* 拿锁成功 */
			return NULL; /* 返回 */

		owner = old;
	}

	return __owner_task(owner);
}

/* 返回 1 表示(当前进程)拿锁成功, 否则拿锁失败 */
static inline bool __mutex_trylock(struct mutex *lock)
{
	return !__mutex_trylock_or_owner(lock);
}

/*
 * Lock a mutex (possibly interruptible), slowpath:
 */
static __always_inline int __sched
__mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,
		    struct lockdep_map *nest_lock, unsigned long ip,
		    struct ww_acquire_ctx *ww_ctx, const bool use_ww_ctx)
{
	struct mutex_waiter waiter; /* 锁等待者 */
	bool first = false;
	struct ww_mutex *ww;
	int ret;

	might_sleep();

	...

	preempt_disable(); /* (1) 禁用抢占 +1 => 1 */
	...

	if (__mutex_trylock(lock) ||
	    ...) {
		// 尝试拿锁成功,返回
		/* got the lock, yay! */
		...
		preempt_enable(); /* 重启抢占: -1 => 0 */
		return 0;
	}

	spin_lock(&lock->wait_lock); /* (2) 先拿到抢锁的资格, 即保护锁自身的数据结构 (禁用抢占 +1 => 2) */
	/*
	 * After waiting to acquire the wait_lock, try again.
	 */
	if (__mutex_trylock(lock)) { /* 再次尝试拿锁,返回 1 表示(当前进程)拿锁成功, */
		...
		goto skip_wait; /* 拿锁成功, 无需进入等待 */
	}

	...

	if (!use_ww_ctx) {
		/* add waiting tasks to the end of the waitqueue (FIFO): */
		list_add_tail(&waiter.list, &lock->wait_list); /* 加入锁的等待队列尾部, 即 FIFO */

#ifdef CONFIG_DEBUG_MUTEXES
		waiter.ww_ctx = MUTEX_POISON_WW_CTX;
#endif
	} else {
		...
	}

	waiter.task = current; /* 等待者进程 */

	if (__mutex_waiter_is_first(lock, &waiter))
		/* 有等待进程, 锁不再能从快速路径释放: 释放时要从慢速路径唤醒等待进程 */
		__mutex_set_flag(lock, MUTEX_FLAG_WAITERS);

	/* 更新等待进程态: TASK_UNINTERRUPTIBLE, TASK_KILLABLE, TASK_INTERRUPTIBLE, ...*/
	set_current_state(state);
	for (;;) {
		/*
		 * Once we hold wait_lock, we're serialized against
		 * mutex_unlock() handing the lock off to us, do a trylock
		 * before testing the error conditions to make sure we pick up
		 * the handoff.
		 */
		if (__mutex_trylock(lock)) /* 再次尝试拿锁: 返回 1 表示(当前进程)拿锁成功, */
			goto acquired;

		/*
		 * Check for signals and wound conditions while holding
		 * wait_lock. This ensures the lock cancellation is ordered
		 * against mutex_unlock() and wake-ups do not go missing.
		 */
		/* 有挂起的信号, 不(继续)睡眠, 转而先去处理信号 (拿锁失败: EINTR) */
		if (unlikely(signal_pending_state(state, current))) {
			ret = -EINTR;
			goto err;
		}

		...

		spin_unlock(&lock->wait_lock); /* 平衡 (2) 处的抢占计数: -1 => 1 */
		/*
		 * (3) 发起调度, 进入睡眠等锁, 直到被唤醒 
		 *     (这里会先将抢占计数 -1 => 0, 调度回来后再将抢占计数 +1 => 1) 
		 */
		schedule_preempt_disabled();

		...

		set_current_state(state);

		/*
		 * Here we order against unlock; we must either see it change
		 * state back to RUNNING and fall through the next schedule(),
		 * or we must see its unlock and acquire.
		 */
		/* 尝试拿锁, 此处抢占计数为 1 */
		if (__mutex_trylock(lock) ||
		    (first && mutex_optimistic_spin(lock, ww_ctx, use_ww_ctx, &waiter)))
			break;

		spin_lock(&lock->wait_lock); /* (4) 再次去拿抢锁的资格 (禁用抢占 +1 => 2) */
	}
	/* 拿锁成功 */
	spin_lock(&lock->wait_lock); /* 此处抢占计数 +1 => 2 */
acquired:
	__set_current_state(TASK_RUNNING);

	mutex_remove_waiter(lock, &waiter, current);
	if (likely(list_empty(&lock->wait_list)))
		__mutex_clear_flag(lock, MUTEX_FLAGS);

	...

skip_wait:
	...

	spin_unlock(&lock->wait_lock); /* 抢占计数 -1 => 1 */
	preempt_enable(); /* 平衡抢占计数: 抢占计数 -1 => 0 */
	return 0;

	/* 有信号待处理(EINTR) 或 出错 */
err:
	__set_current_state(TASK_RUNNING);
	mutex_remove_waiter(lock, &waiter, current);
err_early_backoff:
	spin_unlock(&lock->wait_lock);
	debug_mutex_free_waiter(&waiter);
	mutex_release(&lock->dep_map, 1, ip);
	preempt_enable();
	return ret;
}

拿锁的主要逻辑是:如果锁当前空闲,拿锁后(标记 mutex::owner 为当前进程)直接返回,否则进入睡眠等待,在锁持有者释放锁 (unlock) 被唤醒,继续尝试拿锁,如果成功则返回,否则进入睡眠继续等待被唤醒后再次尝试拿锁,一直到拿锁成功为止

拿锁过程中,需要特别注意的是,如果走的是快速路径,即当前锁处于空闲状态,则进程直接成为锁的持有者,即 mutex::owner = 进程 task_struct 指针值,注意此时 mutex::owner 的低 3 为的 flags 值为 0,这将在 unlock 逻辑中起到关键作用。

下锁的主要 API 接口是 mutex_unlock()

c 复制代码
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_); /* 慢速路径 */
}

static __always_inline bool __mutex_unlock_fast(struct mutex *lock)
{
	unsigned long curr = (unsigned long)current;

	/*
	 * 这里如果 lock->owner == curr 成立,表明是从快速路径拿的锁(即 __mutex_trylock_fast()), 
	 * 因为只有快速路径拿锁时将 lock->owner 低 3 位 flgas 值设为 0。lock->owner 低 3 位 
	 * flgas 值设为 0, 这告诉我们没有锁的竞争者,也就没有必要做唤醒操作,释放锁(lock->owner 
	 * 赋 0)后直接返回即可。
	 */
	if (atomic_long_cmpxchg_release(&lock->owner, curr, 0UL) == curr)
		return true;

	return false;
}
c 复制代码
/*
 * Release the lock, slowpath:
 */
static noinline void __sched __mutex_unlock_slowpath(struct mutex *lock, unsigned long ip)
{
	struct task_struct *next = NULL;
	DEFINE_WAKE_Q(wake_q);
	unsigned long owner;

	...

	/*
	 * Release the lock before (potentially) taking the spinlock such that
	 * other contenders can get on with things ASAP.
	 *
	 * Except when HANDOFF, in that case we must not clear the owner field,
	 * but instead set it to the top waiter.
	 */
	owner = atomic_long_read(&lock->owner);
	for (;;) {
		unsigned long old;

		...

		if (owner & MUTEX_FLAG_HANDOFF)
			break;

		old = atomic_long_cmpxchg_release(&lock->owner, owner,
						  __owner_flags(owner));
		if (old == owner) {
			if (owner & MUTEX_FLAG_WAITERS)
				break;

			return;
		}

		owner = old;
	}

	spin_lock(&lock->wait_lock);
	...
	if (!list_empty(&lock->wait_list)) { /* 有进程在等待锁的释放 */
		/* get the first entry from the wait-list: */
		struct mutex_waiter *waiter =
			list_first_entry(&lock->wait_list,
					 struct mutex_waiter, list);  /* 取等待首进程, 按等待先后的顺序被唤醒(FIFO) */

		next = waiter->task;

		debug_mutex_wake_waiter(lock, waiter);
		wake_q_add(&wake_q, next);
	}

	if (owner & MUTEX_FLAG_HANDOFF)
		__mutex_handoff(lock, next);

	spin_unlock(&lock->wait_lock);

	wake_up_q(&wake_q); /* 唤醒等待进程(如果存在的话) */
}

2.3 小结

mutex 用在可以睡眠的上下文,mutex 锁的竞争规律是:

  • 如果当前锁空闲,任何锁的竞争者有可能获取到锁;
  • 如果锁当前被占有,那锁的等待者进程,按尝试拿锁的先后顺序被唤醒(也即 FIFIO),且一次只唤醒一个等待进程。
相关推荐
zhoupenghui1682 天前
golang 锁实现原理与解析&锁机制(sync)种类与举例说明以及其使用场景
开发语言·后端·golang·mutex·wait·lock·sync
獭.獭.4 个月前
Linux -- 线程互斥
linux·互斥锁·mutex·互斥量·线程互斥
ItJavawfc10 个月前
驱动-互斥锁
互斥锁·mutex·驱动·驱动互斥锁·linux互斥锁
Golinie1 年前
【Go万字洗髓经】Golang中sync.Mutex的单机锁:实现原理与底层源码
golang·并发·mutex··sync.mutex
arong_xu1 年前
现代C++锁介绍
c++·多线程·mutex
许野平1 年前
Rust:AtomicI8 还是 Mutex<u8>?
开发语言·后端·rust·mutex·atomic
PegasusYu1 年前
STM32CUBEIDE FreeRTOS操作教程(五):mutex互斥信号量
stm32·mutex·rtos·信号量·stm32cubeide·free-rtos·互斥信号量
橘色的喵2 年前
C++编程:生产者-消费者模型中条件变量的使用问题及优化方案
mutex·cv·死锁·生产者消费者·pub-sub·资源竞争·notify_node
a187927218312 年前
Go-知识并发控制mutex
golang·go·互斥锁·mutex·独占锁·go 并发管理