Skip to content

Commit 947bf10

Browse files
committed
ARC: [ASID] Track ASID allocation cycles/generations
This helps remove asid-to-mm reverse map While mm->context.id contains the ASID assigned to a process, our ASID allocator also used asid_mm_map[] reverse map. In a new allocation cycle (mm->ASID >= @asid_cache), the Round Robin ASID allocator used this to check if new @asid_cache belonged to some mm2 (from prev cycle). If so, it could locate that mm using the ASID reverse map, and mark that mm as unallocated ASID, to force it to refresh at the time of switch_mm() However, for SMP, the reverse map has to be maintained per CPU, so becomes 2 dimensional, hence got rid of it. With reverse map gone, it is NOT possible to reach out to current assignee. So we track the ASID allocation generation/cycle and on every switch_mm(), check if the current generation of CPU ASID is same as mm's ASID; If not it is refreshed. (Based loosely on arch/sh implementation) Signed-off-by: Vineet Gupta <vgupta@synopsys.com>
1 parent c601155 commit 947bf10

4 files changed

Lines changed: 40 additions & 86 deletions

File tree

‎arch/arc/include/asm/mmu.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
#ifndef __ASSEMBLY__
4949

5050
typedef struct {
51-
unsigned long asid; /* Pvt Addr-Space ID for mm */
51+
unsigned long asid; /* 8 bit MMU PID + Generation cycle */
5252
} mm_context_t;
5353

5454
#ifdef CONFIG_ARC_DBG_TLB_PARANOIA

‎arch/arc/include/asm/mmu_context.h‎

Lines changed: 28 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -34,99 +34,65 @@
3434
* When it reaches max 255, the allocation cycle starts afresh by flushing
3535
* the entire TLB and wrapping ASID back to zero.
3636
*
37-
* For book-keeping, Linux uses a couple of data-structures:
38-
* -mm_struct has an @asid field to keep a note of task's ASID (needed at the
39-
* time of say switch_mm( )
40-
* -An array of mm structs @asid_mm_map[] for asid->mm the reverse mapping,
41-
* given an ASID, finding the mm struct associated.
42-
*
43-
* The round-robin allocation algorithm allows for ASID stealing.
44-
* If asid tracker is at "x-1", a new req will allocate "x", even if "x" was
45-
* already assigned to another (switched-out) task. Obviously the prev owner
46-
* is marked with an invalid ASID to make it request for a new ASID when it
47-
* gets scheduled next time. However its TLB entries (with ASID "x") could
48-
* exist, which must be cleared before the same ASID is used by the new owner.
49-
* Flushing them would be plausible but costly solution. Instead we force a
50-
* allocation policy quirk, which ensures that a stolen ASID won't have any
51-
* TLB entries associates, alleviating the need to flush.
52-
* The quirk essentially is not allowing ASID allocated in prev cycle
53-
* to be used past a roll-over in the next cycle.
54-
* When this happens (i.e. task ASID > asid tracker), task needs to refresh
55-
* its ASID, aligning it to current value of tracker. If the task doesn't get
56-
* scheduled past a roll-over, hence its ASID is not yet realigned with
57-
* tracker, such ASID is anyways safely reusable because it is
58-
* gauranteed that TLB entries with that ASID wont exist.
37+
* A new allocation cycle, post rollover, could potentially reassign an ASID
38+
* to a different task. Thus the rule is to refresh the ASID in a new cycle.
39+
* The 32 bit @asid_cache (and mm->asid) have 8 bits MMU PID and rest 24 bits
40+
* serve as cycle/generation indicator and natural 32 bit unsigned math
41+
* automagically increments the generation when lower 8 bits rollover.
5942
*/
6043

61-
#define FIRST_ASID 0
62-
#define MAX_ASID 255 /* 8 bit PID field in PID Aux reg */
63-
#define NO_ASID (MAX_ASID + 1) /* ASID Not alloc to mmu ctxt */
64-
#define NUM_ASID ((MAX_ASID - FIRST_ASID) + 1)
44+
#define MM_CTXT_ASID_MASK 0x000000ff /* MMU PID reg :8 bit PID */
45+
#define MM_CTXT_CYCLE_MASK (~MM_CTXT_ASID_MASK)
46+
47+
#define MM_CTXT_FIRST_CYCLE (MM_CTXT_ASID_MASK + 1)
48+
#define MM_CTXT_NO_ASID 0UL
6549

