Skip to contentSkip to conversation

sched: unlocked context-switches

PATCH David Mosberger-Tang 5 participants 11 messages Apr 9, 2005 – Apr 13, 2005 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw
Quoted 9 lines

Tony:

Quoted 5 lines

Ingo:

tested on x86, and all other arches should work as well, but if an architecture has irqs-off assumptions in its switch_to() logic it might break. (I havent found any but there may such assumptions.)

Quoted 5 lines

The ia64_switch_to() code includes a section that can change a pinned MMU mapping (when the stack for the new process is in a different granule from the stack for the old process). The code beyond the ".map" label in arch/ia64/kernel/entry.S includes the comment:

Also, there was a nasty dead-lock that could trigger if the context-switch was interrupted by a TLB flush IPI. I don't remember the details offhand, but I'm pretty sure it had to do with switch_mm(), so I suspect it may not be enough to disable irqs just for ia64_switch_to(). Tread with care!

	--david
#2 lore raw
Quoted 11 lines · David Mosberger-Tang
Quoted 5 lines

The ia64_switch_to() code includes a section that can change a pinned MMU mapping (when the stack for the new process is in a different granule from the stack for the old process). The code beyond the ".map" label in arch/ia64/kernel/entry.S includes the comment:

Also, there was a nasty dead-lock that could trigger if the context-switch was interrupted by a TLB flush IPI. I don't remember the details offhand, but I'm pretty sure it had to do with switch_mm(), so I suspect it may not be enough to disable irqs just for ia64_switch_to(). Tread with care!

we'll see. The patch certainly needs more testing. Generally we do switch_mm()'s outside of the scheduler as well, without disabling interrupts.

Ingo

#3 lore raw
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line · Ingo Molnar
Quoted 11 lines · Ingo> * David Mosberger-Tang <[email protected]> wrote:
Quoted 11 lines
Quoted 5 lines

The ia64_switch_to() code includes a section that can change a pinned MMU mapping (when the stack for the new process is in a different granule from the stack for the old process). The code beyond the ".map" label in arch/ia64/kernel/entry.S includes the comment:

Also, there was a nasty dead-lock that could trigger if the context-switch was interrupted by a TLB flush IPI. I don't remember the details offhand, but I'm pretty sure it had to do with switch_mm(), so I suspect it may not be enough to disable irqs just for ia64_switch_to(). Tread with care!

Ingo> we'll see. The patch certainly needs more testing. Generally Ingo> we do switch_mm()'s outside of the scheduler as well, without Ingo> disabling interrupts.

Yes, of course. The deadlock was due to context-switching, not switch_mm() per se. Hopefully someone else beats me to remembering the details before Monday.

	--david
#4 lore raw
Quoted 3 lines · On Sat, 9 Apr 2005 00:15:37 -0700 David Mosberger <[email protected]>…

Yes, of course. The deadlock was due to context-switching, not switch_mm() per se. Hopefully someone else beats me to remembering the details before Monday.

Sparc64 has a deadlock because we hold mm->page_table_lock during switch_mm(). I bet IA64 did something similar, as I remember it had a very similar locking issue in this area.

So the deadlock was, we held the runqueue locks over switch_mm(), switch_mm() spins on mm->page_table_lock, the cpu which does have mm->page_table_lock tries to do a wakeup on the first cpu's runqueue. Classic AB-BA deadlock.

#5 lore raw
Quoted 4 lines · David S. Miller
Quoted 3 lines

Yes, of course. The deadlock was due to context-switching, not switch_mm() per se. Hopefully someone else beats me to remembering the details before Monday.

Quoted 8 lines

Sparc64 has a deadlock because we hold mm->page_table_lock during switch_mm(). I bet IA64 did something similar, as I remember it had a very similar locking issue in this area.

