Skip to contentSkip to conversation

ARM: mm: ensure TTBR0 is restored when changing ASID on rollover

PATCH New Will Deacon 4 participants 10 messages Jun 7, 2011 – Nov 23, 2011 Arm (32-bit)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ARM: mm: ensure TTBR0 is restored when changing ASID on rollover

In 52af9c6c ("ARM: 6943/1: mm: use TTBR1 instead of reserved context ID") I updated the ASID rollover code to use only the kernel page tables whilst updating the ASID.

Unfortunately, the code to restore the user page tables was part of a later patch which isn't yet in mainline, so this leaves the code quite broken.

This patch fixes the issue by calling cpu_switch_mm to change the ASID which has the side-effect of setting up TTBR0 to point to the user tables.

---

Russell - I've reposted this to the list because it somehow got lost in the archive and you've expressed some concerns over the code via the patch system. I think the only opportunity for a race is when a CPU doing switch_mm is interrupted by a rollover event occurring on another core, but this is something that exists in the current code anyway and is not affected by this patch.

 arch/arm/mm/context.c |    3 +--
 1 files changed, 1 insertions(+), 2 deletions(-)
arch/arm/mm/context.c +1 -2
index 8bfae96..2352395 100644
@@ -100,8 +100,7 @@ static void reset_context(void *info)
 	set_mm_context(mm, asid);
 
 	/* set the new ASID */
-	asm("mcr	p15, 0, %0, c13, c0, 1\n" : : "r" (mm->context.id));
-	isb();
+	cpu_switch_mm(mm->pgd, mm);
 }
 
 #else
Signature · 1 line
1.7.0.4
#1 lore raw

PATCH 1/1 ARM: mm: ensure TTBR0 is restored when changing ASID on rollover

Quoted 7 lines · Will Deacon

In 52af9c6c ("ARM: 6943/1: mm: use TTBR1 instead of reserved context ID") I updated the ASID rollover code to use only the kernel page tables whilst updating the ASID.

Unfortunately, the code to restore the user page tables was part of a later patch which isn't yet in mainline, so this leaves the code quite broken.

IOW, after an ASID roll-over on SMP, the cross-called reset_context() function sets TTBR0 to swapper_pg_dir but never sets it back to the one of the currently running process. So interrupted user space processes would fault when returning from an ASID roll-over event happening on a different CPU.

Quoted 6 lines

This patch fixes the issue by calling cpu_switch_mm to change the ASID which has the side-effect of setting up TTBR0 to point to the user tables.

#2 lore raw

PATCH 1/1 ARM: mm: ensure TTBR0 is restored when changing ASID on rollover

In reply to Will Deacon
Quoted 6 lines · Will Deacon

Russell - I've reposted this to the list because it somehow got lost in the archive and you've expressed some concerns over the code via the patch system. I think the only opportunity for a race is when a CPU doing switch_mm is interrupted by a rollover event occurring on another core, but this is something that exists in the current code anyway and is not affected by this patch.

However, these patches are introducing a brand new race between the switch_mm code and the reset_context code.

With the new switch_mm() code, we switch TTBR0 to be the same as TTBR1. If we then receive an IPI for reset_context(), we will change TTBR0 to point at a set of page tables which don't contain just global mappings.

After returning from reset_context(), we will resume switch_mm(), and change the ASID value with the page tables pointing to non-global mappings, violating the whole reason for the switch_mm() change.

The only way around this is to make reset_context() preserve the TTBR0 value across itself, by reading it initially and then restoring before returning.

So, even though the current code is broken, I'm not applying this patch as it isn't anywhere near right - and we can do right quite easily here.

#3 lore raw

PATCH 1/1 ARM: mm: ensure TTBR0 is restored when changing ASID on rollover

Hi Russell,

Thanks for replying to this.

Quoted 11 lines · Russell King - ARM Linux
Quoted 6 lines · Will Deacon

