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

Commit e467b8c7 authored by Sami Tolvanen's avatar Sami Tolvanen Committed by Will Deacon
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
(cherry picked from commit e929fb3f34326c669bff7863da32c90730fa7d72)
Signed-off-by: default avatarSami Tolvanen <samitolvanen@google.com>
parent f829d7f0
Loading
Loading
Loading
Loading
+17 −22
Original line number Original line Diff line number Diff line
@@ -185,36 +185,31 @@ int scs_prepare(struct task_struct *tsk, int node)
}
}


#ifdef CONFIG_DEBUG_STACK_USAGE
#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 *p = __scs_base(tsk);
	unsigned long *end = scs_magic(p);
	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))
	for (; p < end; ++p) {
		p++;
		if (!READ_ONCE_NOCHECK(*p))

			break;
	return (unsigned long)p - s;
		used += sizeof(*p);
	}
	}


static void scs_check_usage(struct task_struct *tsk)
	while (used > curr) {
{
		prev = cmpxchg_relaxed(&highest, curr, used);
	static DEFINE_SPINLOCK(lock);
	static unsigned long highest;
	unsigned long used = scs_used(tsk);

	if (used <= highest)
		return;


	spin_lock(&lock);
		if (prev == curr) {

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


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