So the deadlock was, we held the runqueue locks over switch_mm(), switch_mm() spins on mm->page_table_lock, the cpu which does have mm->page_table_lock tries to do a wakeup on the first cpu's runqueue. Classic AB-BA deadlock.

yeah, i can see that happening - holding the runqueue lock and enabling interrupts. (it's basically never safe to enable irqs with the runqueue lock held.)

the patch drops both the runqueue lock and enables interrupts, so this particular issue should not trigger.

Ingo

#6 lore raw
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line · Ingo Molnar
Quoted 8 lines · Ingo> * David S. Miller <[email protected]> wrote:
Quoted 7 lines
Quoted 2 lines

Yes, of course. The deadlock was due to context-switching, not switch_mm() per se. Hopefully someone else beats me to

remembering > the details before Monday.

Sparc64 has a deadlock because we hold mm->page_table_lock during switch_mm(). I bet IA64 did something similar, as I remember it had a very similar locking issue in this area.

Quoted 5 lines
Quoted 5 lines

So the deadlock was, we held the runqueue locks over switch_mm(), switch_mm() spins on mm->page_table_lock, the cpu which does have mm-> page_table_lock tries to do a wakeup on the first cpu's mm-> runqueue.
Classic AB-BA deadlock.

Ingo> yeah, i can see that happening - holding the runqueue lock and Ingo> enabling interrupts. (it's basically never safe to enable irqs Ingo> with the runqueue lock held.)

Ingo> the patch drops both the runqueue lock and enables interrupts, Ingo> so this particular issue should not trigger.

I had to refresh my memory with a quick Google search that netted [1] (look for "Disable interrupts during context switch"). Actually, it wasn't really a deadlock, but rather a livelock, since a CPU got stuck on an infinite page-not-present loop.

Fundamentally, the issue was that doing the switch_mm() and switch_to() with interrupts enabled opened a window during which you could get a call to flush_tlb_mm() (as a result of an IPI). This, in turn, could end up activating the wrong MMU-context, since the action of flush_tlb_mm() depends on the value of current->active_mm. The problematic sequence was:

1) schedule() calls switch_mm() which calls activate_context() to switch to the new address-space
2) IPI comes in and flush_tlb_mm(mm) gets called
3) "current" still points to old task and if "current->active_mm == mm", activate_mm() is called for the old address-space, resetting the address-space back to that of the old task

Now, Ingo says that the order is reversed with his patch, i.e., switch_mm() happens after switch_to(). That means flush_tlb_mm() may now see a current->active_mm which hasn't really been activated yet. That should be OK since it would just mean that we'd do an early (and duplicate) activate_context(). While it does not give me a warm and fuzzy feeling to have this inconsistent state be observable by interrupt-handlers (and, in particular, IPI-handlers), I don't see any problem with it off hand.

	--david

[1] http://www.gelato.unsw.edu.au/linux-ia64/0307/6109.html

#7 lore raw
Quoted 19 lines · David Mosberger

I had to refresh my memory with a quick Google search that netted [1] (look for "Disable interrupts during context switch"). Actually, it wasn't really a deadlock, but rather a livelock, since a CPU got stuck on an infinite page-not-present loop.

Fundamentally, the issue was that doing the switch_mm() and switch_to() with interrupts enabled opened a window during which you could get a call to flush_tlb_mm() (as a result of an IPI). This, in turn, could end up activating the wrong MMU-context, since the action of flush_tlb_mm() depends on the value of current->active_mm. The problematic sequence was:

1) schedule() calls switch_mm() which calls activate_context() to switch to the new address-space
2) IPI comes in and flush_tlb_mm(mm) gets called
3) "current" still points to old task and if "current->active_mm == mm", activate_mm() is called for the old address-space, resetting the address-space back to that of the old task

Quoted 3 lines

Now, Ingo says that the order is reversed with his patch, i.e., switch_mm() happens after switch_to(). That means flush_tlb_mm() may now see a current->active_mm which hasn't really been activated yet.

