Skip to contentSkip to conversation

sched: Reenable interrupts in do sched_yield()

PATCH Applied Thomas Gleixner 4 participants 8 messages Oct 20, 2020 – Dec 23, 2020 Scheduler

lore
Share & tools
lore
#0 lore raw

do_sched_yield() invokes schedule() with interrupts disabled which is not allowed. This goes back to the pre git era to commit a6efb709806c ("[PATCH] irqlock patch 2.5.27-H6") in the history tree.

Reenable interrupts and remove the misleading comment which "explains" it.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
---
 kernel/sched/core.c |    6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
kernel/sched/core.c +1 -5
@@ -6094,12 +6094,8 @@ static void do_sched_yield(void)
 	schedstat_inc(rq->yld_count);
 	current->sched_class->yield_task(rq);
 
-	/*
-	 * Since we are going to call schedule() anyway, there's
-	 * no need to preempt or enable interrupts:
-	 */
 	preempt_disable();
-	rq_unlock(rq, &rf);
+	rq_unlock_irq(rq, &rf);
 	sched_preempt_enable_no_resched();
 
 	schedule();
#1 lore raw

kernel/sched/core.c static void do_sched_yield(void) · line 6099

Quoted 3 lines · On Tue, 20 Oct 2020 16:46:55 +0200 Thomas Gleixner <[email protected]> wrote:
kernel/sched/core.c +0 -3
-	/*
-	 * Since we are going to call schedule() anyway, there's
-	 * no need to preempt or enable interrupts:

I think the above comment still makes sense, just needs to be tweeked:

/*
* Since we are going to call schedule() anyway, there's
* no need to allow preemption after releasing the rq lock.

kernel/sched/core.c static void do_sched_yield(void) · line 6100

Quoted 1 line
kernel/sched/core.c +0 -1
-	 */

Especially, since we are now enabling interrupts, which is likely to trigger a preemption.

-- Steve

Quoted 6 lines
kernel/sched/core.c +1 -1
 	preempt_disable();
-	rq_unlock(rq, &rf);
+	rq_unlock_irq(rq, &rf);
 	sched_preempt_enable_no_resched();
 
 	schedule();
#2 lore raw
Quoted 9 lines · Steven Rostedt

kernel/sched/core.c static void do_sched_yield(void) · line 6099

Quoted 3 lines · On Tue, 20 Oct 2020 16:46:55 +0200 Thomas Gleixner <[email protected]> wrote:
kernel/sched/core.c +0 -3
-	/*
-	 * Since we are going to call schedule() anyway, there's
-	 * no need to preempt or enable interrupts:

I think the above comment still makes sense, just needs to be tweeked:

Quoted 7 lines

/*
* Since we are going to call schedule() anyway, there's
* no need to allow preemption after releasing the rq lock.

kernel/sched/core.c static void do_sched_yield(void) · line 6100

Quoted 1 line
kernel/sched/core.c +0 -1
-	 */

Especially, since we are now enabling interrupts, which is likely to trigger a preemption.

sched_preempt_enable_no_resched() still enables preemption. It just avoids the check. And it still allows preemption when an interrupt triggering preemption happens between sched_preempt_enable_no_resched() and __schedule() disabling preemption/interrupts.

So no, your new variant is just differently bogus and misleading.

Thanks,

tglx

#3 lore raw
Quoted 18 lines · On Tue, 20 Oct 2020 20:02:55 +0200 Thomas Gleixner <[email protected]> wrote:
Quoted 9 lines · Steven Rostedt

kernel/sched/core.c static void do_sched_yield(void) · line 6099

Quoted 3 lines · On Tue, 20 Oct 2020 16:46:55 +0200 Thomas Gleixner <[email protected]> wrote:
kernel/sched/core.c +0 -3
-	/*
-	 * Since we are going to call schedule() anyway, there's
-	 * no need to preempt or enable interrupts:  

I think the above comment still makes sense, just needs to be tweeked:

Quoted 7 lines

/*
* Since we are going to call schedule() anyway, there's
* no need to allow preemption after releasing the rq lock.

kernel/sched/core.c static void do_sched_yield(void) · line 6100

Quoted 1 line
kernel/sched/core.c +0 -1
-	 */  

Especially, since we are now enabling interrupts, which is likely to trigger a preemption.

Quoted 6 lines

sched_preempt_enable_no_resched() still enables preemption. It just avoids the check. And it still allows preemption when an interrupt triggering preemption happens between sched_preempt_enable_no_resched() and __schedule() disabling preemption/interrupts.

So no, your new variant is just differently bogus and misleading.

What I wrote wasn't exactly what I meant. What I meant to have:

	/*
	 * Since we are going to call schedule() anyways, there's
	 * no need to do the preemption check when the rq_lock is released.
	 */

That is, to document why we have the preempt_disable() before the unlock:

	preempt_disable();
	rq_unlock_irq(rq, &rf);
	sched_preempt_enable_no_resched();

-- Steve

#4 lore raw
Quoted 10 lines · Steven Rostedt

