Skip to content

Commit c28b274

Browse files
committed
openrisc: Implement proper SMP tlb flushing
Up until now when flushing pages from the TLB on SMP OpenRISC was always resorting to flush the entire TLB on all CPUs. This patch adds the mechanics for flushing specific ranges and pages based on the usage. The function switch_mm is updated to account for cpu usage by updating mm_struct's cpumask. This is used in the SMP flush routines. This mostly follows the riscv implementation. Signed-off-by: Stafford Horne <[email protected]>
1 parent 57b8e27 commit c28b274

File tree

2 files changed

+89
-13
lines changed

2 files changed

+89
-13
lines changed

arch/openrisc/kernel/smp.c

Lines changed: 77 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -219,30 +219,99 @@ static inline void ipi_flush_tlb_all(void *ignored)
219219
local_flush_tlb_all();
220220
}
221221

222+
static inline void ipi_flush_tlb_mm(void *info)
223+
{
224+
struct mm_struct *mm = (struct mm_struct *)info;
225+
226+
local_flush_tlb_mm(mm);
227+
}
228+
229+
static void smp_flush_tlb_mm(struct cpumask *cmask, struct mm_struct *mm)
230+
{
231+
unsigned int cpuid;
232+
233+
if (cpumask_empty(cmask))
234+
return;
235+
236+
cpuid = get_cpu();
237+
238+
if (cpumask_any_but(cmask, cpuid) >= nr_cpu_ids) {
239+
/* local cpu is the only cpu present in cpumask */
240+
local_flush_tlb_mm(mm);
241+
} else {
242+
on_each_cpu_mask(cmask, ipi_flush_tlb_mm, mm, 1);
243+
}
244+
put_cpu();
245+
}
246+
247+
struct flush_tlb_data {
248+
unsigned long addr1;
249+
unsigned long addr2;
250+
};
251+
252+
static inline void ipi_flush_tlb_page(void *info)
253+
{
254+
struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
255+
256+
local_flush_tlb_page(NULL, fd->addr1);
257+
}
258+
259+
static inline void ipi_flush_tlb_range(void *info)
260+
{
261+
struct flush_tlb_data *fd = (struct flush_tlb_data *)info;
262+
263+
local_flush_tlb_range(NULL, fd->addr1, fd->addr2);
264+
}
265+
266+
static void smp_flush_tlb_range(struct cpumask *cmask, unsigned long start,
267+
unsigned long end)
268+
{
269+
unsigned int cpuid;
270+
271+
if (cpumask_empty(cmask))
272+
return;
273+
274+
cpuid = get_cpu();
275+
276+
if (cpumask_any_but(cmask, cpuid) >= nr_cpu_ids) {
277+
/* local cpu is the only cpu present in cpumask */
278+
if ((end - start) <= PAGE_SIZE)
279+
local_flush_tlb_page(NULL, start);
280+
else
281+
local_flush_tlb_range(NULL, start, end);
282+
} else {
283+
struct flush_tlb_data fd;
284+
285+
fd.addr1 = start;
286+
fd.addr2 = end;
287+
288+
if ((end - start) <= PAGE_SIZE)
289+
on_each_cpu_mask(cmask, ipi_flush_tlb_page, &fd, 1);
290+
else
291+
on_each_cpu_mask(cmask, ipi_flush_tlb_range, &fd, 1);
292+
}
293+
put_cpu();
294+
}
295+
222296
void flush_tlb_all(void)
223297
{
224298
on_each_cpu(ipi_flush_tlb_all, NULL, 1);
225299
}
226300

227-
/*
228-
* FIXME: implement proper functionality instead of flush_tlb_all.
229-
* *But*, as things currently stands, the local_tlb_flush_* functions will
230-
* all boil down to local_tlb_flush_all anyway.
231-
*/
232301
void flush_tlb_mm(struct mm_struct *mm)
233302
{
234-
on_each_cpu(ipi_flush_tlb_all, NULL, 1);
303+
smp_flush_tlb_mm(mm_cpumask(mm), mm);
235304
}
236305

237306
void flush_tlb_page(struct vm_area_struct *vma, unsigned long uaddr)
238307
{
239-
on_each_cpu(ipi_flush_tlb_all, NULL, 1);
308+
smp_flush_tlb_range(mm_cpumask(vma->vm_mm), uaddr, uaddr + PAGE_SIZE);
240309
}
241310

242311
void flush_tlb_range(struct vm_area_struct *vma,
243312
unsigned long start, unsigned long end)
244313
{
245-
on_each_cpu(ipi_flush_tlb_all, NULL, 1);
314+
smp_flush_tlb_range(mm_cpumask(vma->vm_mm), start, end);
246315
}
247316

248317
/* Instruction cache invalidate - performed on each cpu */

arch/openrisc/mm/tlb.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,21 +137,28 @@ void local_flush_tlb_mm(struct mm_struct *mm)
137137
void switch_mm(struct mm_struct *prev, struct mm_struct *next,
138138
struct task_struct *next_tsk)
139139
{
140+
unsigned int cpu;
141+
142+
if (unlikely(prev == next))
143+
return;
144+
145+
cpu = smp_processor_id();
146+
147+
cpumask_clear_cpu(cpu, mm_cpumask(prev));
148+
cpumask_set_cpu(cpu, mm_cpumask(next));
149+
140150
/* remember the pgd for the fault handlers
141151
* this is similar to the pgd register in some other CPU's.
142152
* we need our own copy of it because current and active_mm
143153
* might be invalid at points where we still need to derefer
144154
* the pgd.
145155
*/
146-
current_pgd[smp_processor_id()] = next->pgd;
156+
current_pgd[cpu] = next->pgd;
147157

148158
/* We don't have context support implemented, so flush all
149159
* entries belonging to previous map
150160
*/
151-
152-
if (prev != next)
153-
local_flush_tlb_mm(prev);
154-
161+
local_flush_tlb_mm(prev);
155162
}
156163

157164
/*

0 commit comments

Comments
 (0)