Skip to content

Commit e73ad5f

Browse files
amlutoIngo Molnar
authored andcommitted
mm, x86/mm: Make the batched unmap TLB flush API more generic
try_to_unmap_flush() used to open-code a rather x86-centric flush sequence: local_flush_tlb() + flush_tlb_others(). Rearrange the code so that the arch (only x86 for now) provides arch_tlbbatch_add_mm() and arch_tlbbatch_flush() and the core code calls those functions instead. I'll want this for x86 because, to enable address space ids, I can't support the flush_tlb_others() mode used by exising try_to_unmap_flush() implementation with good performance. I can support the new API fairly easily, though. I imagine that other architectures may be in a similar position. Architectures with strong remote flush primitives (arm64?) may have even worse performance problems with flush_tlb_others() the way that try_to_unmap_flush() uses it. Signed-off-by: Andy Lutomirski <luto@kernel.org> Acked-by: Kees Cook <keescook@chromium.org> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Borislav Petkov <bpetkov@suse.de> Cc: Dave Hansen <dave.hansen@intel.com> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Mel Gorman <mgorman@suse.de> Cc: Michal Hocko <mhocko@suse.com> Cc: Nadav Amit <nadav.amit@gmail.com> Cc: Nadav Amit <namit@vmware.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Rik van Riel <riel@redhat.com> Cc: Sasha Levin <sasha.levin@oracle.com> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: linux-mm@kvack.org Link: http://lkml.kernel.org/r/19f25a8581f9fb77876b7ff3b001f89835e34ea3.1495492063.git.luto@kernel.org Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent b3b90e5 commit e73ad5f

File tree

5 files changed

+54
-18
lines changed

5 files changed

+54
-18
lines changed

arch/x86/include/asm/tlbbatch.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef _ARCH_X86_TLBBATCH_H
2+
#define _ARCH_X86_TLBBATCH_H
3+
4+
#include <linux/cpumask.h>
5+
6+
#ifdef CONFIG_SMP
7+
struct arch_tlbflush_unmap_batch {
8+
/*
9+
* Each bit set is a CPU that potentially has a TLB entry for one of
10+
* the PFNs being flushed..
11+
*/
12+
struct cpumask cpumask;
13+
};
14+
#endif
15+
16+
#endif /* _ARCH_X86_TLBBATCH_H */

arch/x86/include/asm/tlbflush.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,14 @@ static inline void reset_lazy_tlbstate(void)
329329
this_cpu_write(cpu_tlbstate.active_mm, &init_mm);
330330
}
331331

332+
static inline void arch_tlbbatch_add_mm(struct arch_tlbflush_unmap_batch *batch,
333+
struct mm_struct *mm)
334+
{
335+
cpumask_or(&batch->cpumask, &batch->cpumask, mm_cpumask(mm));
336+
}
337+
338+
extern void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch);
339+
332340
#endif /* SMP */
333341

334342
#ifndef CONFIG_PARAVIRT

arch/x86/mm/tlb.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,23 @@ void flush_tlb_kernel_range(unsigned long start, unsigned long end)
395395
}
396396
}
397397

398+
void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch)
399+
{
400+
int cpu = get_cpu();
401+
402+
if (cpumask_test_cpu(cpu, &batch->cpumask)) {
403+
count_vm_tlb_event(NR_TLB_LOCAL_FLUSH_ALL);
404+
local_flush_tlb();
405+
trace_tlb_flush(TLB_LOCAL_SHOOTDOWN, TLB_FLUSH_ALL);
406+
}
407+
408+
if (cpumask_any_but(&batch->cpumask, cpu) < nr_cpu_ids)
409+
flush_tlb_others(&batch->cpumask, NULL, 0, TLB_FLUSH_ALL);
410+
cpumask_clear(&batch->cpumask);
411+
412+
put_cpu();
413+
}
414+
398415
static ssize_t tlbflush_read_file(struct file *file, char __user *user_buf,
399416
size_t count, loff_t *ppos)
400417
{

include/linux/mm_types_task.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414

1515
#include <asm/page.h>
1616

17+
#ifdef CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH
18+
#include <asm/tlbbatch.h>
19+
#endif
20+
1721
#define USE_SPLIT_PTE_PTLOCKS (NR_CPUS >= CONFIG_SPLIT_PTLOCK_CPUS)
1822
#define USE_SPLIT_PMD_PTLOCKS (USE_SPLIT_PTE_PTLOCKS && \
1923
IS_ENABLED(CONFIG_ARCH_ENABLE_SPLIT_PMD_PTLOCK))
@@ -67,12 +71,15 @@ struct page_frag {
6771
struct tlbflush_unmap_batch {
6872
#ifdef CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH
6973
/*
70-
* Each bit set is a CPU that potentially has a TLB entry for one of
71-
* the PFNs being flushed. See set_tlb_ubc_flush_pending().
74+
* The arch code makes the following promise: generic code can modify a
75+
* PTE, then call arch_tlbbatch_add_mm() (which internally provides all
76+
* needed barriers), then call arch_tlbbatch_flush(), and the entries
77+
* will be flushed on all CPUs by the time that arch_tlbbatch_flush()
78+
* returns.
7279
*/
73-
struct cpumask cpumask;
80+
struct arch_tlbflush_unmap_batch arch;
7481

75-
/* True if any bit in cpumask is set */
82+
/* True if a flush is needed. */
7683
bool flush_required;
7784

7885
/*

mm/rmap.c

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -579,25 +579,13 @@ void page_unlock_anon_vma_read(struct anon_vma *anon_vma)
579579
void try_to_unmap_flush(void)
580580
{
581581
struct tlbflush_unmap_batch *tlb_ubc = &current->tlb_ubc;
582-
int cpu;
583582

584583
if (!tlb_ubc->flush_required)
585584
return;
586585

587-
cpu = get_cpu();
588-
589-
if (cpumask_test_cpu(cpu, &tlb_ubc->cpumask)) {
590-
count_vm_tlb_event(NR_TLB_LOCAL_FLUSH_ALL);
591-
local_flush_tlb();
592-
trace_tlb_flush(TLB_LOCAL_SHOOTDOWN, TLB_FLUSH_ALL);
593-
}
594-
595-
if (cpumask_any_but(&tlb_ubc->cpumask, cpu) < nr_cpu_ids)
596-
flush_tlb_others(&tlb_ubc->cpumask, NULL, 0, TLB_FLUSH_ALL);
597-
cpumask_clear(&tlb_ubc->cpumask);
586+
arch_tlbbatch_flush(&tlb_ubc->arch);
598587
tlb_ubc->flush_required = false;
599588
tlb_ubc->writable = false;
600-
put_cpu();
601589
}
602590

603591
/* Flush iff there are potentially writable TLB entries that can race with IO */
@@ -613,7 +601,7 @@ static void set_tlb_ubc_flush_pending(struct mm_struct *mm, bool writable)
613601
{
614602
struct tlbflush_unmap_batch *tlb_ubc = &current->tlb_ubc;
615603

616-
cpumask_or(&tlb_ubc->cpumask, &tlb_ubc->cpumask, mm_cpumask(mm));
604+
arch_tlbbatch_add_mm(&tlb_ubc->arch, mm);
617605
tlb_ubc->flush_required = true;
618606

619607
/*

0 commit comments

Comments
 (0)