66-
/* ASID to mm struct mapping */
67-
extern struct mm_struct *asid_mm_map[NUM_ASID + 1];
50+
#define hw_pid(mm) (mm->context.asid & MM_CTXT_ASID_MASK)
6851

69-
extern int asid_cache;
52+
extern unsigned int asid_cache;
7053

7154
/*
7255
* Get a new ASID if task doesn't have a valid one (unalloc or from prev cycle)
7356
* Also set the MMU PID register to existing/updated ASID
7457
*/
7558
static inline void get_new_mmu_context(struct mm_struct *mm)
7659
{
77-
struct mm_struct *prev_owner;
7860
unsigned long flags;
7961

8062
local_irq_save(flags);
8163

8264
/*
8365
* Move to new ASID if it was not from current alloc-cycle/generation.
66+
* This is done by ensuring that the generation bits in both mm->ASID
67+
* and cpu's ASID counter are exactly same.
8468
*
8569
* Note: Callers needing new ASID unconditionally, independent of
8670
* generation, e.g. local_flush_tlb_mm() for forking parent,
8771
* first need to destroy the context, setting it to invalid
8872
* value.
8973
*/
90-
if (mm->context.asid <= asid_cache)
74+
if (!((mm->context.asid ^ asid_cache) & MM_CTXT_CYCLE_MASK))
9175
goto set_hw;
9276

93-
/*
94-
* Relinquish the currently owned ASID (if any).
95-
* Doing unconditionally saves a cmp-n-branch; for already unused
96-
* ASID slot, the value was/remains NULL
97-
*/
98-
asid_mm_map[mm->context.asid] = (struct mm_struct *)NULL;
77+
/* move to new ASID and handle rollover */
78+
if (unlikely(!(++asid_cache & MM_CTXT_ASID_MASK))) {
9979

100-
/* move to new ASID */
101-
if (++asid_cache > MAX_ASID) { /* ASID roll-over */
102-
asid_cache = FIRST_ASID;
10380
flush_tlb_all();
104-
}
10581

106-
/*
107-
* Is next ASID already owned by some-one else (we are stealing it).
108-
* If so, let the orig owner be aware of this, so when it runs, it
109-
* asks for a brand new ASID. This would only happen for a long-lived
110-
* task with ASID from prev allocation cycle (before ASID roll-over).
111-
*
112-
* This might look wrong - if we are re-using some other task's ASID,
113-
* won't we use it's stale TLB entries too. Actually the algorithm takes
114-
* care of such a case: it ensures that task with ASID from prev alloc
115-
* cycle, when scheduled will refresh it's ASID
116-
* The stealing scenario described here will only happen if that task
117-
* didn't get a chance to refresh it's ASID - implying stale entries
118-
* won't exist.
119-
*/
120-
prev_owner = asid_mm_map[asid_cache];
121-
if (prev_owner)
122-
prev_owner->context.asid = NO_ASID;
82+
/*
83+
* Above checke for rollover of 8 bit ASID in 32 bit container.
84+
* If the container itself wrapped around, set it to a non zero
85+
* "generation" to distinguish from no context
86+
*/
87+
if (!asid_cache)
88+
asid_cache = MM_CTXT_FIRST_CYCLE;
89+
}
12390

12491
/* Assign new ASID to tsk */
125-
asid_mm_map[asid_cache] = mm;
12692
mm->context.asid = asid_cache;
12793

12894
set_hw:
129-
write_aux_reg(ARC_REG_PID, mm->context.asid | MMU_ENABLE);
95+
write_aux_reg(ARC_REG_PID, hw_pid(mm) | MMU_ENABLE);
13096

13197
local_irq_restore(flags);
13298
}
@@ -138,7 +104,7 @@ static inline void get_new_mmu_context(struct mm_struct *mm)
138104
static inline int
139105
init_new_context(struct task_struct *tsk, struct mm_struct *mm)
140106
{
141-
mm->context.asid = NO_ASID;
107+
mm->context.asid = MM_CTXT_NO_ASID;
142108
return 0;
143109
}
144110

