From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (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 A2AA2484234; Tue, 1 Sep 2026 17:15:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788282942; cv=none; b=qwkfxa16/kfCxRYs66BgyjoSKn/mPBBaT+ialiN6610Kmq/N0fMytiUzCL1KCcQa+26ED++4h0Yq/oXDRiBZF1mxsqLfQZmpxxNnuh4niwq7xtsnl0ipGdDI5lvswlOuSnWv7eYwU9vrfWMtEbdjHMSBgwAhyZlphaek9+hRR2g= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788282942; c=relaxed/simple; bh=xaSmTI9LS9eyAuhjBKW8l2SFyu98wbHf0onA/bVri6g=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=GeaF615isqPTbq4DDfRxvVeqMlQh71UCDA4bSejy/hKEgKrJ4MjjCbT03d5c9NlFAWAZZPnxV0tlLk4JAYPUXM5hFB/bfgz1kYkZArnkYsFF+XKpEAhalyRTrBsNOQCTiskcicb/K3/oOiX/Hz0umbspSOW0F+5mrkNKyuGIgi0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=EjDkff8+; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="EjDkff8+" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8F7DE1F00A3D; Tue, 1 Sep 2026 17:15:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1788282941; bh=FGNWee/yfyIYUeq0phNhaEpYpUuczRbQOv0XGs51Szs=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=EjDkff8+PB/PqMSdkvLTNkRB1RR2uQN6hGW85hkDNe66dk+tGPcziIsgxwIXFJ+9t tbCD8FYmEfQQIAMLueKZrLVZKDA76MgFrGPoWNGJQOTR4Fbd2wzNFboNKFeQ8O6bgf g9UmLVoDBcHSh1Bk0cGeKv2N8qx4AJAjr4n3Ab8kFFqwsIRhyTO9wipt15ul7+AfC1 ZzDvM3NRhCjFHcYyZfZTmOA/gtzXdArKE0hSwUNfLiJH+KEwFD+W9EF52D8D3QyBBR 2sQ1KYb4uykiDVH82Cr+sPdgF++1bzpUbEI2we1O/PSJA7oAGGEWvXkrUPiYIvBk7Q gDzS6ddLMDyrA== Received: from phl-compute-05.internal (phl-compute-05.internal [10.202.2.45]) by mailfauth.ams.internal (Postfix) with ESMTP id 8D10B1980082; Tue, 1 Sep 2026 13:15:21 -0400 (EDT) Received: from phl-frontend-03 ([10.202.2.162]) by phl-compute-05.internal (MEProxy); Tue, 01 Sep 2026 13:15:36 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: dmFkZTEYX5efotjdzBxG3ALFHNBriUAjRl1NaDQcBbtf545JlTWl8puARyoc1M48QLkTle Zy+DNI48newDPVlWImF1Faw5W1GdOvkG331QFNWqXsS36x8nYIvZkExZ02FyZDNo3imqSO vx+6PbbCZjIWTmWK3f9wDaPsdOrKllg3nPiDeTujNg7QScH79PdB1/uzR6LCnWC3G64VYn GHMdJtZr7tl3naSkkwKGVvaRI3xNIM1Vpn9wQM1eNoqT7XIGThtVtOA+q2qz2+cjtaTpZj qEM5PDu25TZmRuhbx6DVTKRpS6Xkl1hJA5Lrjt0ldQvhD92NmcHWzFHmiu5NS/Ddrh91RS oEIYoI6jcBVZRYEqPIQ3YWxmK7df4Xai0OPDkg2MfU9DIhhXQx3bsZyOPdYoBu1fZzi3mI 9COKHxpd51axawUzzdPbbHo4qFiQXCsINSt5OTD8o2xsQivlbyxj6NFVUlP4m225t89xIR dJ5apX7XlKEtt4HhCJ9eBLLmWDTmsQzhqfdzCVOyy89B/ed+iZD0tvpmPkm8HXDlmFmgUX pI+m2W4cNU8lg3Mlsu1jG0yzbdxBrZA1C5l94jk+JqKA2fZ/+87Dm/Bai+4A0tSdNZXigK 1YTCuTPU5tr3c9qwCJxPS8mFmNq97ErTQXKYdjvem1U1fQYobqoM2g+1Krcg X-ME-Proxy: Feedback-ID: i10464835:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 1 Sep 2026 13:15:18 -0400 (EDT) Date: Tue, 1 Sep 2026 18:15:17 +0100 From: Kiryl Shutsemau To: "Lorenzo Stoakes (ARM)" 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-s390@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 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/ Otherwise, LGTM. -- Kiryl Shutsemau / Kirill A. Shutemov