If that did bother you, could you keep track of the actually activated mm in your arch code? Or would that involve more arch hooks and general ugliness in the scheduler?

Could you alternatively just disable interrupts across the switch? I guess that would require a bit of #ifdefery in sched.c, which we are trying to move away from, but it would be pretty minimal. Much better than what is there now.

Quoted 6 lines

That should be OK since it would just mean that we'd do an early (and duplicate) activate_context(). While it does not give me a warm and fuzzy feeling to have this inconsistent state be observable by interrupt-handlers (and, in particular, IPI-handlers), I don't see any problem with it off hand.

IMO it *is* the direction we should move towards. That is, liberating context switching from scheduler locking, and providing a single set of semantics for the context switch hooks for all architectures.

No rush, of course. But it would be good to get it in sooner or later.

Nick

#8 lore raw
Quoted 6 lines
Quoted 6 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line · Nick Piggin

Now, Ingo says that the order is reversed with his patch, i.e., switch_mm() happens after switch_to(). That means flush_tlb_mm() may now see a current->active_mm which hasn't really been activated yet.

Nick> If that did bother you, could you keep track of the actually Nick> activated mm in your arch code? Or would that involve more Nick> arch hooks and general ugliness in the scheduler?

I'm sorry, but I don't see the point of this. We are already tracking care of ownership, just not atomically. What's the point of putting another level of (atomic) tracking on top of it. That seems exceedingly ugly.

	--david
#9 lore raw
Quoted 8 lines · David Mosberger
Quoted 7 lines
Quoted 7 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line · Nick Piggin

Now, Ingo says that the order is reversed with his patch, i.e., switch_mm() happens after switch_to(). That means flush_tlb_mm() may now see a current->active_mm which hasn't really been activated yet.

Quoted 9 lines

Nick> If that did bother you, could you keep track of the actually Nick> activated mm in your arch code? Or would that involve more Nick> arch hooks and general ugliness in the scheduler?

I'm sorry, but I don't see the point of this. We are already tracking care of ownership, just not atomically. What's the point of putting another level of (atomic) tracking on top of it. That seems exceedingly ugly.

Well, you were worried about it not being atomic. So that would be the point, but I agree it would probably be exceedingly ugly if implemented.

Nick

#10 lore raw
In reply to David Mosberger
Quoted 8 lines · David Mosberger

Now, Ingo says that the order is reversed with his patch, i.e., switch_mm() happens after switch_to(). That means flush_tlb_mm() may now see a current->active_mm which hasn't really been activated yet. That should be OK since it would just mean that we'd do an early (and duplicate) activate_context(). While it does not give me a warm and fuzzy feeling to have this inconsistent state be observable by interrupt-handlers (and, in particular, IPI-handlers), I don't see any problem with it off hand.

thanks for the analysis. I fundamentally dont have any fuzzy feeling from having _any_ portion of the context-switch path nonatomic, but with more complex hardware it's just not possible it seems.

Ingo

#11 lore raw
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line · Ingo Molnar
Quoted 9 lines · Ingo> * David Mosberger <[email protected]> wrote:
Quoted 9 lines

Now, Ingo says that the order is reversed with his patch, i.e., switch_mm() happens after switch_to(). That means flush_tlb_mm() may now see a current->active_mm which hasn't really been activated yet. That should be OK since it would just mean that we'd do an early (and duplicate) activate_context(). While it does not give me a warm and fuzzy feeling to have this inconsistent state be observable by interrupt-handlers (and, in particular, IPI-handlers), I don't see any problem with it off hand.

Ingo> thanks for the analysis. I fundamentally dont have any fuzzy Ingo> feeling from having _any_ portion of the context-switch path Ingo> nonatomic, but with more complex hardware it's just not Ingo> possible it seems.

No kidding! ;-)

I _think_ the change is OK. I'll need testing, of course. Sure would be nice to have 2.7.xx...

Thanks,

	--david