@@ -167,14 +133,7 @@ static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
167133

168134
static inline void destroy_context(struct mm_struct *mm)
169135
{
170-
unsigned long flags;
171-
172-
local_irq_save(flags);
173-
174-
asid_mm_map[mm->context.asid] = NULL;
175-
mm->context.asid = NO_ASID;
176-
177-
local_irq_restore(flags);
136+
mm->context.asid = MM_CTXT_NO_ASID;
178137
}
179138

180139
/* it seemed that deactivate_mm( ) is a reasonable place to do book-keeping

‎arch/arc/mm/tlb.c‎

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,7 @@
100100

101101

102102
/* A copy of the ASID from the PID reg is kept in asid_cache */
103-
int asid_cache = FIRST_ASID;
104-
105-
/* ASID to mm struct mapping. We have one extra entry corresponding to
106-
* NO_ASID to save us a compare when clearing the mm entry for old asid
107-
* see get_new_mmu_context (asm-arc/mmu_context.h)
108-
*/
109-
struct mm_struct *asid_mm_map[NUM_ASID + 1];
103+
unsigned int asid_cache = MM_CTXT_FIRST_CYCLE;
110104

111105
/*
112106
* Utility Routine to erase a J-TLB entry
@@ -281,7 +275,6 @@ void local_flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
281275
unsigned long end)
282276
{
283277
unsigned long flags;
284-
unsigned int asid;
285278

286279
/* If range @start to @end is more than 32 TLB entries deep,
287280
* its better to move to a new ASID rather than searching for
@@ -303,11 +296,10 @@ void local_flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
303296
start &= PAGE_MASK;
304297

305298
local_irq_save(flags);
306-
asid = vma->vm_mm->context.asid;
307299

308-
if (asid != NO_ASID) {
300+
if (vma->vm_mm->context.asid != MM_CTXT_NO_ASID) {
309301
while (start < end) {
310-
tlb_entry_erase(start | (asid & 0xff));
302+
tlb_entry_erase(start | hw_pid(vma->vm_mm));
311303
start += PAGE_SIZE;
312304
}
313305
}
@@ -361,9 +353,8 @@ void local_flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
361353
*/
362354
local_irq_save(flags);
363355

364-
if (vma->vm_mm->context.asid != NO_ASID) {
365-
tlb_entry_erase((page & PAGE_MASK) |
366-
(vma->vm_mm->context.asid & 0xff));
356+
if (vma->vm_mm->context.asid != MM_CTXT_NO_ASID) {
357+
tlb_entry_erase((page & PAGE_MASK) | hw_pid(vma->vm_mm));
367358
utlb_invalidate();
368359
}
369360

@@ -709,7 +700,8 @@ void tlb_paranoid_check(unsigned int mm_asid, unsigned long addr)
709700
* - SW needs to have a valid ASID
710701
*/
711702
if (addr < 0x70000000 &&
712-
((mmu_asid != mm_asid) || (mm_asid == NO_ASID)))
703+
((mm_asid == MM_CTXT_NO_ASID) ||
704+
(mmu_asid != (mm_asid & MM_CTXT_ASID_MASK))))
713705
print_asid_mismatch(mm_asid, mmu_asid, 0);
714706
}
715707
#endif

‎arch/arc/mm/tlbex.S‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,15 @@ ex_saved_reg1:
140140
GET_CURR_TASK_ON_CPU r3
141141
ld r0, [r3, TASK_ACT_MM]
142142
ld r0, [r0, MM_CTXT+MM_CTXT_ASID]
143+
breq r0, 0, 55f ; Error if no ASID allocated
143144

144145
lr r1, [ARC_REG_PID]
145146
and r1, r1, 0xFF
146147

147-
breq r1, r0, 5f
148+
and r2, r0, 0xFF ; MMU PID bits only for comparison
149+
breq r1, r2, 5f
148150

151+
55:
149152
; Error if H/w and S/w ASID don't match, but NOT if in kernel mode
150153
lr r2, [erstatus]
151154
bbit0 r2, STATUS_U_BIT, 5f

0 commit comments

Comments
 (0)