Skip to contentSkip to conversation

kernel/signal: Remove no longer required irqsave/restore

PATCH In review Sebastian Andrzej Siewior 6 participants 17 messages May 4, 2018 – Jun 7, 2018

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kernel/signal: Remove no longer required irqsave/restore

From: Anna-Maria Gleixner <[email protected]>

Commit a841796f11c9 ("signal: align __lock_task_sighand() irq disabling and RCU") introduced a rcu read side critical section with interrupts disabled. The changelog suggested that a better long-term fix would be "to make rt_mutex_unlock() disable irqs when acquiring the rt_mutex structure's ->wait_lock".

This long-term fix has been made in commit 4abf91047cf ("rtmutex: Make > wait_lock irq safe") for different reason.

Therefore revert commit a841796f11c9 ("signal: align > __lock_task_sighand() irq disabling and RCU") as the interrupt disable dance is not longer required.

---
 kernel/signal.c | 24 +++++++-----------------
 1 file changed, 7 insertions(+), 17 deletions(-)
kernel/signal.c +7 -17
index c6e4c83dc090..16b87c54d027 100644
@@ -1244,19 +1244,12 @@ struct sighand_struct *__lock_task_sighand(struct task_struct *tsk,
 {
 	struct sighand_struct *sighand;
 
+	rcu_read_lock();
 	for (;;) {
-		/*
-		 * Disable interrupts early to avoid deadlocks.
-		 * See rcu_read_unlock() comment header for details.
-		 */
-		local_irq_save(*flags);
-		rcu_read_lock();
 		sighand = rcu_dereference(tsk->sighand);
-		if (unlikely(sighand == NULL)) {
-			rcu_read_unlock();
-			local_irq_restore(*flags);
+		if (unlikely(sighand == NULL))
 			break;
-		}
+
 		/*
 		 * This sighand can be already freed and even reused, but
 		 * we rely on SLAB_TYPESAFE_BY_RCU and sighand_ctor() which
@@ -1268,15 +1261,12 @@ struct sighand_struct *__lock_task_sighand(struct task_struct *tsk,
 		 * __exit_signal(). In the latter case the next iteration
 		 * must see ->sighand == NULL.
 		 */
-		spin_lock(&sighand->siglock);
-		if (likely(sighand == tsk->sighand)) {
-			rcu_read_unlock();
+		spin_lock_irqsave(&sighand->siglock, *flags);
+		if (likely(sighand == tsk->sighand))
 			break;
-		}
-		spin_unlock(&sighand->siglock);
-		rcu_read_unlock();
-		local_irq_restore(*flags);
+		spin_unlock_irqrestore(&sighand->siglock, *flags);
 	}
+	rcu_read_unlock();
 
 	return sighand;
 }
Signature · 1 line
2.17.0
#1 lore raw
Quoted 10 lines · Sebastian Andrzej Siewior

From: Anna-Maria Gleixner <[email protected]>

Commit a841796f11c9 ("signal: align __lock_task_sighand() irq disabling and RCU") introduced a rcu read side critical section with interrupts disabled. The changelog suggested that a better long-term fix would be "to make rt_mutex_unlock() disable irqs when acquiring the rt_mutex structure's ->wait_lock".

This long-term fix has been made in commit 4abf91047cf ("rtmutex: Make > wait_lock irq safe") for different reason.

Which tree has this change been made in? I am not finding the commit you mention above in Linus's tree.

Quoted 3 lines

Therefore revert commit a841796f11c9 ("signal: align > __lock_task_sighand() irq disabling and RCU") as the interrupt disable dance is not longer required.

Which tree is change aimed at?

Eric

Quoted 53 lines
---
 kernel/signal.c | 24 +++++++-----------------
 1 file changed, 7 insertions(+), 17 deletions(-)
kernel/signal.c +7 -17
index c6e4c83dc090..16b87c54d027 100644
@@ -1244,19 +1244,12 @@ struct sighand_struct *__lock_task_sighand(struct task_struct *tsk,
 {
 	struct sighand_struct *sighand;
 
+	rcu_read_lock();
 	for (;;) {
-		/*
-		 * Disable interrupts early to avoid deadlocks.
-		 * See rcu_read_unlock() comment header for details.
-		 */
-		local_irq_save(*flags);
-		rcu_read_lock();
 		sighand = rcu_dereference(tsk->sighand);
-		if (unlikely(sighand == NULL)) {
-			rcu_read_unlock();
-			local_irq_restore(*flags);
+		if (unlikely(sighand == NULL))
 			break;
-		}
+
 		/*
 		 * This sighand can be already freed and even reused, but
 		 * we rely on SLAB_TYPESAFE_BY_RCU and sighand_ctor() which
@@ -1268,15 +1261,12 @@ struct sighand_struct *__lock_task_sighand(struct task_struct *tsk,
 		 * __exit_signal(). In the latter case the next iteration
 		 * must see ->sighand == NULL.
 		 */
-		spin_lock(&sighand->siglock);
-		if (likely(sighand == tsk->sighand)) {
-			rcu_read_unlock();
+		spin_lock_irqsave(&sighand->siglock, *flags);
+		if (likely(sighand == tsk->sighand))
 			break;
-		}
-		spin_unlock(&sighand->siglock);
-		rcu_read_unlock();
-		local_irq_restore(*flags);
+		spin_unlock_irqrestore(&sighand->siglock, *flags);
 	}
+	rcu_read_unlock();
 
 	return sighand;
 }
#2 lore raw
Quoted 2 lines · Eric W. Biederman
Quoted 1 line · Sebastian Andrzej Siewior

From: Anna-Maria Gleixner <[email protected]>

…

Quoted 5 lines
Quoted 2 lines

This long-term fix has been made in commit 4abf91047cf ("rtmutex: Make > wait_lock irq safe") for different reason.

Which tree has this change been made in? I am not finding the commit you mention above in Linus's tree.

I'm sorry, it should have been commit b4abf91047cf ("rtmutex: Make wait_lock irq safe").

Quoted 5 lines
Quoted 3 lines

Therefore revert commit a841796f11c9 ("signal: align > __lock_task_sighand() irq disabling and RCU") as the interrupt disable dance is not longer required.

Which tree is change aimed at?

tip -> Linus' tree

Quoted 1 line

Eric

Sebastian

#3 lore raw
Quoted 4 lines · Sebastian Andrzej Siewior
Quoted 2 lines · Eric W. Biederman
Quoted 1 line · Sebastian Andrzej Siewior

From: Anna-Maria Gleixner <[email protected]>

…

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

This long-term fix has been made in commit 4abf91047cf ("rtmutex: Make > wait_lock irq safe") for different reason.

Which tree has this change been made in? I am not finding the commit you mention above in Linus's tree.

I'm sorry, it should have been commit b4abf91047cf ("rtmutex: Make wait_lock irq safe").

Can you fix that in your patch description and can you also up the description of rcu_read_unlock?

If we don't need to jump through hoops it looks very reasonable to remove this unnecessary logic. But we should fix the description in rcu_read_unlock that still says we need these hoops.

Eric

#4 lore raw
Quoted 15 lines · Eric W. Biederman
Quoted 4 lines · Sebastian Andrzej Siewior
Quoted 2 lines · Eric W. Biederman
Quoted 1 line · Sebastian Andrzej Siewior

From: Anna-Maria Gleixner <[email protected]>

…

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

This long-term fix has been made in commit 4abf91047cf ("rtmutex: Make > wait_lock irq safe") for different reason.

Which tree has this change been made in? I am not finding the commit you mention above in Linus's tree.

I'm sorry, it should have been commit b4abf91047cf ("rtmutex: Make wait_lock irq safe").

Quoted 6 lines

Can you fix that in your patch description and can you also up the description of rcu_read_unlock?

If we don't need to jump through hoops it looks very reasonable to remove this unnecessary logic. But we should fix the description in rcu_read_unlock that still says we need these hoops.

The hoops are still required for rcu_read_lock(), otherwise you get deadlocks between the scheduler and RCU in PREEMPT=y kernels. What happens with this patch (if I understand it correctly) is that the signal code now uses a different way of jumping through the hoops. But the hoops are still jumped through.

Thanx, Paul

#5 lore raw
Quoted 23 lines · Paul E. McKenney
Quoted 15 lines · Eric W. Biederman
Quoted 4 lines · Sebastian Andrzej Siewior
Quoted 2 lines · Eric W. Biederman
Quoted 1 line · Sebastian Andrzej Siewior

From: Anna-Maria Gleixner <[email protected]>

…

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

This long-term fix has been made in commit 4abf91047cf ("rtmutex: Make > wait_lock irq safe") for different reason.

Which tree has this change been made in? I am not finding the commit you mention above in Linus's tree.

I'm sorry, it should have been commit b4abf91047cf ("rtmutex: Make wait_lock irq safe").

Quoted 6 lines

Can you fix that in your patch description and can you also up the description of rcu_read_unlock?

If we don't need to jump through hoops it looks very reasonable to remove this unnecessary logic. But we should fix the description in rcu_read_unlock that still says we need these hoops.

Quoted 5 lines

The hoops are still required for rcu_read_lock(), otherwise you get deadlocks between the scheduler and RCU in PREEMPT=y kernels. What happens with this patch (if I understand it correctly) is that the signal code now uses a different way of jumping through the hoops. But the hoops are still jumped through.

The patch changes:

local_irq_disable();
rcu_read_lock();
spin_lock();
rcu_read_unlock();

to:

rcu_read_lock();
spin_lock_irq();
rcu_read_unlock();

Now that I have a chance to relfect on it the fact that the patern that is being restored does not work is scary. As the failure has nothing to do with lock ordering and people won't realize what is going on. Especially since the common rcu modes won't care.

So is it true that taking spin_lock_irq before calling rcu_read_unlock is a problem because of rt_mutex_unlock()? Or has b4abf91047cf ("rtmutex: Make wait_lock irq safe") actually fixed that and we can correct the documentation of rcu_read_unlock() ? And fix __lock_task_sighand?

Eric

#6 lore raw
Quoted 49 lines · Eric W. Biederman
Quoted 23 lines · Paul E. McKenney
Quoted 15 lines · Eric W. Biederman
Quoted 4 lines · Sebastian Andrzej Siewior
Quoted 2 lines · Eric W. Biederman
Quoted 1 line · Sebastian Andrzej Siewior

From: Anna-Maria Gleixner <[email protected]>

…

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

This long-term fix has been made in commit 4abf91047cf ("rtmutex: Make > wait_lock irq safe") for different reason.

Which tree has this change been made in? I am not finding the commit you mention above in Linus's tree.

I'm sorry, it should have been commit b4abf91047cf ("rtmutex: Make wait_lock irq safe").

Quoted 6 lines

Can you fix that in your patch description and can you also up the description of rcu_read_unlock?

If we don't need to jump through hoops it looks very reasonable to remove this unnecessary logic. But we should fix the description in rcu_read_unlock that still says we need these hoops.

Quoted 5 lines

The hoops are still required for rcu_read_lock(), otherwise you get deadlocks between the scheduler and RCU in PREEMPT=y kernels. What happens with this patch (if I understand it correctly) is that the signal code now uses a different way of jumping through the hoops. But the hoops are still jumped through.

The patch changes:

local_irq_disable();
rcu_read_lock();
spin_lock();
rcu_read_unlock();

to:

rcu_read_lock();
spin_lock_irq();
rcu_read_unlock();

Now that I have a chance to relfect on it the fact that the patern that is being restored does not work is scary. As the failure has nothing to do with lock ordering and people won't realize what is going on. Especially since the common rcu modes won't care.

Quoted 4 lines

So is it true that taking spin_lock_irq before calling rcu_read_unlock is a problem because of rt_mutex_unlock()? Or has b4abf91047cf ("rtmutex: Make wait_lock irq safe") actually fixed that and we can correct the documentation of rcu_read_unlock() ? And fix __lock_task_sighand?

The problem is that the thing taking the lock might be the scheduler, or one of the locks taken while the scheduler's pi and rq locks are held. This occurs only with RCU-preempt.

Here is what can happen:

o A task does rcu_read_lock().

o That task is preempted.

o That task stays preempted for a long time, and is therefore
priority boosted. This boosting involves a high-priority RCU kthread creating an rt_mutex, pretending that the preempted task already holds it, and then acquiring it.

o The task awakens, acquires the scheduler's rq lock, and
then does rcu_read_unlock().

o Because the task has been priority boosted, __rcu_read_unlock()
invokes the rcu_read_unlock_special() slowpath, which does (as you say) rt_mutex_unlock() to deboost. The deboosting can cause the scheduler to acquire the rq and pi locks, which results in deadlock.

In contrast, holding these scheduler locks across the entirety of the RCU-preempt read-side critical section is harmless because then the critical section cannot be preempted, which means that priority boosting cannot happen, which means that there will be no need to deboost at rcu_read_unlock() time.

This restriction has not changed, and as far as I can see is inherent in the fact that RCU uses the scheduler and the scheduler uses RCU. There is going to be an odd corner case in there somewhere!

Thanx, Paul

#7 lore raw
Quoted 85 lines · Paul E. McKenney
Quoted 49 lines · Eric W. Biederman
Quoted 23 lines · Paul E. McKenney
Quoted 15 lines · Eric W. Biederman
Quoted 4 lines · Sebastian Andrzej Siewior
Quoted 2 lines · Eric W. Biederman
Quoted 1 line · Sebastian Andrzej Siewior

From: Anna-Maria Gleixner <[email protected]>

…

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

This long-term fix has been made in commit 4abf91047cf ("rtmutex: Make > wait_lock irq safe") for different reason.

Which tree has this change been made in? I am not finding the commit you mention above in Linus's tree.

I'm sorry, it should have been commit b4abf91047cf ("rtmutex: Make wait_lock irq safe").

Quoted 6 lines

Can you fix that in your patch description and can you also up the description of rcu_read_unlock?

If we don't need to jump through hoops it looks very reasonable to remove this unnecessary logic. But we should fix the description in rcu_read_unlock that still says we need these hoops.

Quoted 5 lines

The hoops are still required for rcu_read_lock(), otherwise you get deadlocks between the scheduler and RCU in PREEMPT=y kernels. What happens with this patch (if I understand it correctly) is that the signal code now uses a different way of jumping through the hoops. But the hoops are still jumped through.

The patch changes:

local_irq_disable();
rcu_read_lock();
spin_lock();
rcu_read_unlock();

to:

rcu_read_lock();
spin_lock_irq();
rcu_read_unlock();

Now that I have a chance to relfect on it the fact that the patern that is being restored does not work is scary. As the failure has nothing to do with lock ordering and people won't realize what is going on. Especially since the common rcu modes won't care.

Quoted 4 lines

So is it true that taking spin_lock_irq before calling rcu_read_unlock is a problem because of rt_mutex_unlock()? Or has b4abf91047cf ("rtmutex: Make wait_lock irq safe") actually fixed that and we can correct the documentation of rcu_read_unlock() ? And fix __lock_task_sighand?

The problem is that the thing taking the lock might be the scheduler, or one of the locks taken while the scheduler's pi and rq locks are held. This occurs only with RCU-preempt.

Here is what can happen:

o A task does rcu_read_lock().

o That task is preempted.

o That task stays preempted for a long time, and is therefore
priority boosted. This boosting involves a high-priority RCU kthread creating an rt_mutex, pretending that the preempted task already holds it, and then acquiring it.

o The task awakens, acquires the scheduler's rq lock, and
then does rcu_read_unlock().

o Because the task has been priority boosted, __rcu_read_unlock()
invokes the rcu_read_unlock_special() slowpath, which does (as you say) rt_mutex_unlock() to deboost. The deboosting can cause the scheduler to acquire the rq and pi locks, which results in deadlock.

In contrast, holding these scheduler locks across the entirety of the RCU-preempt read-side critical section is harmless because then the critical section cannot be preempted, which means that priority boosting cannot happen, which means that there will be no need to deboost at rcu_read_unlock() time.

Quoted 3 lines

This restriction has not changed, and as far as I can see is inherent in the fact that RCU uses the scheduler and the scheduler uses RCU. There is going to be an odd corner case in there somewhere!

However if I read things correctly b4abf91047cf ("rtmutex: Make wait_lock irq safe") did change this.

In particular it changed things so that it is only the scheduler locks that matter, not any old lock that disabled interrupts. This was done by disabling disabling interrupts when taking the wait_lock.

The rcu_read_unlock documentation states:

* In most situations, rcu_read_unlock() is immune from deadlock.
* However, in kernels built with CONFIG_RCU_BOOST, rcu_read_unlock()
* is responsible for deboosting, which it does via rt_mutex_unlock().
* Unfortunately, this function acquires the scheduler's runqueue and
* priority-inheritance spinlocks. This means that deadlock could result
* if the caller of rcu_read_unlock() already holds one of these locks or
* any lock that is ever acquired while holding them; or any lock which
* can be taken from interrupt context because rcu_boost()->rt_mutex_lock()
* does not disable irqs while taking ->wait_lock.

So we can now remove the clause:
* ; or any lock which
* can be taken from interrupt context because rcu_boost()->rt_mutex_lock()
* does not disable irqs while taking ->wait_lock.

Without the any lock that disabled interrupts restriction it is now safe to not worry about the issues with the scheduler locks and the rt_mutex Which does make it safe to not worry about these crazy complexities in lock_task_sighand.

Paul do you agree or is the patch unsafe?

Eric

#8 lore raw
Quoted 115 lines · Eric W. Biederman
Quoted 85 lines · Paul E. McKenney
Quoted 49 lines · Eric W. Biederman
Quoted 23 lines · Paul E. McKenney
Quoted 15 lines · Eric W. Biederman
Quoted 4 lines · Sebastian Andrzej Siewior
Quoted 2 lines · Eric W. Biederman
Quoted 1 line · Sebastian Andrzej Siewior

From: Anna-Maria Gleixner <[email protected]>

…

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

This long-term fix has been made in commit 4abf91047cf ("rtmutex: Make > wait_lock irq safe") for different reason.

Which tree has this change been made in? I am not finding the commit you mention above in Linus's tree.

I'm sorry, it should have been commit b4abf91047cf ("rtmutex: Make wait_lock irq safe").

Quoted 6 lines

Can you fix that in your patch description and can you also up the description of rcu_read_unlock?

If we don't need to jump through hoops it looks very reasonable to remove this unnecessary logic. But we should fix the description in rcu_read_unlock that still says we need these hoops.

Quoted 5 lines

The hoops are still required for rcu_read_lock(), otherwise you get deadlocks between the scheduler and RCU in PREEMPT=y kernels. What happens with this patch (if I understand it correctly) is that the signal code now uses a different way of jumping through the hoops. But the hoops are still jumped through.

The patch changes:

local_irq_disable();
rcu_read_lock();
spin_lock();
rcu_read_unlock();

to:

rcu_read_lock();
spin_lock_irq();
rcu_read_unlock();

Now that I have a chance to relfect on it the fact that the patern that is being restored does not work is scary. As the failure has nothing to do with lock ordering and people won't realize what is going on. Especially since the common rcu modes won't care.

Quoted 4 lines

So is it true that taking spin_lock_irq before calling rcu_read_unlock is a problem because of rt_mutex_unlock()? Or has b4abf91047cf ("rtmutex: Make wait_lock irq safe") actually fixed that and we can correct the documentation of rcu_read_unlock() ? And fix __lock_task_sighand?

The problem is that the thing taking the lock might be the scheduler, or one of the locks taken while the scheduler's pi and rq locks are held. This occurs only with RCU-preempt.

Here is what can happen:

o A task does rcu_read_lock().

o That task is preempted.

o That task stays preempted for a long time, and is therefore
priority boosted. This boosting involves a high-priority RCU kthread creating an rt_mutex, pretending that the preempted task already holds it, and then acquiring it.

o The task awakens, acquires the scheduler's rq lock, and
then does rcu_read_unlock().

o Because the task has been priority boosted, __rcu_read_unlock()
invokes the rcu_read_unlock_special() slowpath, which does (as you say) rt_mutex_unlock() to deboost. The deboosting can cause the scheduler to acquire the rq and pi locks, which results in deadlock.

In contrast, holding these scheduler locks across the entirety of the RCU-preempt read-side critical section is harmless because then the critical section cannot be preempted, which means that priority boosting cannot happen, which means that there will be no need to deboost at rcu_read_unlock() time.

Quoted 3 lines

This restriction has not changed, and as far as I can see is inherent in the fact that RCU uses the scheduler and the scheduler uses RCU. There is going to be an odd corner case in there somewhere!

However if I read things correctly b4abf91047cf ("rtmutex: Make wait_lock irq safe") did change this.

In particular it changed things so that it is only the scheduler locks that matter, not any old lock that disabled interrupts. This was done by disabling disabling interrupts when taking the wait_lock.

The rcu_read_unlock documentation states:

* In most situations, rcu_read_unlock() is immune from deadlock.
* However, in kernels built with CONFIG_RCU_BOOST, rcu_read_unlock()
* is responsible for deboosting, which it does via rt_mutex_unlock().
* Unfortunately, this function acquires the scheduler's runqueue and
* priority-inheritance spinlocks. This means that deadlock could result
* if the caller of rcu_read_unlock() already holds one of these locks or
* any lock that is ever acquired while holding them; or any lock which
* can be taken from interrupt context because rcu_boost()->rt_mutex_lock()
* does not disable irqs while taking ->wait_lock.

So we can now remove the clause:
* ; or any lock which
* can be taken from interrupt context because rcu_boost()->rt_mutex_lock()
* does not disable irqs while taking ->wait_lock.

Quoted 6 lines

Without the any lock that disabled interrupts restriction it is now safe to not worry about the issues with the scheduler locks and the rt_mutex Which does make it safe to not worry about these crazy complexities in lock_task_sighand.

Paul do you agree or is the patch unsafe?

Ah, I thought you were trying to get rid of all but the first line of that paragraph, not just the final clause. Apologies for my confusion!

It looks plausible, but the patch should be stress-tested on a preemptible kernel with priority boosting enabled. Has that been done?

(Me, I would run rcutorture scenario TREE03 for an extended time period on b4abf91047cf with your patch applied. But what testing have you done already?)

Thanx, Paul

#9 lore raw
Quoted 126 lines · Paul E. McKenney
Quoted 115 lines · Eric W. Biederman
Quoted 85 lines · Paul E. McKenney
Quoted 49 lines · Eric W. Biederman
Quoted 23 lines · Paul E. McKenney
Quoted 15 lines · Eric W. Biederman
Quoted 4 lines · Sebastian Andrzej Siewior
Quoted 2 lines · Eric W. Biederman
Quoted 1 line · Sebastian Andrzej Siewior

From: Anna-Maria Gleixner <[email protected]>

…

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

This long-term fix has been made in commit 4abf91047cf ("rtmutex: Make > wait_lock irq safe") for different reason.

Which tree has this change been made in? I am not finding the commit you mention above in Linus's tree.

I'm sorry, it should have been commit b4abf91047cf ("rtmutex: Make wait_lock irq safe").

Quoted 6 lines

Can you fix that in your patch description and can you also up the description of rcu_read_unlock?

If we don't need to jump through hoops it looks very reasonable to remove this unnecessary logic. But we should fix the description in rcu_read_unlock that still says we need these hoops.

Quoted 5 lines

The hoops are still required for rcu_read_lock(), otherwise you get deadlocks between the scheduler and RCU in PREEMPT=y kernels. What happens with this patch (if I understand it correctly) is that the signal code now uses a different way of jumping through the hoops. But the hoops are still jumped through.

The patch changes:

local_irq_disable();
rcu_read_lock();
spin_lock();
rcu_read_unlock();

to:

rcu_read_lock();
spin_lock_irq();
rcu_read_unlock();

Now that I have a chance to relfect on it the fact that the patern that is being restored does not work is scary. As the failure has nothing to do with lock ordering and people won't realize what is going on. Especially since the common rcu modes won't care.

Quoted 4 lines

So is it true that taking spin_lock_irq before calling rcu_read_unlock is a problem because of rt_mutex_unlock()? Or has b4abf91047cf ("rtmutex: Make wait_lock irq safe") actually fixed that and we can correct the documentation of rcu_read_unlock() ? And fix __lock_task_sighand?

The problem is that the thing taking the lock might be the scheduler, or one of the locks taken while the scheduler's pi and rq locks are held. This occurs only with RCU-preempt.

Here is what can happen:

o A task does rcu_read_lock().

o That task is preempted.

o That task stays preempted for a long time, and is therefore
priority boosted. This boosting involves a high-priority RCU kthread creating an rt_mutex, pretending that the preempted task already holds it, and then acquiring it.

o The task awakens, acquires the scheduler's rq lock, and
then does rcu_read_unlock().

o Because the task has been priority boosted, __rcu_read_unlock()
invokes the rcu_read_unlock_special() slowpath, which does (as you say) rt_mutex_unlock() to deboost. The deboosting can cause the scheduler to acquire the rq and pi locks, which results in deadlock.

In contrast, holding these scheduler locks across the entirety of the RCU-preempt read-side critical section is harmless because then the critical section cannot be preempted, which means that priority boosting cannot happen, which means that there will be no need to deboost at rcu_read_unlock() time.

Quoted 3 lines

This restriction has not changed, and as far as I can see is inherent in the fact that RCU uses the scheduler and the scheduler uses RCU. There is going to be an odd corner case in there somewhere!

However if I read things correctly b4abf91047cf ("rtmutex: Make wait_lock irq safe") did change this.

In particular it changed things so that it is only the scheduler locks that matter, not any old lock that disabled interrupts. This was done by disabling disabling interrupts when taking the wait_lock.

The rcu_read_unlock documentation states:

* In most situations, rcu_read_unlock() is immune from deadlock.
* However, in kernels built with CONFIG_RCU_BOOST, rcu_read_unlock()
* is responsible for deboosting, which it does via rt_mutex_unlock().
* Unfortunately, this function acquires the scheduler's runqueue and
* priority-inheritance spinlocks. This means that deadlock could result
* if the caller of rcu_read_unlock() already holds one of these locks or
* any lock that is ever acquired while holding them; or any lock which
* can be taken from interrupt context because rcu_boost()->rt_mutex_lock()
* does not disable irqs while taking ->wait_lock.

So we can now remove the clause:
* ; or any lock which
* can be taken from interrupt context because rcu_boost()->rt_mutex_lock()
* does not disable irqs while taking ->wait_lock.

Quoted 6 lines

Without the any lock that disabled interrupts restriction it is now safe to not worry about the issues with the scheduler locks and the rt_mutex Which does make it safe to not worry about these crazy complexities in lock_task_sighand.

Paul do you agree or is the patch unsafe?

Ah, I thought you were trying to get rid of all but the first line of that paragraph, not just the final clause. Apologies for my confusion!

Quoted 6 lines

It looks plausible, but the patch should be stress-tested on a preemptible kernel with priority boosting enabled. Has that been done?

(Me, I would run rcutorture scenario TREE03 for an extended time period on b4abf91047cf with your patch applied. But what testing have you done already?)

Not my patch. I was just the reviewer asking for some obvious cleanups. So people won't get confused. Sebastian? Can you tell Paul what testing you have done?

Eric

#10 lore raw
Quoted 125 lines · Eric W. Biederman
Quoted 123 lines · Paul E. McKenney
Quoted 121 lines · Eric W. Biederman
Quoted 85 lines · Paul E. McKenney
Quoted 49 lines · Eric W. Biederman
Quoted 23 lines · Paul E. McKenney
Quoted 15 lines · Eric W. Biederman
Quoted 4 lines · Sebastian Andrzej Siewior
Quoted 2 lines · Eric W. Biederman
Quoted 1 line · Sebastian Andrzej Siewior

From: Anna-Maria Gleixner <[email protected]>

…

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

This long-term fix has been made in commit 4abf91047cf ("rtmutex: Make > wait_lock irq safe") for different reason.

Which tree has this change been made in? I am not finding the commit you mention above in Linus's tree.

I'm sorry, it should have been commit b4abf91047cf ("rtmutex: Make wait_lock irq safe").

Quoted 6 lines

Can you fix that in your patch description and can you also up the description of rcu_read_unlock?

If we don't need to jump through hoops it looks very reasonable to remove this unnecessary logic. But we should fix the description in rcu_read_unlock that still says we need these hoops.

Quoted 5 lines

The hoops are still required for rcu_read_lock(), otherwise you get deadlocks between the scheduler and RCU in PREEMPT=y kernels. What happens with this patch (if I understand it correctly) is that the signal code now uses a different way of jumping through the hoops. But the hoops are still jumped through.

The patch changes:

local_irq_disable();
rcu_read_lock();
spin_lock();
rcu_read_unlock();

to:

rcu_read_lock();
spin_lock_irq();
rcu_read_unlock();

Now that I have a chance to relfect on it the fact that the patern that is being restored does not work is scary. As the failure has nothing to do with lock ordering and people won't realize what is going on. Especially since the common rcu modes won't care.

Quoted 4 lines

So is it true that taking spin_lock_irq before calling rcu_read_unlock is a problem because of rt_mutex_unlock()? Or has b4abf91047cf ("rtmutex: Make wait_lock irq safe") actually fixed that and we can correct the documentation of rcu_read_unlock() ? And fix __lock_task_sighand?

The problem is that the thing taking the lock might be the scheduler, or one of the locks taken while the scheduler's pi and rq locks are held. This occurs only with RCU-preempt.

Here is what can happen:

o A task does rcu_read_lock().

o That task is preempted.

o That task stays preempted for a long time, and is therefore
priority boosted. This boosting involves a high-priority RCU kthread creating an rt_mutex, pretending that the preempted task already holds it, and then acquiring it.

o The task awakens, acquires the scheduler's rq lock, and
then does rcu_read_unlock().

o Because the task has been priority boosted, __rcu_read_unlock()
invokes the rcu_read_unlock_special() slowpath, which does (as you say) rt_mutex_unlock() to deboost. The deboosting can cause the scheduler to acquire the rq and pi locks, which results in deadlock.

In contrast, holding these scheduler locks across the entirety of the RCU-preempt read-side critical section is harmless because then the critical section cannot be preempted, which means that priority boosting cannot happen, which means that there will be no need to deboost at rcu_read_unlock() time.

Quoted 3 lines

This restriction has not changed, and as far as I can see is inherent in the fact that RCU uses the scheduler and the scheduler uses RCU. There is going to be an odd corner case in there somewhere!

However if I read things correctly b4abf91047cf ("rtmutex: Make wait_lock irq safe") did change this.

In particular it changed things so that it is only the scheduler locks that matter, not any old lock that disabled interrupts. This was done by disabling disabling interrupts when taking the wait_lock.

The rcu_read_unlock documentation states:

* In most situations, rcu_read_unlock() is immune from deadlock.
* However, in kernels built with CONFIG_RCU_BOOST, rcu_read_unlock()
* is responsible for deboosting, which it does via rt_mutex_unlock().
* Unfortunately, this function acquires the scheduler's runqueue and
* priority-inheritance spinlocks. This means that deadlock could result
* if the caller of rcu_read_unlock() already holds one of these locks or
* any lock that is ever acquired while holding them; or any lock which
* can be taken from interrupt context because rcu_boost()->rt_mutex_lock()
* does not disable irqs while taking ->wait_lock.

So we can now remove the clause:
* ; or any lock which
* can be taken from interrupt context because rcu_boost()->rt_mutex_lock()
* does not disable irqs while taking ->wait_lock.

Without the any lock that disabled interrupts restriction it is now safe to not worry about the issues with the scheduler locks and the rt_mutex Which does make it safe to not worry about these crazy complexities in lock_task_sighand.

Paul do you agree or is the patch unsafe?

Quoted 8 lines
Quoted 8 lines

Ah, I thought you were trying to get rid of all but the first line of that paragraph, not just the final clause. Apologies for my confusion!

It looks plausible, but the patch should be stress-tested on a preemptible kernel with priority boosting enabled. Has that been done?

(Me, I would run rcutorture scenario TREE03 for an extended time period on b4abf91047cf with your patch applied.

And with lockdep enabled, which TREE03 does not do by default.

Quoted 6 lines
Quoted 2 lines

But what testing have you
done already?)

Not my patch. I was just the reviewer asking for some obvious cleanups. So people won't get confused. Sebastian? Can you tell Paul what testing you have done?

Ah, apologies for my confusion!

Thanx, Paul

#11 lore raw
Quoted 5 lines · Paul E. McKenney
Quoted 2 lines · Eric W. Biederman
Quoted 2 lines

(Me, I would run rcutorture scenario TREE03 for an extended time period on b4abf91047cf with your patch applied.

And with lockdep enabled, which TREE03 does not do by default.

Will run that again to make sure.

Thanks,

tglx

#12 lore raw
Quoted 9 lines · Thomas Gleixner
Quoted 5 lines · Paul E. McKenney
Quoted 2 lines · Eric W. Biederman
Quoted 2 lines

(Me, I would run rcutorture scenario TREE03 for an extended time period on b4abf91047cf with your patch applied.

And with lockdep enabled, which TREE03 does not do by default.

Will run that again to make sure.

I ran the rcutorture scenario TREE03 for 4 hours with the above described setup. It was successful and without any lockdep splats.

Anna-Maria

#13 lore raw
Quoted 7 lines · Anna-Maria Gleixner
Quoted 5 lines · Thomas Gleixner
Quoted 4 lines · Paul E. McKenney
Quoted 2 lines · Eric W. Biederman
Quoted 2 lines

(Me, I would run rcutorture scenario TREE03 for an extended time period on b4abf91047cf with your patch applied.

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

And with lockdep enabled, which TREE03 does not do by default.

Will run that again to make sure.

I ran the rcutorture scenario TREE03 for 4 hours with the above described setup. It was successful and without any lockdep splats.

Thank you for the testing, Anna-Maria! If you give them a Tested-by, I will give them an ack. ;-)

Thanx, Paul

#14 lore raw
Quoted 10 lines · Paul E. McKenney
Quoted 9 lines · Anna-Maria Gleixner
Quoted 7 lines · Thomas Gleixner
Quoted 5 lines · Paul E. McKenney
Quoted 2 lines · Eric W. Biederman
Quoted 2 lines

(Me, I would run rcutorture scenario TREE03 for an extended time period on b4abf91047cf with your patch applied.

And with lockdep enabled, which TREE03 does not do by default.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Will run that again to make sure.

I ran the rcutorture scenario TREE03 for 4 hours with the above described setup. It was successful and without any lockdep splats.

Thank you for the testing, Anna-Maria! If you give them a Tested-by, I will give them an ack. ;-)

If it is ok to give a Tested-by to the patch I wrote, I will do this to get your ack :)

#15 lore raw
Quoted 17 lines · Anna-Maria Gleixner
Quoted 15 lines · Paul E. McKenney
Quoted 13 lines · Anna-Maria Gleixner
Quoted 8 lines · Thomas Gleixner
Quoted 5 lines · Paul E. McKenney
Quoted 2 lines · Eric W. Biederman
Quoted 2 lines

(Me, I would run rcutorture scenario TREE03 for an extended time period on b4abf91047cf with your patch applied.

And with lockdep enabled, which TREE03 does not do by default.

Will run that again to make sure.

I ran the rcutorture scenario TREE03 for 4 hours with the above described setup. It was successful and without any lockdep splats.

Quoted 8 lines
Quoted 3 lines

Thank you for the testing, Anna-Maria! If you give them a Tested-by, I will give them an ack. ;-)

If it is ok to give a Tested-by to the patch I wrote, I will do this to get your ack :)

It wasn't even all that early in the morning for me, was it? ;-) Anyway:

I am sure that you have already considered adding your test results to the commit log as an alternative to applying a Tested-by to your own patch.

Thanx, Paul

#16 lore raw

[tip:core/urgent] signal: Remove no longer required irqsave/restore

In reply to Sebastian Andrzej Siewior
signal: Remove no longer required irqsave/restore
Commit-ID:  e79e0f38083e607da5d7b493e7a0f78ba38d788e
Gitweb:     https://git.kernel.org/tip/e79e0f38083e607da5d7b493e7a0f78ba38d788e
Author:     Anna-Maria Gleixner <[email protected]>
AuthorDate: Fri, 4 May 2018 16:40:14 +0200
Committer:  Thomas Gleixner <[email protected]>
CommitDate: Thu, 7 Jun 2018 22:18:55 +0200

signal: Remove no longer required irqsave/restore

Commit a841796f11c9 ("signal: align __lock_task_sighand() irq disabling and RCU") introduced a rcu read side critical section with interrupts disabled. The changelog suggested that a better long-term fix would be "to make rt_mutex_unlock() disable irqs when acquiring the rt_mutex structure's ->wait_lock".

This long-term fix has been made in commit 4abf91047cf ("rtmutex: Make > wait_lock irq safe") for different reason.

Therefore revert commit a841796f11c9 ("signal: align > __lock_task_sighand() irq disabling and RCU") as the interrupt disable dance is not longer required.

Testing was done over an extensive period with RCU torture, especially with TREE03 as requested by Paul.

---
 kernel/signal.c | 24 +++++++-----------------
 1 file changed, 7 insertions(+), 17 deletions(-)
kernel/signal.c +7 -17
index 0f865d67415d..8d8a940422a8 100644
@@ -1244,19 +1244,12 @@ struct sighand_struct *__lock_task_sighand(struct task_struct *tsk,
 {
 	struct sighand_struct *sighand;
 
+	rcu_read_lock();
 	for (;;) {
-		/*
-		 * Disable interrupts early to avoid deadlocks.
-		 * See rcu_read_unlock() comment header for details.
-		 */
-		local_irq_save(*flags);
-		rcu_read_lock();
 		sighand = rcu_dereference(tsk->sighand);
-		if (unlikely(sighand == NULL)) {
-			rcu_read_unlock();
-			local_irq_restore(*flags);
+		if (unlikely(sighand == NULL))
 			break;
-		}
+
 		/*
 		 * This sighand can be already freed and even reused, but
 		 * we rely on SLAB_TYPESAFE_BY_RCU and sighand_ctor() which
@@ -1268,15 +1261,12 @@ struct sighand_struct *__lock_task_sighand(struct task_struct *tsk,
 		 * __exit_signal(). In the latter case the next iteration
 		 * must see ->sighand == NULL.
 		 */
-		spin_lock(&sighand->siglock);
-		if (likely(sighand == tsk->sighand)) {
-			rcu_read_unlock();
+		spin_lock_irqsave(&sighand->siglock, *flags);
+		if (likely(sighand == tsk->sighand))
 			break;
-		}
-		spin_unlock(&sighand->siglock);
-		rcu_read_unlock();
-		local_irq_restore(*flags);
+		spin_unlock_irqrestore(&sighand->siglock, *flags);
 	}
+	rcu_read_unlock();
 
 	return sighand;
 }