Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b41f4d4a authored by Sami Tolvanen's avatar Sami Tolvanen
Browse files

ANDROID: scs: fix recursive spinlock in scs_check_usage



Use cmpxchg instead of a spinlock in scs_check_usage() to avoid
deadlocks.

Bug: 157781894
Change-Id: I1701ccaf25fdbd34ce4798c6f93e220b1565fb34
Signed-off-by: default avatarSami Tolvanen <samitolvanen@google.com>
parent 3045070c
Loading
Loading
Loading
Loading
+17 −22
Original line number Diff line number Diff line
@@ -185,36 +185,31 @@ int scs_prepare(struct task_struct *tsk, int node)
}

#ifdef CONFIG_DEBUG_STACK_USAGE
static inline unsigned long scs_used(struct task_struct *tsk)
static void scs_check_usage(struct task_struct *tsk)
{
	static unsigned long highest;

	unsigned long *p = __scs_base(tsk);
	unsigned long *end = scs_magic(p);
	unsigned long s = (unsigned long)p;
	unsigned long prev, curr = highest, used = 0;

	while (p < end && READ_ONCE_NOCHECK(*p))
		p++;

	return (unsigned long)p - s;
	for (; p < end; ++p) {
		if (!READ_ONCE_NOCHECK(*p))
			break;
		used += sizeof(*p);
	}

static void scs_check_usage(struct task_struct *tsk)
{
	static DEFINE_SPINLOCK(lock);
	static unsigned long highest;
	unsigned long used = scs_used(tsk);

	if (used <= highest)
		return;
	while (used > curr) {
		prev = cmpxchg_relaxed(&highest, curr, used);

	spin_lock(&lock);

	if (used > highest) {
		if (prev == curr) {
			pr_info("%s (%d): highest shadow stack usage: %lu bytes\n",
				tsk->comm, task_pid_nr(tsk), used);
		highest = used;
			break;
		}

	spin_unlock(&lock);
		curr = prev;
	}
}
#else
static inline void scs_check_usage(struct task_struct *tsk)