aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWill Deacon <will.deacon@arm.com>2015-11-26 08:49:39 -0500
committerCatalin Marinas <catalin.marinas@arm.com>2015-11-26 10:27:10 -0500
commit0ebea8088095f1c18c1d1de284ccc4c479ca21c1 (patch)
treeb556eb0a55a84435836a00d49027d0593e0ead3b
parentf1b9032f61c0412082a240cb7245f8b79e09ae8d (diff)
arm64: mm: keep reserved ASIDs in sync with mm after multiple rollovers
Under some unusual context-switching patterns, it is possible to end up with multiple threads from the same mm running concurrently with different ASIDs: 1. CPU x schedules task t with mm p containing ASID a and generation g This task doesn't block and the CPU doesn't context switch. So: * per_cpu(active_asid, x) = {g,a} * p->context.id = {g,a} 2. Some other CPU generates an ASID rollover. The global generation is now (g + 1). CPU x is still running t, with no context switch and so per_cpu(reserved_asid, x) = {g,a} 3. CPU y schedules task t', which shares mm p with t. The generation mismatches, so we take the slowpath and hit the reserved ASID from CPU x. p is then updated so that p->context.id = {g + 1,a} 4. CPU y schedules some other task u, which has an mm != p. 5. Some other CPU generates *another* CPU rollover. The global generation is now (g + 2). CPU x is still running t, with no context switch and so per_cpu(reserved_asid, x) = {g,a}. 6. CPU y once again schedules task t', but now *fails* to hit the reserved ASID from CPU x because of the generation mismatch. This results in a new ASID being allocated, despite the fact that t is still running on CPU x with the same mm. Consequently, TLBIs (e.g. as a result of CoW) will not be synchronised between the two threads. This patch fixes the problem by updating all of the matching reserved ASIDs when we hit on the slowpath (i.e. in step 3 above). This keeps the reserved ASIDs in-sync with the mm and avoids the problem. Reported-by: Tony Thompson <anthony.thompson@arm.com> Reviewed-by: Catalin Marinas <catalin.marinas@arm.com> Signed-off-by: Will Deacon <will.deacon@arm.com> Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
-rw-r--r--arch/arm64/mm/context.c38
1 files changed, 26 insertions, 12 deletions
diff --git a/arch/arm64/mm/context.c b/arch/arm64/mm/context.c
index f636a2639f03..e87f53ff5f58 100644
--- a/arch/arm64/mm/context.c
+++ b/arch/arm64/mm/context.c
@@ -76,13 +76,28 @@ static void flush_context(unsigned int cpu)
76 __flush_icache_all(); 76 __flush_icache_all();
77} 77}
78 78
79static int is_reserved_asid(u64 asid) 79static bool check_update_reserved_asid(u64 asid, u64 newasid)
80{ 80{
81 int cpu; 81 int cpu;
82 for_each_possible_cpu(cpu) 82 bool hit = false;
83 if (per_cpu(reserved_asids, cpu) == asid) 83
84 return 1; 84 /*
85 return 0; 85 * Iterate over the set of reserved ASIDs looking for a match.
86 * If we find one, then we can update our mm to use newasid
87 * (i.e. the same ASID in the current generation) but we can't
88 * exit the loop early, since we need to ensure that all copies
89 * of the old ASID are updated to reflect the mm. Failure to do
90 * so could result in us missing the reserved ASID in a future
91 * generation.
92 */
93 for_each_possible_cpu(cpu) {
94 if (per_cpu(reserved_asids, cpu) == asid) {
95 hit = true;
96 per_cpu(reserved_asids, cpu) = newasid;
97 }
98 }
99
100 return hit;
86} 101}
87 102
88static u64 new_context(struct mm_struct *mm, unsigned int cpu) 103static u64 new_context(struct mm_struct *mm, unsigned int cpu)
@@ -92,12 +107,14 @@ static u64 new_context(struct mm_struct *mm, unsigned int cpu)
92 u64 generation = atomic64_read(&asid_generation); 107 u64 generation = atomic64_read(&asid_generation);
93 108
94 if (asid != 0) { 109 if (asid != 0) {
110 u64 newasid = generation | (asid & ~ASID_MASK);
111
95 /* 112 /*
96 * If our current ASID was active during a rollover, we 113 * If our current ASID was active during a rollover, we
97 * can continue to use it and this was just a false alarm. 114 * can continue to use it and this was just a false alarm.
98 */ 115 */
99 if (is_reserved_asid(asid)) 116 if (check_update_reserved_asid(asid, newasid))
100 return generation | (asid & ~ASID_MASK); 117 return newasid;
101 118
102 /* 119 /*
103 * We had a valid ASID in a previous life, so try to re-use 120 * We had a valid ASID in a previous life, so try to re-use
@@ -105,7 +122,7 @@ static u64 new_context(struct mm_struct *mm, unsigned int cpu)
105 */ 122 */
106 asid &= ~ASID_MASK; 123 asid &= ~ASID_MASK;
107 if (!__test_and_set_bit(asid, asid_map)) 124 if (!__test_and_set_bit(asid, asid_map))
108 goto bump_gen; 125 return newasid;
109 } 126 }
110 127
111 /* 128 /*
@@ -129,10 +146,7 @@ static u64 new_context(struct mm_struct *mm, unsigned int cpu)
129set_asid: 146set_asid:
130 __set_bit(asid, asid_map); 147 __set_bit(asid, asid_map);
131 cur_idx = asid; 148 cur_idx = asid;
132 149 return asid | generation;
133bump_gen:
134 asid |= generation;
135 return asid;
136} 150}
137 151
138void check_and_switch_context(struct mm_struct *mm, unsigned int cpu) 152void check_and_switch_context(struct mm_struct *mm, unsigned int cpu)