Russell - I've reposted this to the list because it somehow got lost in the archive and you've expressed some concerns over the code via the patch system. I think the only opportunity for a race is when a CPU doing switch_mm is interrupted by a rollover event occurring on another core, but this is something that exists in the current code anyway and is not affected by this patch.

However, these patches are introducing a brand new race between the switch_mm code and the reset_context code.

Quoted 7 lines

With the new switch_mm() code, we switch TTBR0 to be the same as TTBR1. If we then receive an IPI for reset_context(), we will change TTBR0 to point at a set of page tables which don't contain just global mappings.

After returning from reset_context(), we will resume switch_mm(), and change the ASID value with the page tables pointing to non-global mappings, violating the whole reason for the switch_mm() change.

Whilst this is a new race condition, it is analagous to the one we have already and could be fixed at the same time.

Quoted 3 lines

The only way around this is to make reset_context() preserve the TTBR0 value across itself, by reading it initially and then restoring before returning.

I don't think this is safe. The reset_context() broadcast could interrupt us at a time where current_mm has been updated during context switch, but TTBR0 still contains the page tables of the previous mm. If we blindly save and restore the value from the hardware, we could end up setting the wrong ASID and then we're back to square one.

Quoted 2 lines

So, even though the current code is broken, I'm not applying this patch as it isn't anywhere near right - and we can do right quite easily here.

I think the easiest fix is to go with my original proposal of disabling interrupts during switch_mm. Then this patch will work as intended and we'll eliminate the original race too. Since the interrupts need only be disabled for SMP systems, it won't affect any platforms with VIVT D-caches, where interupts should be left enabled while the cache is flushed.

Any ideas?

Will

#4 lore raw

PATCH 1/1 ARM: mm: ensure TTBR0 is restored when changing ASID on rollover

Quoted 8 lines · Will Deacon
Quoted 7 lines · Russell King - ARM Linux

However, these patches are introducing a brand new race between the switch_mm code and the reset_context code.

With the new switch_mm() code, we switch TTBR0 to be the same as TTBR1. If we then receive an IPI for reset_context(), we will change TTBR0 to point at a set of page tables which don't contain just global mappings.

Quoted 6 lines
Quoted 3 lines

After returning from reset_context(), we will resume switch_mm(), and change the ASID value with the page tables pointing to non-global mappings, violating the whole reason for the switch_mm() change.

Whilst this is a new race condition, it is analagous to the one we have already and could be fixed at the same time.

Ok, I think we should revert the original patches then. They were rushed in during the merge window, and as can be seen, rushing in patches because we _think_ they're right is never the correct thing to do - we've ended up with a completely broken situation as stuff now stands.

Let's take out these changes and sort it out properly - not only do we need to sort out these problems but we should also get rid of the __ARCH_WANT_INTERRUPTS_ON_CTXSW thing completely. I have a patch which I've only tested on SA-1110 which does this so far, but it needs a little more work to clean up some stuff.

So, we're weren't - and still aren't - ready for any of these changes.

#5 lore raw

PATCH 1/1 ARM: mm: ensure TTBR0 is restored when changing ASID on rollover

Quoted 13 lines · Russell King - ARM Linux
Quoted 12 lines · Will Deacon
Quoted 10 lines · Russell King - ARM Linux

However, these patches are introducing a brand new race between the switch_mm code and the reset_context code.

With the new switch_mm() code, we switch TTBR0 to be the same as TTBR1. If we then receive an IPI for reset_context(), we will change TTBR0 to point at a set of page tables which don't contain just global mappings.

After returning from reset_context(), we will resume switch_mm(), and change the ASID value with the page tables pointing to non-global mappings, violating the whole reason for the switch_mm() change.

Quoted 7 lines
Quoted 2 lines

Whilst this is a new race condition, it is analagous to the one we have already and could be fixed at the same time.

Ok, I think we should revert the original patches then. They were rushed in during the merge window, and as can be seen, rushing in patches because we _think_ they're right is never the correct thing to do - we've ended up with a completely broken situation as stuff now stands.