On Tue, 20 Oct 2020 20:02:55 +0200
Thomas Gleixner <[email protected]> wrote:
What I wrote wasn't exactly what I meant. What I meant to have:

	/*
	 * Since we are going to call schedule() anyways, there's
	 * no need to do the preemption check when the rq_lock is released.
	 */

That is, to document why we have the preempt_disable() before the unlock:

which is pretty obvious, but I let Peter decide on that.

#5 lore raw
Quoted 5 lines · On Wed, 21 Oct 2020 09:27:22 +0200 Thomas Gleixner <[email protected]> wrote:
Quoted 4 lines · Steven Rostedt

On Tue, 20 Oct 2020 20:02:55 +0200
Thomas Gleixner <[email protected]> wrote:
What I wrote wasn't exactly what I meant. What I meant to have:

Quoted 8 lines
Quoted 6 lines
	/*
	 * Since we are going to call schedule() anyways, there's
	 * no need to do the preemption check when the rq_lock is released.
	 */

That is, to document why we have the preempt_disable() before the unlock:

which is pretty obvious, but I let Peter decide on that.

To us maybe, but I like to have comments that explain why things are done to average people. ;-)

If I go to another kernel developer outside the core kernel, would they know why there's a preempt_disable() there?

 	preempt_disable();
	rq_unlock_irq(rq, &rf);

sched_preempt_enable_no_resched();

schedule();

Not everyone knows that the rq_unlock_irq() would trigger a schedule if an interrupt happened as soon as irqs were enabled again and need_resched was set.

-- Steve

#6 lore raw
Quoted 31 lines · Steven Rostedt
Quoted 5 lines · On Wed, 21 Oct 2020 09:27:22 +0200 Thomas Gleixner <[email protected]> wrote:
Quoted 4 lines · Steven Rostedt

On Tue, 20 Oct 2020 20:02:55 +0200
Thomas Gleixner <[email protected]> wrote:
What I wrote wasn't exactly what I meant. What I meant to have:

Quoted 8 lines
Quoted 6 lines
	/*
	 * Since we are going to call schedule() anyways, there's
	 * no need to do the preemption check when the rq_lock is released.
	 */

That is, to document why we have the preempt_disable() before the unlock:

which is pretty obvious, but I let Peter decide on that.

To us maybe, but I like to have comments that explain why things are done to average people. ;-)

If I go to another kernel developer outside the core kernel, would they know why there's a preempt_disable() there?

 	preempt_disable();
	rq_unlock_irq(rq, &rf);
kernel/sched/core.c +0 -0
 	sched_preempt_enable_no_resched();
 
 	schedule();
Quoted 3 lines

Not everyone knows that the rq_unlock_irq() would trigger a schedule if an interrupt happened as soon as irqs were enabled again and need_resched was set.

Sorry a bit late to the party.

Personally, what actually is tripping me off is that rq_unlock_irq() will end up calling preempt_enable(), and then we do sched_preempt_enable_no_resched(). Was there an earlier preempt_disable() called up in the chain that I couldn't figure out that's why it's okay to do the 2? Otherwise I see we have imbalanced preempt_disable/enable.

	preempt_disable()
	rq_unlock_irq()
		__raw_spin_unlock_irq()
			local_irq_enable()
			preempt_enable()	// first preempt_count_dec()
	sched_preempt_enable_no_resched()	// second preempt_count_dec()

Thanks

--
Qais Yousef

#7 lore raw

[tip: sched/core] sched: Reenable interrupts in do_sched_yield()

In reply to Thomas Gleixner
The following commit has been merged into the sched/core branch of tip:

The following commit has been merged into the sched/core branch of tip:

Commit-ID:     345a957fcc95630bf5535d7668a59ed983eb49a7
Gitweb:        https://git.kernel.org/tip/345a957fcc95630bf5535d7668a59ed983eb49a7
Author:        Thomas Gleixner <[email protected]>
AuthorDate:    Tue, 20 Oct 2020 16:46:55 +02:00
Committer:     Peter Zijlstra <[email protected]>
CommitterDate: Thu, 29 Oct 2020 11:00:31 +01:00

sched: Reenable interrupts in do_sched_yield()

do_sched_yield() invokes schedule() with interrupts disabled which is not allowed. This goes back to the pre git era to commit a6efb709806c ("[PATCH] irqlock patch 2.5.27-H6") in the history tree.

Reenable interrupts and remove the misleading comment which "explains" it.

---
 kernel/sched/core.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
kernel/sched/core.c +1 -5
index d2003a7..6f533bb 100644
@@ -6094,12 +6094,8 @@ static void do_sched_yield(void)
 	schedstat_inc(rq->yld_count);
 	current->sched_class->yield_task(rq);
 
-	/*
-	 * Since we are going to call schedule() anyway, there's
-	 * no need to preempt or enable interrupts:
-	 */
 	preempt_disable();
-	rq_unlock(rq, &rf);
+	rq_unlock_irq(rq, &rf);
 	sched_preempt_enable_no_resched();
 
 	schedule();