Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp6431456imm; Mon, 27 Aug 2018 16:03:51 -0700 (PDT) X-Google-Smtp-Source: ANB0VdZtaUe+B0T4xOBToPCK2uWQk7HTcldoQ20QKn/BxeLDAhE41ZVXMgH1p/1AlzS1MyBu42+G X-Received: by 2002:a63:bd01:: with SMTP id a1-v6mr13998394pgf.12.1535411031884; Mon, 27 Aug 2018 16:03:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1535411031; cv=none; d=google.com; s=arc-20160816; b=QQky6fHDXjMcjYFVVhCrMpt1WVf5XXTayTYc+ZyhzL0wh8DJoTjfIjrn0DFROqdSfh FkYT94Hb/KbfjBP5pPKvSKCqWGE0/K7av0vGGD+TICLEnsOneJg1DMHmUDUjF1/x1oDS JzKsCnzVyB/Cdf+0aS1wIBAlodCtihNtOAPKZH/kB+t+m4/I2+7d0TG2wZqsChkMPc4T Reo9xoAHT/CyuSxg09IyDHKmbG/+Rq2Uyf9U21YNy80kQpkwn7qW6hRwapKE3JDG4Aak gREHsMvoouJu9d/nBkSM0lNMTJo0Im2K3golNh67nyV+bzu2kLIkZDDl64z7nZrfgH7d fIwg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:cc:to:subject :message-id:date:from:references:in-reply-to:mime-version :dkim-signature:arc-authentication-results; bh=jCEDkBYd2+Hsj4IQv4yRgqyEwEogkW/pE5eStqWG8bk=; b=LuBSfoJROOTZU8o/wC69Fcq72/zRUKpi/OfjPOjDh0F9MFPjjipzQSEhEV8nZ5ovH0 n3abrdfFXM94UrEiLPuQjvM7D8mJr5EySgjLQZy/FgPz4WMaHDLPS7HlBaNPK2roDfhC mZ7Um63CqN7YTz7VQEg3/oqsLgEkQy1vpZQm43mLhw+7zIxp5axsWaO3qVOSwoSMeG1S rpjpEFMDXr/aL7dqsed9Utxp+yfAz0QgXwrlTb7ihSf8Hq5Dc3ht/jCBRD/A6/UO7NhY yP0fLNqxzGtosV+IUBv/dDleko++n2YliT9YutFR4prmk+pt/BsYvlH7eff/zIfFoB5c lrGQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="I/D5l8pg"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id q23-v6si464624pgq.483.2018.08.27.16.03.36; Mon, 27 Aug 2018 16:03:51 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="I/D5l8pg"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727516AbeH1Cuj (ORCPT + 99 others); Mon, 27 Aug 2018 22:50:39 -0400 Received: from mail.kernel.org ([198.145.29.99]:60554 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727058AbeH1Cui (ORCPT ); Mon, 27 Aug 2018 22:50:38 -0400 Received: from mail-wr1-f47.google.com (mail-wr1-f47.google.com [209.85.221.47]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id CDD21208B4 for ; Mon, 27 Aug 2018 23:01:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1535410915; bh=yBNbRsrm/nIsj/x/P4L7Pnb5oLNw+/dnFbDZg9BDkOY=; h=In-Reply-To:References:From:Date:Subject:To:Cc:From; b=I/D5l8pg5DM+yLgmMITgKjmmatUvqEg2bsLYUW81eqB0KTOD2LgaqGHiaWPA1ZrMN c+aHrlfH/gAxwjl5Mf/OAme0bDISgMH3xN3lWiJoHHOFzeheAbIke56EkhJ0Eczovb sNPzKcI+VBLgt2M1KqRFc82uFHYW6l3WlbFFl7+k= Received: by mail-wr1-f47.google.com with SMTP id w11-v6so508091wrc.5 for ; Mon, 27 Aug 2018 16:01:54 -0700 (PDT) X-Gm-Message-State: APzg51DLpLh6s2f+64DLT2qTmHEIBEL4KMCZt7gYNq4XALMz03JWT6Ak lK1Dkx6z0gFbCoB9RPug4+f00busXLgg4zon+EOXuw== X-Received: by 2002:adf:dcc1:: with SMTP id x1-v6mr10311869wrm.21.1535410913345; Mon, 27 Aug 2018 16:01:53 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:a1c:548:0:0:0:0:0 with HTTP; Mon, 27 Aug 2018 16:01:32 -0700 (PDT) In-Reply-To: <2998E63A-5663-4805-9DE7-829A5870AA6D@gmail.com> References: <20180824180438.GS24124@hirez.programming.kicks-ass.net> <56A9902F-44BE-4520-A17C-26650FCC3A11@gmail.com> <9A38D3F4-2F75-401D-8B4D-83A844C9061B@gmail.com> <8E0D8C66-6F21-4890-8984-B6B3082D4CC5@gmail.com> <20180826112341.f77a528763e297cbc36058fa@kernel.org> <20180826090958.GT24124@hirez.programming.kicks-ass.net> <20180827120305.01a6f26267c64610cadec5d8@kernel.org> <4BF82052-4738-441C-8763-26C85003F2C9@gmail.com> <20180827170511.6bafa15cbc102ae135366e86@kernel.org> <01DA0BDD-7504-4209-8A8F-20B27CF6A1C7@gmail.com> <0000D631-FDDF-4273-8F3C-714E6825E59B@gmail.com> <823D916E-4056-4A36-BDD8-0FB682A8DCAE@gmail.com> <08A6BCB2-66C2-47ED-AEB8-AA8F4D7DBD45@gmail.com> <4F72D40A-25A3-4C64-B8DD-56970CFDE61E@gmail.com> <2998E63A-5663-4805-9DE7-829A5870AA6D@gmail.com> From: Andy Lutomirski Date: Mon, 27 Aug 2018 16:01:32 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: TLB flushes on fixmap changes To: Nadav Amit Cc: Andy Lutomirski , Masami Hiramatsu , Peter Zijlstra , Kees Cook , Linus Torvalds , Paolo Bonzini , Jiri Kosina , Will Deacon , Benjamin Herrenschmidt , Nick Piggin , "the arch/x86 maintainers" , Borislav Petkov , Rik van Riel , Jann Horn , Adin Scannell , Dave Hansen , Linux Kernel Mailing List , linux-mm , David Miller , Martin Schwidefsky , Michael Ellerman Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Aug 27, 2018 at 3:54 PM, Nadav Amit wrote: > at 3:32 PM, Andy Lutomirski wrote: > >> On Mon, Aug 27, 2018 at 2:55 PM, Nadav Amit wrote= : >>> at 1:16 PM, Nadav Amit wrote: >>> >>>> at 12:58 PM, Andy Lutomirski wrote: >>>> >>>>> On Mon, Aug 27, 2018 at 12:43 PM, Nadav Amit w= rote: >>>>>> at 12:10 PM, Nadav Amit wrote: >>>>>> >>>>>>> at 11:58 AM, Andy Lutomirski wrote: >>>>>>> >>>>>>>> On Mon, Aug 27, 2018 at 11:54 AM, Nadav Amit wrote: >>>>>>>>>> On Mon, Aug 27, 2018 at 10:34 AM, Nadav Amit wrote: >>>>>>>>>> What do you all think? >>>>>>>>> >>>>>>>>> I agree in general. But I think that current->mm would need to be= loaded, as >>>>>>>>> otherwise I am afraid it would break switch_mm_irqs_off(). >>>>>>>> >>>>>>>> What breaks? >>>>>>> >>>>>>> Actually nothing. I just saw the IBPB stuff regarding tsk, but it s= hould not >>>>>>> matter. >>>>>> >>>>>> So here is what I got. It certainly needs some cleanup, but it boots= . >>>>>> >>>>>> Let me know how crappy you find it... >>>>>> >>>>>> >>>>>> diff --git a/arch/x86/include/asm/mmu_context.h b/arch/x86/include/a= sm/mmu_context.h >>>>>> index bbc796eb0a3b..336779650a41 100644 >>>>>> --- a/arch/x86/include/asm/mmu_context.h >>>>>> +++ b/arch/x86/include/asm/mmu_context.h >>>>>> @@ -343,4 +343,24 @@ static inline unsigned long __get_current_cr3_f= ast(void) >>>>>> return cr3; >>>>>> } >>>>>> >>>>>> +typedef struct { >>>>>> + struct mm_struct *prev; >>>>>> +} temporary_mm_state_t; >>>>>> + >>>>>> +static inline temporary_mm_state_t use_temporary_mm(struct mm_struc= t *mm) >>>>>> +{ >>>>>> + temporary_mm_state_t state; >>>>>> + >>>>>> + lockdep_assert_irqs_disabled(); >>>>>> + state.prev =3D this_cpu_read(cpu_tlbstate.loaded_mm); >>>>>> + switch_mm_irqs_off(NULL, mm, current); >>>>>> + return state; >>>>>> +} >>>>>> + >>>>>> +static inline void unuse_temporary_mm(temporary_mm_state_t prev) >>>>>> +{ >>>>>> + lockdep_assert_irqs_disabled(); >>>>>> + switch_mm_irqs_off(NULL, prev.prev, current); >>>>>> +} >>>>>> + >>>>>> #endif /* _ASM_X86_MMU_CONTEXT_H */ >>>>>> diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/p= gtable.h >>>>>> index 5715647fc4fe..ef62af9a0ef7 100644 >>>>>> --- a/arch/x86/include/asm/pgtable.h >>>>>> +++ b/arch/x86/include/asm/pgtable.h >>>>>> @@ -976,6 +976,10 @@ static inline void __meminit init_trampoline_de= fault(void) >>>>>> /* Default trampoline pgd value */ >>>>>> trampoline_pgd_entry =3D init_top_pgt[pgd_index(__PAGE_OFFSET)]= ; >>>>>> } >>>>>> + >>>>>> +void __init patching_mm_init(void); >>>>>> +#define patching_mm_init patching_mm_init >>>>>> + >>>>>> # ifdef CONFIG_RANDOMIZE_MEMORY >>>>>> void __meminit init_trampoline(void); >>>>>> # else >>>>>> diff --git a/arch/x86/include/asm/pgtable_64_types.h b/arch/x86/incl= ude/asm/pgtable_64_types.h >>>>>> index 054765ab2da2..9f44262abde0 100644 >>>>>> --- a/arch/x86/include/asm/pgtable_64_types.h >>>>>> +++ b/arch/x86/include/asm/pgtable_64_types.h >>>>>> @@ -116,6 +116,9 @@ extern unsigned int ptrs_per_p4d; >>>>>> #define LDT_PGD_ENTRY (pgtable_l5_enabled() ? LDT_PGD_ENTRY= _L5 : LDT_PGD_ENTRY_L4) >>>>>> #define LDT_BASE_ADDR (LDT_PGD_ENTRY << PGDIR_SHIFT) >>>>>> >>>>>> +#define TEXT_POKE_PGD_ENTRY -5UL >>>>>> +#define TEXT_POKE_ADDR (TEXT_POKE_PGD_ENTRY << PGDIR_SHIFT) >>>>>> + >>>>>> #define __VMALLOC_BASE_L4 0xffffc90000000000UL >>>>>> #define __VMALLOC_BASE_L5 0xffa0000000000000UL >>>>>> >>>>>> diff --git a/arch/x86/include/asm/pgtable_types.h b/arch/x86/include= /asm/pgtable_types.h >>>>>> index 99fff853c944..840c72ec8c4f 100644 >>>>>> --- a/arch/x86/include/asm/pgtable_types.h >>>>>> +++ b/arch/x86/include/asm/pgtable_types.h >>>>>> @@ -505,6 +505,9 @@ pgprot_t phys_mem_access_prot(struct file *file,= unsigned long pfn, >>>>>> /* Install a pte for a particular vaddr in kernel space. */ >>>>>> void set_pte_vaddr(unsigned long vaddr, pte_t pte); >>>>>> >>>>>> +struct mm_struct; >>>>>> +void set_mm_pte_vaddr(struct mm_struct *mm, unsigned long vaddr, pt= e_t pte); >>>>>> + >>>>>> #ifdef CONFIG_X86_32 >>>>>> extern void native_pagetable_init(void); >>>>>> #else >>>>>> diff --git a/arch/x86/include/asm/text-patching.h b/arch/x86/include= /asm/text-patching.h >>>>>> index 2ecd34e2d46c..cb364ea5b19d 100644 >>>>>> --- a/arch/x86/include/asm/text-patching.h >>>>>> +++ b/arch/x86/include/asm/text-patching.h >>>>>> @@ -38,4 +38,6 @@ extern void *text_poke(void *addr, const void *opc= ode, size_t len); >>>>>> extern int poke_int3_handler(struct pt_regs *regs); >>>>>> extern void *text_poke_bp(void *addr, const void *opcode, size_t len= , void *handler); >>>>>> >>>>>> +extern struct mm_struct *patching_mm; >>>>>> + >>>>>> #endif /* _ASM_X86_TEXT_PATCHING_H */ >>>>>> diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alterna= tive.c >>>>>> index a481763a3776..fd8a950b0d62 100644 >>>>>> --- a/arch/x86/kernel/alternative.c >>>>>> +++ b/arch/x86/kernel/alternative.c >>>>>> @@ -11,6 +11,7 @@ >>>>>> #include >>>>>> #include >>>>>> #include >>>>>> +#include >>>>>> #include >>>>>> #include >>>>>> #include >>>>>> @@ -701,8 +702,36 @@ void *text_poke(void *addr, const void *opcode,= size_t len) >>>>>> WARN_ON(!PageReserved(pages[0])); >>>>>> pages[1] =3D virt_to_page(addr + PAGE_SIZE); >>>>>> } >>>>>> - BUG_ON(!pages[0]); >>>>>> + >>>>>> local_irq_save(flags); >>>>>> + BUG_ON(!pages[0]); >>>>>> + >>>>>> + /* >>>>>> + * During initial boot, it is hard to initialize patching_mm= due to >>>>>> + * dependencies in boot order. >>>>>> + */ >>>>>> + if (patching_mm) { >>>>>> + pte_t pte; >>>>>> + temporary_mm_state_t prev; >>>>>> + >>>>>> + prev =3D use_temporary_mm(patching_mm); >>>>>> + pte =3D mk_pte(pages[0], PAGE_KERNEL); >>>>>> + set_mm_pte_vaddr(patching_mm, TEXT_POKE_ADDR, pte); >>>>>> + pte =3D mk_pte(pages[1], PAGE_KERNEL); >>>>>> + set_mm_pte_vaddr(patching_mm, TEXT_POKE_ADDR + PAGE_= SIZE, pte); >>>>>> + >>>>>> + memcpy((void *)(TEXT_POKE_ADDR | ((unsigned long)add= r & ~PAGE_MASK)), >>>>>> + opcode, len); >>>>>> + >>>>>> + set_mm_pte_vaddr(patching_mm, TEXT_POKE_ADDR, __pte(= 0)); >>>>>> + set_mm_pte_vaddr(patching_mm, TEXT_POKE_ADDR + PAGE_= SIZE, __pte(0)); >>>>>> + local_flush_tlb(); >>>>> >>>>> Hmm. This is stuff busted on SMP, and it's IMO more complicated than >>>>> needed. How about getting rid of all the weird TLB flushing stuff an= d >>>>> instead putting the mapping at vaddr - __START_KERNEL_map or whatever >>>>> it is? You *might* need to flush_tlb_mm_range() on module unload, bu= t >>>>> that's it. >>>> >>>> I don=E2=80=99t see what=E2=80=99s wrong in SMP, since this entire pie= ce of code should be >>>> running under text_mutex. >>>> >>>> I don=E2=80=99t quite understand your proposal. I really don=E2=80=99t= want to have any >>>> chance in which the page-tables for the poked address is not prealloca= ted. >>>> >>>> It is more complicated than needed, and there are redundant TLB flushe= s. The >>>> reason I preferred to do it this way, is in order not to use other fun= ctions >>>> that take locks during the software page-walk and not to duplicate exi= sting >>>> code. Yet, duplication might be the way to go. >>>> >>>>>> + sync_core(); >>>>> >>>>> I can't think of any case where sync_core() is needed. The mm switch >>>>> serializes. >>>> >>>> Good point! >>>> >>>>> Also, is there any circumstance in which any of this is used before a= t >>>>> least jump table init? All the early stuff is text_poke_early(), >>>>> right? >>>> >>>> Not before jump_label_init. However, I did not manage to get rid of th= e two >>>> code-patches in text_poke(), since text_poke is used relatively early = by >>>> x86_late_time_init(), and at this stage kmem_cache_alloc() - which is = needed >>>> to duplicate init_mm - still fails. >>> >>> Another correction: the populate_extra_pte() is not needed. >>> >>> Anyhow, if you want to do this whole thing differently, I obviously wil= l not >>> object, but I think it will end up more complicated. >>> >>> I think I finally understood your comment about "vaddr - >>> __START_KERNEL_map=E2=80=9D. I did something like that before, and it i= s not >>> super-simple. You need not only to conditionally flush the TLB, but als= o >>> to synchronize the PUD/PMD on changes. Don=E2=80=99t forget that module= memory >>> is installed even when BPF programs are installed. >>> >>> Let me know if you want me to submit cleaner patches or you want to car= ry on >>> yourself. >> >> I think your approach is a good start and should be good enough (with >> cleanups) as a fix for the bug. But I think your code has the same >> bug that we have now! You're reusing the same address on multiple >> CPUs without flushing. You can easily fix it by forcing a flush >> before loading the mm, which should be as simple as adding >> flush_tlb_mm() before you load the mm. (It won't actually flush >> anything by itself, since the mm isn't loaded, but it will update the >> bookkeeping so that switch_mm_irqs_off() flushes the mm.) > > What am I missing? > > We have a lock (text_mutex) which prevents the use of the new page-table > hierarchy on multiple CPUs. In addition we have __set_pte_vaddr() which d= oes > a local TLB flush before the lock is released and before IRQs are enabled= . > So how can the PTE be cached on multiple CPUs? > > Yes, __set_pte_vaddr() is ugly and flushes too much. I=E2=80=99ll try to = remove some > redundant TLB flushes, but these flushes are already there. I missed that set_pte_vaddr() contained a flush. It's probably nicer to use one of the APIs that doesn't imply a flush. If you end up with vm_insert_pfn() or similar, you can use can use copy_to_user() if needed :) > >> Also, please at least get rid of TEXT_POKE_ADDR. If you don't want to >> do the vaddr - __START_KERNEL_map thing, then at least pick an address >> in the low half of the address space such as 0 :) Ideally you'd only >> use this thing late enough that you could even use the normal >> insert_pfn (or similar) API for it, but that doesn't really matter. > > Perhaps the vaddr - __START_KERNEL_map actually makes sense. I was > misunderstanding it (again) before, thinking you want me to use vaddr (an= d > not the delta). I=E2=80=99ll give it a try. It does prevent easy preallocation of the intermediate tables. And do we really allow text_poke() / text_poke_bp() on BPF programs? Hmm.