Seems a shame given that disabling interrupts during switch_mm would fix this, but yes, reverting them is certainly better than having a broken kernel. I think you just need to revert 6944/1 and 6943/1; the other two from that series are fine to be left in.

Quoted 5 lines

Let's take out these changes and sort it out properly - not only do we need to sort out these problems but we should also get rid of the __ARCH_WANT_INTERRUPTS_ON_CTXSW thing completely. I have a patch which I've only tested on SA-1110 which does this so far, but it needs a little more work to clean up some stuff.

Could you post that code please? I can then rebase these patches against it for future inclusion in mainline. Does the removal of __ARCH_WANT_INTERRUPTS_ON_CTXSW mean that switch_mm will run with interrupts disabled by default?

Quoted 1 line

So, we're weren't - and still aren't - ready for any of these changes.

Once the interrupt stuff is sorted out I can rebase the ASID stuff on top of it and we should be ready to go.

Cheers,

Will

#6 lore raw

PATCH 1/1 ARM: mm: ensure TTBR0 is restored when changing ASID on rollover

Quoted 4 lines · Will Deacon

Seems a shame given that disabling interrupts during switch_mm would fix this, but yes, reverting them is certainly better than having a broken kernel. I think you just need to revert 6944/1 and 6943/1; the other two from that series are fine to be left in.

Ok.

Quoted 8 lines
Quoted 5 lines

Let's take out these changes and sort it out properly - not only do we need to sort out these problems but we should also get rid of the __ARCH_WANT_INTERRUPTS_ON_CTXSW thing completely. I have a patch which I've only tested on SA-1110 which does this so far, but it needs a little more work to clean up some stuff.

Could you post that code please? I can then rebase these patches against it for future inclusion in mainline.

I'd like to finish sorting it out properly first, so that it's all sane.

Quoted 3 lines

Does the removal of
__ARCH_WANT_INTERRUPTS_ON_CTXSW mean that switch_mm will run with interrupts disabled by default?

Yes, just like all but one other arch in the kernel. The scheduler folk would like to see this symbol die...

#7 lore raw

PATCH 1/1 ARM: mm: ensure TTBR0 is restored when changing ASID on rollover

Sorry to dig up an old thread....

Quoted 8 lines · Russell King - ARM Linux
Quoted 7 lines · Will Deacon
Quoted 5 lines

Let's take out these changes and sort it out properly - not only do we need to sort out these problems but we should also get rid of the __ARCH_WANT_INTERRUPTS_ON_CTXSW thing completely. I have a patch which I've only tested on SA-1110 which does this so far, but it needs a little more work to clean up some stuff.

Could you post that code please? I can then rebase these patches against it for future inclusion in mainline.

Quoted 7 lines

I'd like to finish sorting it out properly first, so that it's all sane.

Quoted 3 lines

Does the removal of
__ARCH_WANT_INTERRUPTS_ON_CTXSW mean that switch_mm will run with interrupts disabled by default?

Yes, just like all but one other arch in the kernel. The scheduler folk would like to see this symbol die...

Was this patch ever posted? This looks like a particularly annoying problem to debug if it actually occurs (plus ARM is the final holdout for this symbol now).

Signature · 2 lines
Sent by an employee of the Qualcomm Innovation Center, Inc.
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum.
#8 lore raw

PATCH 1/1 ARM: mm: ensure TTBR0 is restored when changing ASID on rollover

In reply to Russell King - ARM Linux
Quoted 13 lines · Russell King - ARM Linux
Quoted 12 lines · Will Deacon
Quoted 10 lines · Russell King - ARM Linux

However, these patches are introducing a brand new race between the switch_mm code and the reset_context code.

With the new switch_mm() code, we switch TTBR0 to be the same as TTBR1. If we then receive an IPI for reset_context(), we will change TTBR0 to point at a set of page tables which don't contain just global mappings.

After returning from reset_context(), we will resume switch_mm(), and change the ASID value with the page tables pointing to non-global mappings, violating the whole reason for the switch_mm() change.

Quoted 7 lines
Quoted 2 lines

