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 EB66BC02188 for ; Mon, 27 Jan 2025 10:03:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 64B0D280144; Mon, 27 Jan 2025 05:03:26 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5FB1628013F; Mon, 27 Jan 2025 05:03:26 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4E99F280144; Mon, 27 Jan 2025 05:03:26 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 302F828013F for ; Mon, 27 Jan 2025 05:03:26 -0500 (EST) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id CCA861222F3 for ; Mon, 27 Jan 2025 10:03:25 +0000 (UTC) X-FDA: 83052794370.16.989D295 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf02.hostedemail.com (Postfix) with ESMTP id A971680017 for ; Mon, 27 Jan 2025 10:03:23 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=SDdOK1Dt; spf=none (imf02.hostedemail.com: domain of peterz@infradead.org has no SPF policy when checking 90.155.50.34) 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=1737972204; 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=cqqWTLTGcefHQFj93a6JzifbYNKjM6buoj1GhNpFJgU=; b=LRQ6aiGSGmDs80fNeuUwJVh+2tqm3WeVPeeHXKAdfOa161a1LasNedzVVaDOegB5OySSSn CVeGOQhMApyDN7Y/I07P/YvO+IqbbJz+YfJNilSp2XZvG1ovLX93sNC09d+nh45SuukxPa OJZDsxDoo8c4JsvgD/zL41I19Dj/W+w= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=SDdOK1Dt; spf=none (imf02.hostedemail.com: domain of peterz@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=peterz@infradead.org; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737972204; a=rsa-sha256; cv=none; b=VgPrVuQkVZRMPUzzh4QTx4TlL1KWAnBoAq3unAQyN37RDryiy9QCFQyTCL5ZfR2g9Epozo enGCucrOvx84x6aHbTlkgt1dVZaWK0RquWxUcjC1PhjOZKXeok0lcXdgvvg90vV0D9jnFF YZNZ11JEArW9YXF2l0pS7DrPslJINC4= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; 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=cqqWTLTGcefHQFj93a6JzifbYNKjM6buoj1GhNpFJgU=; b=SDdOK1Dt/Gj/VFCUcunnKUPAdZ JlDDS9o7ZGOw/5ZKkQwL+USGtZ4Q+h8HL7CvHopZsdMiweCfUoFeVzL0emBvXm+iUIWbi/d+j/miJ Bd6Rv5Aro4K69bnQv9F7FpshBJheDPnNCd+VLCS3O4G9joct0DErNCgMf5Ng/tm9lAVm2WMCMyTVg 7fLDnW1LmT2BbhWWNJxfy0gUT+/81Ao6Ji2gDflEDsmoRA0ghS7fhoTBAsg7oqOB931N/6BKH41EL k2VJdg9D7XbjUKFPYwVBv1/yvy5KFMRJhWkgHlEf04H28xsKDw19BOgJ5hE/KmGy5Ax7LiGhyeFQE b26ZEz8A==; Received: from 77-249-17-89.cable.dynamic.v4.ziggo.nl ([77.249.17.89] helo=noisy.programming.kicks-ass.net) by casper.infradead.org with esmtpsa (Exim 4.98 #2 (Red Hat Linux)) id 1tcLxg-00000009FFV-3Csm; Mon, 27 Jan 2025 10:03:20 +0000 Received: by noisy.programming.kicks-ass.net (Postfix, from userid 1000) id 5889B3004DE; Mon, 27 Jan 2025 11:03:20 +0100 (CET) Date: Mon, 27 Jan 2025 11:03:20 +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 v2] mmu_gather: move tlb flush for VM_PFNMAP/VM_MIXEDMAP vmas into free_pgtables() Message-ID: <20250127100320.GC16742@noisy.programming.kicks-ass.net> References: <20250122232716.1321171-1-roman.gushchin@linux.dev> <20250123214531.GA969@noisy.programming.kicks-ass.net> <20250124082250.GA13226@noisy.programming.kicks-ass.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: A971680017 X-Stat-Signature: fatqpk8g4fxifxd1ukz8ciodmamzieta X-Rspam-User: X-HE-Tag: 1737972203-978832 X-HE-Meta: U2FsdGVkX18AmbJHEy68klPGXCOA1s2i9aDt/MDM4QmmXxMZ4XzjsD/o7KdD+pBZTSPjrNYlEwBkMsJayp3Y/8/8Ibd/10JSM2iQsxm53VPLxC8VE/ZfXnYyx0Tr+ecLup6hKQmoGrQ9pC2Deapg/5O03fnVz07PbIGmDvMtaTAcQpYSFs1bnY8jLJF4Zu3ri66dIkrEW/tM54eYTL4KUj909yZqDoBMibcCPO0foL1TY5O9w9/nzgD6k29XENI4cVysb3c8A+MwyXa8DzeG5/UFcjQ2q4agzPxNQ6W+HtRwcy1ZaazxJL1sb/YFNDIodu1PyhKE7mPywP1oQltgG1KkQrf4tQ4GGlLZGHXGkEfouvvTBk8fjsNUrPdwq6nTXFVpOEcCerD7McMiQMthsFz297NXzHwwDpQ+oZXnl+rRNo7/O+YPP1Aiyd8i0u5FqYA4V2wBZ4FiWuFH8Y7GnStu38YlfVzBQmbDcKwR4LytCb6noRWMdXv00uUInTu5BvEjUDEuIcFOZ8z/9GnLiNpjgAK1oDCaHiCMMRWelXaL/WwIXcGTlc7gYXTOmskrd9FPCP71tKDKmde+ATM5Uhn69uEGqb6Yb374AZojf2HJ8Of4026BSUR1wFfDYapWpT6uO3d3hfH2RlpSb80Rv5JYkLfV8TLPCG2O++gpqF0Xo5z45ow34Ko8OUeAZAfAjfJ9NVLTYFtYEcZgu4+6GysatTAGDvH4JDNtrsP+ryHKR7qHrnd7bkSym6jYfgDWkr4IZoo2MrkXJkkIEMQZ+zS15mjRqSP7N46fM2kcUM60OUEM7Rwc9gFce/F8oRNmMAkdDxctX3PcbrUIP7XxqVZTav43W0F7JReoodqM+htTQpSKcZLrBD+KePG+eGuE0DyGE/Qf5OvZDmYHAmxgyphr0UBX2Z6IIiZwAHAacm5NO0xEI5X0StF1Pm8FnejVLMz0uVpXjJOK+/U5J1w 2C+6vBRQ s83Hhyyb7zB38/9QBG+7H41pzCG8O4Awt0chlbPMAp1kOQrb23QlU+Wc9hXVZVPczm3cd15fUkg0CEhruzPlb9nbolHoEJlUv/+RrcnWO3NeTURzbgxYnnIbuuMBTo+VxnMrGLR60ocdJLDPm5pU1P62Hg+vNgKx7Fe5qAjoJzYMeZ5QBLsbLKPs20+SW9u2FnpdX5THmTOeMbv7RdyGSAj3WBl3UfHvM0JBW5yqSdk4aGVE= 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 Sat, Jan 25, 2025 at 01:23:54AM +0000, Roman Gushchin wrote: > On Fri, Jan 24, 2025 at 09:22:50AM +0100, Peter Zijlstra wrote: > > On Thu, Jan 23, 2025 at 11:12:33PM +0000, Roman Gushchin wrote: > > > > > > +static inline void tlb_free_vma(struct mmu_gather *tlb, struct vm_area_struct *vma) > > > > { > > > > if (tlb->fullmm) > > > > return; > > > > > > > > /* > > > > * 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. > > > > + * > > > > + * Specifically() there is a race between munmap() and > > > > + * unmap_mapping_range(), where munmap() will unlink the VMA, such > > > > + * that unmap_mapping_range() will no longer observe the VMA and > > > > + * no-op, without observing the TLBI, returning prematurely. > > > > + * > > > > + * So if we're about to unlink such a VMA, and we have pending > > > > + * TLBI for such a vma, flush things now. > > > > */ > > > > + if ((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) && tlb->vma_pfn) > > > > tlb_flush_mmu_tlbonly(tlb); > > > > > > Why do we need to re-check vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP) here? > > > > No need, but an opportunity. > > > > > In free_pgtables() we're iterating over multiple vma's. What if the first has > > > no VM_PFNMAP set, but some other do? Idk if it's even possible, but it's not > > > obvious that it's not possible either. > > > > If we only need to flush PFN entries before unlinking PFN VMAs, then: > > > > - if there are no PFNs pending (vma_pfn), we don't need to flush; > > - if no PFN vma is being freed (vm_flags), we don't need to flush. > > Right, but if I understand the code correctly, more than one vma can be > freed by a single free_pgtables() invocation. Should we then check > each vma's flags in the while loop in free_pgtables()? But then > we're back to where we're now with multiple flushes. Right, I misplaced it -- it should be in the vma loop. > Do I misunderstand this? I'm not sure how this would cause more flushes; notably it will not cause flushes where no page-tables are dropped, eg. MADV, which was why you started all this IIUC.