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 EEC15C02181 for ; Wed, 22 Jan 2025 10:06:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 48D776B0085; Wed, 22 Jan 2025 05:06:49 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 43D196B0088; Wed, 22 Jan 2025 05:06:49 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 304C76B0089; Wed, 22 Jan 2025 05:06:49 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 0E1966B0085 for ; Wed, 22 Jan 2025 05:06:49 -0500 (EST) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id AFC16A168D for ; Wed, 22 Jan 2025 10:06:48 +0000 (UTC) X-FDA: 83034658896.12.800CCB1 Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) by imf14.hostedemail.com (Postfix) with ESMTP id 5A3E910000D for ; Wed, 22 Jan 2025 10:06:46 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=infradead.org header.s=desiato.20200630 header.b="Gb4+2Lp/"; spf=none (imf14.hostedemail.com: domain of peterz@infradead.org has no SPF policy when checking 90.155.92.199) smtp.mailfrom=peterz@infradead.org; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1737540407; 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=+4aqblxBo0mSTU8WQnlzPUPzt49g8vCbbD65eJ7VJE0=; b=SN1qAzV7KVDx29dsiwK9q32MTw4uqXu0PhtQawbkk71e25Lg2fO1w6udxlg0doyxKcgY23 TOi9ryRLzrH6drWF1QAh7K6TM7WaHiloVOkQNeyvHxdig3YbvE2ZHcp+juE18wjQfU0Hs2 uhxKDLr1Qpa0VU/yQNZxPLok+mHkSqw= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737540407; a=rsa-sha256; cv=none; b=2aJC7uAr729bmfNwfciXODGQQDoUro6aFB68JaAoJRy6oY/iuvchTxA98qyFPaEyxSXGwY HRgOPiVgUKZxQZVp+AeBzqcgz852T5/Dhd+dK62toYjL4lhziaHtOIaoO9lT1pELeh2aYr K/1+BdhFvriiAx8Hy5qiUEz+jutp+hQ= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=infradead.org header.s=desiato.20200630 header.b="Gb4+2Lp/"; spf=none (imf14.hostedemail.com: domain of peterz@infradead.org has no SPF policy when checking 90.155.92.199) smtp.mailfrom=peterz@infradead.org; dmarc=none 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=+4aqblxBo0mSTU8WQnlzPUPzt49g8vCbbD65eJ7VJE0=; b=Gb4+2Lp/lGo8WDR8KXtfHjbDfl 4F15tHMq8LG+F0bd6ZE2kGbRbnqRwftqkkoURex3D0IPLM96mSJEia1fNnb+y7Vrn2fG/FJVgZ8r4 TYPrmrFwRfRHjQVUHSan93o+fBZAmJEWU0/Z03lMSMns5cPCvF9m8RF+ost/+g/o2tW4YPbVbcKSw witQeG9dzQsZqLAqo5BGep+BxqL7MrW8CKAQ8BBTE3A+OgpqyPtoPP65Zo197G9s75EkJu6jR05Tc R/3F16MGLdac4KiaJBDu5j8Gv77kEe1wsEmEBS1cZu+++MpOK3JazRUo7c3l6jfJQZ5ztbT3z2MdT mJLd/wPw==; Received: from 77-249-17-89.cable.dynamic.v4.ziggo.nl ([77.249.17.89] helo=noisy.programming.kicks-ass.net) by desiato.infradead.org with esmtpsa (Exim 4.98 #2 (Red Hat Linux)) id 1taXdB-0000000DXET-1aaq; Wed, 22 Jan 2025 10:06:41 +0000 Received: by noisy.programming.kicks-ass.net (Postfix, from userid 1000) id 28303300599; Wed, 22 Jan 2025 11:06:40 +0100 (CET) Date: Wed, 22 Jan 2025 11:06:39 +0100 From: Peter Zijlstra To: Roman Gushchin Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, Andrew Morton , Jann Horn , Will Deacon , "Aneesh Kumar K.V" , Nick Piggin , Hugh Dickins , linux-arch@vger.kernel.org Subject: Re: [PATCH] mmu_gather: move tlb flush for VM_PFNMAP/VM_MIXEDMAP vmas into free_pgtables() Message-ID: <20250122100639.GF7145@noisy.programming.kicks-ass.net> References: <20250121200929.188542-1-roman.gushchin@linux.dev> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20250121200929.188542-1-roman.gushchin@linux.dev> X-Stat-Signature: 1fwoyjb4753ywtm1imqtdzfi5egxjc3f X-Rspamd-Queue-Id: 5A3E910000D X-Rspam-User: X-Rspamd-Server: rspam06 X-HE-Tag: 1737540406-993928 X-HE-Meta: U2FsdGVkX1+jZ5LlBHuPua88+skz/kzOFpIBtjcd7PDxvGhB3nAKjoTHmt+6SUO3MVTTIEMrL5Wu8JJVroVQRd3pFzf0mi14Few+hNnhtYedSwXlq6GwPOKq0Sezfg9bN7H1IHE2zk2XrzcP3BrOZ946OaH6fTAVUEKfZsKhknU5guFqLLiGpGBTwMoTQfOcv06mDBJexuyGtuz4xeAVINP/Bpc6+6nsNt5uOG3bO8doSPSeGzEr6vNeRRmXD2mZsV3KEd6MGYSO25g030AwSDddAoymMy7c9MaYoITfCZ9+03/2A+wYGHWfeR9MF+fTHLa2ZTBjyGRBkH+GMZu0hpiAGfgDc080fE4SK8fs/7xa0AZ29HIKWYTeKUpH6/3OOnoWN687inWlMwlpjpSPbNA+AX9aqaliM+rwbWA4iHRNZK8ryLkjhEVLbzWEV65MUWORgLANLSoUJJnDuMrLwmwvpyCK3BQtoBQjhJsLvXzBnvhkIAJqc4I7RRwWFnyx+XBJtK0HfQC5PF4g7LBseBuZ/TpTDU6Zp95rW3asvmGeXNzFIwseIc8KA7lRnenxCIU02WuC+7hh3znwnLpTx4/JHsWDCCkbNHyeTY1ffmxk2Oyyhv4XaWQ6/pL9HxIfpgGFIqwQiiuKHxf7yGy9/4P5jZ7od9aojGf4zZbtLaecatGOUAHpa93k37Tt3sZCaBlBG4JrGdy8xc4kDmVC6NxifMBe/ASJS1VMmIxsXeDvkb6ydi41OOvGseXDKCCTOZwinvWDtFJJbEvBr0gm3oBRVivARc/dYfydg/5Em4X+SQBfE8ozlWO66FmCkoGReEo5IDv6JRmlgShXIesmTfmoWgQtSU1LbYaOYWtboI6Lj0PBH5Tx1bwre9srIob7ywMSHBGO+6RG178gd8I8te5yprVYBN+N8HFZSyrj2dP1mE9jiq8fQCFsBzv5sUU/WYJnxnkEl9XHIwUdyDw gCoCV3/c bcjP5wVTbAHSaHxibY5EZLGbnoiLwq36esevBjk+66RyUvuwB3qogcG6kMYuFCPoXTv8Om8dFwTsriuEgKA8N9k3/ChdUXiE28YhNeq6ebBVn/tETuXnpW+nLVkyF7Yjpe2c/vtSFCHuAycXvo/vTtIYQyKwfInnyJPfYOogWW41mmNEp12y7kkBILs36cZ4jTXA56xPMKuWzT+TCsZy9O6toB295h4vgOciUs947hvPh0PzBWHjPIYjBn5QeEz5CEbk8GZOyVUt1ams= 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: List-Subscribe: List-Unsubscribe: On Tue, Jan 21, 2025 at 08:09:29PM +0000, Roman Gushchin wrote: > Commit b67fbebd4cf9 ("mmu_gather: Force tlb-flush VM_PFNMAP vmas") > added a forced tlbflush to tlb_vma_end(), which is required to avoid a > race between munmap() and unmap_mapping_range(). However it added some > overhead to other paths where tlb_vma_end() is used, but vmas are not > removed, e.g. madvise(MADV_DONTNEED). > > Fix this by moving the tlb flush out of tlb_end_vma() into > free_pgtables(), somewhat similar to the stable version of the > original commit: e.g. stable commit 895428ee124a ("mm: Force TLB flush > for PFNMAP mappings before unlink_file_vma()"). > > Note, that if tlb->fullmm is set, no flush is required, as the whole > mm is about to be destroyed. > > Suggested-by: Jann Horn > Signed-off-by: Roman Gushchin > diff --git a/mm/memory.c b/mm/memory.c > index 398c031be9ba..2071415f68dd 100644 > --- a/mm/memory.c > +++ b/mm/memory.c > @@ -365,6 +365,13 @@ void free_pgtables(struct mmu_gather *tlb, struct ma_state *mas, > { > struct unlink_vma_file_batch vb; > > + /* > + * Ensure we have no stale TLB entries by the time this mapping is > + * removed from the rmap. This comment would be ever so much better if it would explain *why* this is important. > + */ > + if (tlb->vma_pfn && !tlb->fullmm) > + tlb_flush_mmu(tlb); I can't say I particularly like accessing vma_pfn outside of the mmu_gather code, but I'm also not sure its worth it to add a special helper. I do worry this makes the whole thing a little more fragile though.