Whilst this is a new race condition, it is analagous to the one we have already and could be fixed at the same time.

Ok, I think we should revert the original patches then. ?They were rushed in during the merge window, and as can be seen, rushing in patches because we _think_ they're right is never the correct thing to do - we've ended up with a completely broken situation as stuff now stands.

We rushed a series of patches fixing this but you didn't like the patch disabling interrupts around cpu_switch_mm(). This turned out to be essential for avoiding the race condition.

Please note that the old switch_mm code with reserved ASID is broken on A15 (and not just in theory), hence the need to use reserved TTBR0.

Quoted 5 lines

Let's take out these changes and sort it out properly - not only do we need to sort out these problems but we should also get rid of the __ARCH_WANT_INTERRUPTS_ON_CTXSW thing completely. ?I have a patch which I've only tested on SA-1110 which does this so far, but it needs a little more work to clean up some stuff.

Even if you get rid of __ARCH_WANT_INTERRUPTS_ON_CTXSW, I would much prefer to use the new switch_mm code as a base rather than going back to the reserved ASID. The simplest way to fix the race condition you mentioned is to also integrate the other patch from Will which disables the interrupts around cpu_switch_mm(). After that we have more time to review the __ARCH_WANT_INTERRUPTS_ON_CTXSW patch.

Signature · 1 line
Catalin
#9 lore raw

PATCH 1/1 ARM: mm: ensure TTBR0 is restored when changing ASID on rollover

Quoted 17 lines · Catalin Marinas
Quoted 16 lines · Russell King - ARM Linux
Quoted 14 lines · Will Deacon
Quoted 10 lines · Russell King - ARM Linux

However, these patches are introducing a brand new race between the switch_mm code and the reset_context code.

With the new switch_mm() code, we switch TTBR0 to be the same as TTBR1. If we then receive an IPI for reset_context(), we will change TTBR0 to point at a set of page tables which don't contain just global mappings.

After returning from reset_context(), we will resume switch_mm(), and change the ASID value with the page tables pointing to non-global mappings, violating the whole reason for the switch_mm() change.

Whilst this is a new race condition, it is analagous to the one we have already and could be fixed at the same time.

Quoted 7 lines
Quoted 4 lines

Ok, I think we should revert the original patches then. ?They were rushed in during the merge window, and as can be seen, rushing in patches because we _think_ they're right is never the correct thing to do - we've ended up with a completely broken situation as stuff now stands.

We rushed a series of patches fixing this but you didn't like the patch disabling interrupts around cpu_switch_mm().

No. We rushed the entire thing as can be seen due to the "forgotten" patch and the submission of it _during_ the merge window. Clearly no one had thought enough about the inter-relationship between the patches when deciding that only some of the patches should go in.

That's why there's a big reason to avoid merging any new patches during the merge window - it gives time for such things to be found before it becomes a problem.

Quoted 2 lines

Please note that the old switch_mm code with reserved ASID is broken on A15 (and not just in theory), hence the need to use reserved TTBR0.

But A15 is not actively supported in mainline yet so that's not a decision relevant to what we do with mainline.

Quoted 6 lines
Quoted 5 lines

Let's take out these changes and sort it out properly - not only do we need to sort out these problems but we should also get rid of the __ARCH_WANT_INTERRUPTS_ON_CTXSW thing completely. ?I have a patch which I've only tested on SA-1110 which does this so far, but it needs a little more work to clean up some stuff.

Quoted 6 lines

Even if you get rid of __ARCH_WANT_INTERRUPTS_ON_CTXSW, I would much prefer to use the new switch_mm code as a base rather than going back to the reserved ASID. The simplest way to fix the race condition you mentioned is to also integrate the other patch from Will which disables the interrupts around cpu_switch_mm(). After that we have more time to review the __ARCH_WANT_INTERRUPTS_ON_CTXSW patch.

Once we have the context switching situation sorted for VIVT we can then reconsider these patches - their pre-requisits will have been solved by way of the VIVT situation being sorted.