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

Commit 8057ad31 authored by Prateek Sood's avatar Prateek Sood
Browse files

osq_lock: avoid live-lock issue for RT task



Live Lock  due to task spinning while unqueue of CPU osq_node
from optimistic_spin_queue. Task T1 had decremented mutex count to
acquire the lock on CPU0. Before setting owner it got preempted. On
CPU1 task T2 acquired osq_lock and started spinning on owner of mutex
with preemption disabled. CPU1 runq has one task, so need_resched will
not be set. On CPU0 task T3 tried to acquire osq_lock to spin on the
same mutex. At this time following scenario causes soft lockup:

After preemption of task T1, RT task T3 tried to acquire the same
mutex. It will start spinning on the osq_lock until the lock is available
or need_resched is set. For RT task, need_resched will not be set. Task T3
will not be able to bail out of the infinite loop.

Change-Id: Icd599bdcebda23b22dcd5e6f9be8842bb8f2f5b6
Signed-off-by: default avatarPrateek Sood <prsood@codeaurora.org>
parent f67a2861
Loading
Loading
Loading
Loading
+8 −1
Original line number Diff line number Diff line
#include <linux/percpu.h>
#include <linux/sched.h>
#include "mcs_spinlock.h"
#include <linux/sched/rt.h>

#ifdef CONFIG_SMP

@@ -87,6 +88,7 @@ bool osq_lock(struct optimistic_spin_queue *lock)
{
	struct optimistic_spin_node *node = this_cpu_ptr(&osq_node);
	struct optimistic_spin_node *prev, *next;
	struct task_struct *task = current;
	int curr = encode_cpu(smp_processor_id());
	int old;

@@ -114,8 +116,13 @@ bool osq_lock(struct optimistic_spin_queue *lock)
	while (!smp_load_acquire(&node->locked)) {
		/*
		 * If we need to reschedule bail... so we can block.
		 * If a task spins on owner on a CPU after acquiring
		 * osq_lock while a RT task spins on another CPU  to
		 * acquire osq_lock, it will starve the owner from
		 * completing if owner is to be scheduled on the same CPU.
		 * It will be a live lock.
		 */
		if (need_resched())
		if (need_resched() || rt_task(task))
			goto unqueue;

		cpu_relax_lowlatency();