From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1F2C3C433EF for ; Fri, 8 Jul 2022 14:03:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 94A096B0072; Fri, 8 Jul 2022 10:03:36 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8F9056B0073; Fri, 8 Jul 2022 10:03:36 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7C1F96B0074; Fri, 8 Jul 2022 10:03:36 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 6D87B6B0072 for ; Fri, 8 Jul 2022 10:03:36 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 3B4D260640 for ; Fri, 8 Jul 2022 14:03:36 +0000 (UTC) X-FDA: 79664100432.01.ADEC63D Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) by imf31.hostedemail.com (Postfix) with ESMTP id 3854E2005F for ; Fri, 8 Jul 2022 14:03:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=s+EQgSxpahsYAaya4GvQorTV7f/1MGWOK/61uO29yIE=; b=hPsai+AxuTgYZlDVehKcaTAQk2 HFVsub9E1A10zLvA0c416AM6Ik8JARZjyNbbWpZd8Debivja/CIGUkIbreYZB2GuMLTI6dn3ZkJBr g0te6bWnOddM6PjYnJqBZVbvscki8TEcmZfIbSYnh0VXIwzJl+nnFEBjuER8yXBVm0KC/AY7rFB0s iJlgZ172dIETrlDg4DIiDY49u4qPtsIgAD6v2eYlLrW0joj+BzY4nNnLX5kvo4A6Rk9mT8J1IR9gD Dd2M+qAYBFRl5TH7y2c5VnsG/+ioMruiQM50HFnQ+qPywFVhZhBLV57T3tmb1IXEvPptPBOaxs7j7 tlc74pbg==; Received: from j130084.upc-j.chello.nl ([24.132.130.84] helo=worktop.programming.kicks-ass.net) by desiato.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1o9oZi-001kyL-BT; Fri, 08 Jul 2022 14:03:18 +0000 Received: by worktop.programming.kicks-ass.net (Postfix, from userid 1000) id C296198007C; Fri, 8 Jul 2022 16:03:17 +0200 (CEST) Date: Fri, 8 Jul 2022 16:03:17 +0200 From: Peter Zijlstra To: Will Deacon 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: References: <20220708071802.751003711@infradead.org> <20220708071834.149930530@infradead.org> <20220708133605.GE5989@willie-the-truck> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220708133605.GE5989@willie-the-truck> ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1657289013; a=rsa-sha256; cv=none; b=H4xFLaD1e/sxjiBT6UCjv9kSUfVMWt0WyB/YFG5PhthYUc2K8qPvyTX1ZV1WcG7fW49yTz zddwJlrjCDou7X2c8DXoTRXIJ4HiVIe5WQTIWmPtgw50mO5uph7XDZ73MrrJsZglj07z9H NqqWzK1XCwCLdoge0uIB72avkD8v8HQ= ARC-Authentication-Results: i=1; imf31.hostedemail.com; dkim=pass header.d=infradead.org header.s=desiato.20200630 header.b=hPsai+Ax; dmarc=none; spf=none (imf31.hostedemail.com: domain of peterz@infradead.org has no SPF policy when checking 90.155.92.199) smtp.mailfrom=peterz@infradead.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1657289013; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=s+EQgSxpahsYAaya4GvQorTV7f/1MGWOK/61uO29yIE=; b=xnAzkjfbP9AYOTck5OBMmU0q6lxbtYVGhHy4Zkm3VvkYMAqZiBZShQU2PDrmPKCeZteaeT ZPo1SszC6+JK2zJdQh3gdEMbbWOATtmrs5VixPGRl5/Vo0PHvsU+JTyaUL2EHojG5ho9ET nwOiOYW9EZkZiqUz8zaCrynKv4VbddM= X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 3854E2005F Authentication-Results: imf31.hostedemail.com; dkim=pass header.d=infradead.org header.s=desiato.20200630 header.b=hPsai+Ax; dmarc=none; spf=none (imf31.hostedemail.com: domain of peterz@infradead.org has no SPF policy when checking 90.155.92.199) smtp.mailfrom=peterz@infradead.org X-Stat-Signature: pmam4s8xt4ef7sai8rno53unsyj1kha8 X-Rspam-User: X-HE-Tag: 1657289012-493710 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Fri, Jul 08, 2022 at 02:36:06PM +0100, Will Deacon wrote: > On Fri, Jul 08, 2022 at 09:18:06AM +0200, Peter Zijlstra wrote: > > @@ -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. Duh, that's just me being daft again. For some raisin I was convinced (and failed to check) that we only had the vma at start. I can easily respin this to not need the extra variable. How's this then? --- Subject: mmu_gather: Force tlb-flush VM_PFNMAP vmas From: Peter Zijlstra Date: Thu Jul 7 11:51:16 CEST 2022 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 | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) --- a/include/asm-generic/tlb.h +++ b/include/asm-generic/tlb.h @@ -507,16 +507,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 ((vma->vm_flags & VM_PFNMAP) || !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); + } } /*