Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp532947imw; Fri, 8 Jul 2022 07:17:14 -0700 (PDT) X-Google-Smtp-Source: AGRyM1v0g+tUYe5fh2ZyYcFZ72Dp4QwkSdANz+WVhE5bjJgbRqHW1Thux0HcU0e1BWgjfoP/5BOx X-Received: by 2002:a17:90b:1650:b0:1ec:b5e7:42ae with SMTP id il16-20020a17090b165000b001ecb5e742aemr54749pjb.15.1657289833932; Fri, 08 Jul 2022 07:17:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1657289833; cv=none; d=google.com; s=arc-20160816; b=CZiAxpWy5RkCnsw4TsV4OXG3xQHvU0U8iYkNP6EaizpzXrqG2vAzg4tQcGSoKrzVt2 3sFIfmRBPn9q08pmM2nMRbiVhly98vfsbiu7KTicCntI2g/PrHDvce3q5Xqr4vZ+VbmN wvTff8eZKlqKkE8IIISHUSLq7+ZSYtLoxSGSTiQ1ka2lzxwyqXuEuNX70GHl3EQvB0WC W5TuiNKf3ehUNwDikguY4LEgDc/jo/jU0HWptzpA1E0SauiRqIh0M47d2nZbH+fEkC71 14BVPq98YUchot7Q/zO3jxbg4T4v4JETk7tDP7tnGl6y+xyytua3GyJ7DMMRI3mmOscX Xk3A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=aX4YNGv499DhZQ6Cjk1GxbdGQqLxUKEtJDjQZz2FVKo=; b=KYHE4bMwtKyii/aPJ0ZLcBf9ueCeTL1C0Gm03Sj2QTtGFkXg3xmnzNBnN6UkckeiJj J3XD6vzdIe3Z7AMxYfM5MIkE3KGR5cKqosCWsCgcKA+GE9yQju96r4zO4ZvZWKoVnwJt FX1rorxthy3wr3wlqeSI5mWGNEbziHO/lwnmwuCPJFWre1tt+71dIN617kAlvR04Ftl8 nxSj/HAhVYqMMWC5kMGOdCKBoPykEUEIv9CcXs3dhpDB/Y0GI8XyAGAiYrqkJDllzmj3 1hfmv8M76f8jIAyqgPlujw9l0xWLnDZnIlHXPALjzGz08wbSLafgC+ZaoyHl6ajJ1huo mkAA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Z9emsyrJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c14-20020a170903234e00b0016be4d63bebsi987321plh.217.2022.07.08.07.17.02; Fri, 08 Jul 2022 07:17:13 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Z9emsyrJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S238227AbiGHNgQ (ORCPT + 99 others); Fri, 8 Jul 2022 09:36:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42716 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238123AbiGHNgP (ORCPT ); Fri, 8 Jul 2022 09:36:15 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 29FAD15A0B for ; Fri, 8 Jul 2022 06:36:14 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id D84F0B827E8 for ; Fri, 8 Jul 2022 13:36:12 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A5914C341C6; Fri, 8 Jul 2022 13:36:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1657287371; bh=o2KSwDFs5DfzOXVzIO7vzMubdIpBYjtEzTcmGGuDQyI=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=Z9emsyrJrYTYbSfBVr+Ws5JU7z4eB3w2za9nZYxTI+Eul1U0YNktpPkELUusEJCts OFB2FU8y310puqbeI8zWMcmNqNz96CwETZ2rG6CaFctWB9UltUTMSQ4f/rvm97zCMs VjokYr5Ieyou51WfxzsGjpGwOtoteEaJl92G52H0hFHDetMR0e+8+6k7aOnWhm5jMr y4dzgmtTWZk2QPrHeZjMjuEbeWUoM3QLqcfpBq5KrnP7MDDizerTj65LBrO9J7Aopm dPniDSAIXpcA2kYCeHduKX+iY8hwdf++4LaWfbahVpJfJrzuj6BT6RTYHg5ZqUbQb3 TAVEbDFS+7z5Q== Date: Fri, 8 Jul 2022 14:36:06 +0100 From: Will Deacon To: Peter Zijlstra Cc: Jann Horn , Linus Torvalds , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Dave Airlie , Daniel Vetter , Andrew Morton , Guo Ren , David Miller Subject: Re: [PATCH 4/4] mmu_gather: Force tlb-flush VM_PFNMAP vmas Message-ID: <20220708133605.GE5989@willie-the-truck> References: <20220708071802.751003711@infradead.org> <20220708071834.149930530@infradead.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220708071834.149930530@infradead.org> User-Agent: Mutt/1.10.1 (2018-07-13) X-Spam-Status: No, score=-7.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Jul 08, 2022 at 09:18:06AM +0200, Peter Zijlstra wrote: > Jann reported a race between munmap() and unmap_mapping_range(), where > unmap_mapping_range() will no-op once unmap_vmas() has unlinked the > VMA; however munmap() will not yet have invalidated the TLBs. > > Therefore unmap_mapping_range() will complete while there are still > (stale) TLB entries for the specified range. > > Mitigate this by force flushing TLBs for VM_PFNMAP ranges. > > Signed-off-by: Peter Zijlstra (Intel) > --- > include/asm-generic/tlb.h | 33 +++++++++++++++++---------------- > 1 file changed, 17 insertions(+), 16 deletions(-) > > --- a/include/asm-generic/tlb.h > +++ b/include/asm-generic/tlb.h > @@ -303,6 +303,7 @@ struct mmu_gather { > */ > unsigned int vma_exec : 1; > unsigned int vma_huge : 1; > + unsigned int vma_pfn : 1; > > unsigned int batch_count; > > @@ -373,7 +374,6 @@ tlb_update_vma_flags(struct mmu_gather * > #else /* CONFIG_MMU_GATHER_NO_RANGE */ > > #ifndef tlb_flush > - > /* > * When an architecture does not provide its own tlb_flush() implementation > * but does have a reasonably efficient flush_vma_range() implementation > @@ -393,6 +393,9 @@ static inline void tlb_flush(struct mmu_ > flush_tlb_range(&vma, tlb->start, tlb->end); > } > } > +#endif > + > +#endif /* CONFIG_MMU_GATHER_NO_RANGE */ > > static inline void > tlb_update_vma_flags(struct mmu_gather *tlb, struct vm_area_struct *vma) > @@ -410,17 +413,9 @@ tlb_update_vma_flags(struct mmu_gather * > */ > tlb->vma_huge = is_vm_hugetlb_page(vma); > tlb->vma_exec = !!(vma->vm_flags & VM_EXEC); > + tlb->vma_pfn = !!(vma->vm_flags & VM_PFNMAP); > } > > -#else > - > -static inline void > -tlb_update_vma_flags(struct mmu_gather *tlb, struct vm_area_struct *vma) { } > - > -#endif > - > -#endif /* CONFIG_MMU_GATHER_NO_RANGE */ > - > static inline void tlb_flush_mmu_tlbonly(struct mmu_gather *tlb) > { > /* > @@ -507,16 +502,22 @@ static inline void tlb_start_vma(struct > > static inline void tlb_end_vma(struct mmu_gather *tlb, struct vm_area_struct *vma) > { > - if (tlb->fullmm || IS_ENABLED(CONFIG_MMU_GATHER_MERGE_VMAS)) > + if (tlb->fullmm) > return; > > /* > - * Do a TLB flush and reset the range at VMA boundaries; this avoids > - * the ranges growing with the unused space between consecutive VMAs, > - * but also the mmu_gather::vma_* flags from tlb_start_vma() rely on > - * this. > + * VM_PFNMAP is more fragile because the core mm will not track the > + * page mapcount -- there might not be page-frames for these PFNs after > + * all. Force flush TLBs for such ranges to avoid munmap() vs > + * unmap_mapping_range() races. > */ > - tlb_flush_mmu_tlbonly(tlb); > + if (tlb->vma_pfn || !IS_ENABLED(CONFIG_MMU_GATHER_MERGE_VMAS)) { > + /* > + * Do a TLB flush and reset the range at VMA boundaries; this avoids > + * the ranges growing with the unused space between consecutive VMAs. > + */ > + tlb_flush_mmu_tlbonly(tlb); > + } We already have the vma here, so I'm not sure how much the new 'vma_pfn' field really buys us over checking the 'vm_flags', but perhaps that's cleanup for another day. Acked-by: Will Deacon Will