From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from tor.source.kernel.org (tor.source.kernel.org [172.105.4.254]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6CA5A488762 for ; Tue, 1 Sep 2026 17:25:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=172.105.4.254 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788283547; cv=none; b=s04r52Cy/gx4O31pav987fyGBKwGLcBRWTqQvM7H10tv9B3HIERjtz/Sg8m9BPvOkfhK6B0Kfk278tqbS8c2BQMCQGyZHHrRSJaZEJAKv3uPaBhF8UDhVJz+BG/LBwhdOAGaun3aVLkCCWaoW02ClG9Zqj6WnQO533Y5SniYoeM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788283547; c=relaxed/simple; bh=QctYkGHGrMucr4GtNr0KI1/R0IrrYuWB6T48LH5+pTk=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=SRsOBupkYe8B5k5GyCCkT1AGEqI0Yhcj2D3B86tXXqDyZq5BdTEuwmZ7rHo97EYZtFnygtrUqjf4lYv1vI1eiBbJtCMXnVzmIWAeLKb5N2ggCYUheRpiiraG1hdKT+q8cVfJYTCFlIlOlJt9281VfqSWes+7az8gM0fdfEvRJqU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=kernel.org; spf=pass smtp.mailfrom=kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Yi2t1tkD; arc=none smtp.client-ip=172.105.4.254 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=kernel.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=kernel.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Yi2t1tkD" Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by tor.source.kernel.org (Postfix) with ESMTP id 9F5C86020D; Tue, 1 Sep 2026 17:25:43 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2F38E1F000E9; Tue, 1 Sep 2026 17:25:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1788283543; bh=RU/XHZsQrPB/KjzCrGwIunh3KbYqDQIpxjG/zB2SrYU=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=Yi2t1tkD7CbVI2GlNVLMPxmx2y6uQ0N17KQVN6tfdVVmsc8e3U70HKz60pHNwlQs3 1uaubu11GEhlMNqEwwCAnmGwy18sQmPBDQlkl4HFerFnkf9qqSXhXZRQB4AbUCIy65 +F5eg8cH3mV8+ZyrzWtQ7QlVH0tfsCC88xh+SXAYBAqn1Leh14VC/K7s9GATAG+x4E KI4pAxyEdR503wQD7kpPC4DxKOGSs/zzQQ249JbOJTrJb8Tg6ICIFvJvGHbD91pyny gkZbMWCaIAhY75HIF/43FdM8kRnN6lg6QLV7LnJhpcpnHrbOS/0Iht1oAeMIckn9b0 Sr0NCrNgxEH6Q== Date: Tue, 1 Sep 2026 18:25:26 +0100 From: "Lorenzo Stoakes (ARM)" To: Kiryl Shutsemau Cc: Andrew Morton , David Hildenbrand , Zi Yan , Baolin Wang , "Liam R. Howlett" , Nico Pache , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , Usama Arif , Guo Ren , Brian Cain , Geert Uytterhoeven , Dinh Nguyen , Simon Schuster , Jonas Bonn , Stefan Kristiansson , Stafford Horne , Yoshinori Sato , Rich Felker , John Paul Adrian Glaubitz , Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexandre Ghiti , Russell King , Vineet Gupta , Michal Simek , Chris Zankel , Max Filippov , Will Deacon , "Aneesh Kumar K.V" , Nick Piggin , Peter Zijlstra , "David S. Miller" , Andreas Larsson , Richard Henderson , Matt Turner , Magnus Lindholm , Catalin Marinas , Mark Rutland , Huacai Chen , WANG Xuerui , Thomas Bogendoerfer , "James E.J. Bottomley" , Helge Deller , Madhavan Srinivasan , Michael Ellerman , "Christophe Leroy (CS GROUP)" , Heiko Carstens , Vasily Gorbik , Alexander Gordeev , Christian Borntraeger , Sven Schnelle , Richard Weinberger , Anton Ivanov , Johannes Berg , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Arnd Bergmann , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Jason Gunthorpe , John Hubbard , Peter Xu , linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-csky@vger.kernel.org, linux-hexagon@vger.kernel.org, linux-m68k@lists.linux-m68k.org, linux-openrisc@vger.kernel.org, linux-sh@vger.kernel.org, linux-riscv@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-snps-arc@lists.infradead.org, linux-arch@vger.kernel.org, sparclinux@vger.kernel.org, linux-alpha@vger.kernel.org, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linux-parisc@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-s390@vger.kernel.org, linux-um@lists.infradead.org, Hugh Dickins , Qi Zheng Subject: Re: [PATCH 12/12] mm: change the contract for free_pgtables(), update docs Message-ID: References: <20260901-rcu-pagetable-freeing-v1-0-5456a81c8212@kernel.org> <20260901-rcu-pagetable-freeing-v1-12-5456a81c8212@kernel.org> Precedence: bulk X-Mailing-List: linux-m68k@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: On Tue, Sep 01, 2026 at 06:15:17PM +0100, Kiryl Shutsemau wrote: > On Tue, Sep 01, 2026 at 03:31:26PM +0100, Lorenzo Stoakes (ARM) wrote: > > On Tue, Sep 01, 2026 at 02:57:13PM +0100, Kiryl Shutsemau wrote: > > > On Tue, Sep 01, 2026 at 12:01:32PM +0100, Lorenzo Stoakes (ARM) wrote: > > > > diff --git a/mm/pgtable-generic.c b/mm/pgtable-generic.c > > > > index b91b1a98029c..ff8ff3706485 100644 > > > > --- a/mm/pgtable-generic.c > > > > +++ b/mm/pgtable-generic.c > > > > @@ -386,9 +386,21 @@ pte_t *pte_offset_map_rw_nolock(struct mm_struct *mm, pmd_t *pmd, > > > > * be read-only/read-write protected. > > > > * > > > > * Note that free_pgtables(), used after unmapping detached vmas, or when > > > > - * exiting the whole mm, does not take page table lock before freeing a page > > > > - * table, and may not use RCU at all: "outsiders" like khugepaged should avoid > > > > - * pte_offset_map() and co once the vma is detached from mm or mm_users is zero. > > > > + * exiting the whole mm, does not take the page table lock before freeing a > > > > + * table. > > > > + * > > > > + * However, the PMD entry is cleared first, and the table freed only after > > > > + * an RCU grace period, so a walker that mapped the table under > > > > + * rcu_read_lock() stays safe, and the pmd_same() recheck in > > > > + * pte_offset_map_lock() detects the teardown. > > > > + * > > > > + * Therefore it is safe for "outsiders" like khugepaged to use > > > > + * pte_offset_map() and co. for VMAs that might be undergoing page table > > > > + * teardown. > > > > > > Is it strictly true? > > > > > > free_pte_range() clears the pmd without taking the PTL, so the > > > pmd_same() recheck in pte_offset_map_lock() can pass and the pmd gets > > > cleared right after. > > > > > > Readers are fine -- __pte_offset_map() holds rcu_read_lock() until > > > pte_unmap(), so the table cannot go away. > > > > > > khugepaged is an odd example here. collapse_pte_mapped_thp() and > > > retract_page_tables() don't rely on this recheck -- they take pmd_lock() > > > plus the ptl and do their own pmd_same() under both. > > > > > > Can we say walks are safe and leave the write rule where it is? > > > > Hmm yeah, ah page tables, what a rabbit hole of horror :) > > > > I guess this is effectively a reflection of the 'write lock on write, RCU load > > on read' pattern in RCU itself generally. > > > > So, maybe something like: > > > > * Note that free_pgtables(), used after unampping detached vmas, or when > > * exiting the whoel mm, does not take a page table lock before freeing a page > > * table. > > * > > * As page table freeing itself is RCU-safe, page table readers can safely run > > * concurrently with page table teardown. However, writers cannot, as without > > * a lock nothing prevents concurrent teardown. > > */ > > > > Instead? What do you think? > > s/whoel/whole/ Haha yup the inevitable typo... > > Otherwise, LGTM. Thanks! :) > > -- > Kiryl Shutsemau / Kirill A. Shutemov -- Cheers, Lorenzo