Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752795AbdFRIGZ (ORCPT ); Sun, 18 Jun 2017 04:06:25 -0400 Received: from mail-pg0-f66.google.com ([74.125.83.66]:32820 "EHLO mail-pg0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750968AbdFRIGU (ORCPT ); Sun, 18 Jun 2017 04:06:20 -0400 Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 10.3 \(3273\)) Subject: Re: [PATCH v2 05/10] x86/mm: Rework lazy TLB mode and TLB freshness tracking From: Nadav Amit In-Reply-To: <039935bc914009103fdaa6f72f14980c19562de5.1497415951.git.luto@kernel.org> Date: Sun, 18 Jun 2017 01:06:15 -0700 Cc: X86 ML , LKML , Borislav Petkov , Linus Torvalds , Andrew Morton , Mel Gorman , "linux-mm@kvack.org" , Rik van Riel , Dave Hansen , Arjan van de Ven , Peter Zijlstra , Andrew Banman , Mike Travis , Dimitri Sivanich , Juergen Gross , Boris Ostrovsky Message-Id: <515383DE-922D-4278-9FF6-AEF5445A0547@gmail.com> References: <039935bc914009103fdaa6f72f14980c19562de5.1497415951.git.luto@kernel.org> To: Andy Lutomirski X-Mailer: Apple Mail (2.3273) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: 8bit X-MIME-Autoconverted: from quoted-printable to 8bit by mail.home.local id v5I86ZJl015933 Content-Length: 2949 Lines: 65 > On Jun 13, 2017, at 9:56 PM, Andy Lutomirski wrote: > > x86's lazy TLB mode used to be fairly weak -- it would switch to > init_mm the first time it tried to flush a lazy TLB. This meant an > unnecessary CR3 write and, if the flush was remote, an unnecessary > IPI. > > Rewrite it entirely. When we enter lazy mode, we simply remove the > cpu from mm_cpumask. This means that we need a way to figure out > whether we've missed a flush when we switch back out of lazy mode. > I use the tlb_gen machinery to track whether a context is up to > date. > > Note to reviewers: this patch, my itself, looks a bit odd. I'm > using an array of length 1 containing (ctx_id, tlb_gen) rather than > just storing tlb_gen, and making it at array isn't necessary yet. > I'm doing this because the next few patches add PCID support, and, > with PCID, we need ctx_id, and the array will end up with a length > greater than 1. Making it an array now means that there will be > less churn and therefore less stress on your eyeballs. > > NB: This is dubious but, AFAICT, still correct on Xen and UV. > xen_exit_mmap() uses mm_cpumask() for nefarious purposes and this > patch changes the way that mm_cpumask() works. This should be okay, > since Xen *also* iterates all online CPUs to find all the CPUs it > needs to twiddle. > > The UV tlbflush code is rather dated and should be changed. > > Cc: Andrew Banman > Cc: Mike Travis > Cc: Dimitri Sivanich > Cc: Juergen Gross > Cc: Boris Ostrovsky > Signed-off-by: Andy Lutomirski > --- > arch/x86/include/asm/mmu_context.h | 6 +- > arch/x86/include/asm/tlbflush.h | 4 - > arch/x86/mm/init.c | 1 - > arch/x86/mm/tlb.c | 242 +++++++++++++++++++------------------ > 4 files changed, 131 insertions(+), 122 deletions(-) > > diff --git a/arch/x86/include/asm/mmu_context.h b/arch/x86/include/asm/mmu_context.h > index e5295d485899..69a4f1ee86ac 100644 > --- a/arch/x86/include/asm/mmu_context.h > +++ b/arch/x86/include/asm/mmu_context.h > @@ -125,8 +125,10 @@ static inline void switch_ldt(struct mm_struct *prev, struct mm_struct *next) > > static inline void enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk) > { > - if (this_cpu_read(cpu_tlbstate.state) == TLBSTATE_OK) > - this_cpu_write(cpu_tlbstate.state, TLBSTATE_LAZY); > + int cpu = smp_processor_id(); > + > + if (cpumask_test_cpu(cpu, mm_cpumask(mm))) > + cpumask_clear_cpu(cpu, mm_cpumask(mm)); The indication for laziness that was in cpu_tlbstate.state may be a better indication whether the cpu needs to be cleared from the previous mm_cpumask(). If you kept this indication, you could have used this per-cpu information in switch_mm_irqs_off() instead of "cpumask_test_cpu(cpu, mm_cpumask(next))”, which might have been accessed by another core.