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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 326CBC61DD3 for ; Tue, 1 Sep 2026 17:15:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:In-Reply-To:Content-Type: MIME-Version:References:Message-ID:Subject:Cc:To:From:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=FGNWee/yfyIYUeq0phNhaEpYpUuczRbQOv0XGs51Szs=; b=MKmsNtFZFbQCoDuooKY/E8MJNe 3QwWXUaPjCq4QSAmvVpAag756tXL7MD2D6Poi9V98LUaTW+nzUbAu/Gt3m3p949JhLbhaFfzmdcWF dkHOGxxiDFQDEqC5TxZzcCsy7mYyej1ye8dk7oD0uFEbJxWkd6Lk1H2Sl4/TxMqCRyDx+5vE2LTVi HXh+pGXzEArffyiaQOnBe5rbeBLPBBfj2Dvgj5YNA6YyWL4OfrFGdiXwCtztCceqk/lqU4q/i9DR6 vNezC616luR8KBTtw6cScwI3HfhWFp+/OeNxwZkarLr2rQICuERfsjoqzRUK+PJGPHgrz/1dENbEu la0pznMA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.99.1 #2 (Red Hat Linux)) id 1x1S5I-0000000Cn5O-2bgd; Tue, 01 Sep 2026 17:15:44 +0000 Received: from sea.source.kernel.org ([2600:3c0a:e001:78e:0:1991:8:25]) by bombadil.infradead.org with esmtps (Exim 4.99.1 #2 (Red Hat Linux)) id 1x1S5G-0000000Cn4i-2efP; Tue, 01 Sep 2026 17:15:42 +0000 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by sea.source.kernel.org (Postfix) with ESMTP id 4FE9943875; Tue, 1 Sep 2026 17:15:42 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8D0181F00A3A; 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=1788282942; bh=FGNWee/yfyIYUeq0phNhaEpYpUuczRbQOv0XGs51Szs=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=RIXa8dZkzMG2REHgA+VDAVKolhCMPi+Irdr20SxXJMX7HMyXOMlIZP78yNzX5WGK9 2Q4fXWAmZrvB514yG3zy5S8VLVpqQ3vQkgHOehuxKNoTERFFWTW4+QLvqN20efYMvx C3unqe6ijhgX3ovf1O8T9Vz9+k4UMWd/bYoJTnhtxLRd4Kgy/9lp+XCWbYXgMalSOY nrPiTr7Yh8PLixNUIX1V0R4kTFgkjX2hngNjLdW0hS5WPPS3VopfDXq68957fjP1n+ WFiWsZyfSI61wWrG8R+nmkPCKmtMMrQguU2YX4lBdcYCQjfVs2NOssv9ZhspqUsm// Rg36srZr7tcEQ== 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> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-BeenThere: linux-um@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-um" Errors-To: linux-um-bounces+linux-um=archiver.kernel.org@lists.infradead.org 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 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 61C2DC624D3 for ; Tue, 1 Sep 2026 17:15:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References: Message-ID:Subject:Cc:To:From:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=9CX8kvqropHRGL9JxZ7pkYrfaSQRX/mczoKGuYzU8cQ=; b=EhRtQ4kjqxDyuC jGXuFcxpOT1gGSHwdqA5CjyZNsuQZ5KwBaDUvnG9jY7m6ped/e/RwlCPyRYPUOvVZfzGVe4B+ynIU F4VGDkMWFuKSpPXW1m8F7/HlTAdQm2wRN+uiWVu/cKZenXMV4xs8UGd1M5tB/9e8CJubf8yqp4IIF fl1qZ4ioxrTwOzX7ZMZxXO5kIyB1LkOqpUPBxKzbu8yHhl/3oTA8lnqB7SFY8bXQCzQx2b/AXCx0e eERBGqlVX8rw6vP2HNK4OXdDUimVDDpXQFS+e+ENRaPyB0Pye1ce1PUJ3j01f5SgHj+qt9hV8KcwA +wo75uc6unavpiQ30Jpw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.99.1 #2 (Red Hat Linux)) id 1x1S5I-0000000Cn5J-2G6C; Tue, 01 Sep 2026 17:15:44 +0000 Received: from sea.source.kernel.org ([2600:3c0a:e001:78e:0:1991:8:25]) by bombadil.infradead.org with esmtps (Exim 4.99.1 #2 (Red Hat Linux)) id 1x1S5G-0000000Cn4i-2efP; Tue, 01 Sep 2026 17:15:42 +0000 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by sea.source.kernel.org (Postfix) with ESMTP id 4FE9943875; Tue, 1 Sep 2026 17:15:42 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8D0181F00A3A; 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=1788282942; bh=FGNWee/yfyIYUeq0phNhaEpYpUuczRbQOv0XGs51Szs=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=RIXa8dZkzMG2REHgA+VDAVKolhCMPi+Irdr20SxXJMX7HMyXOMlIZP78yNzX5WGK9 2Q4fXWAmZrvB514yG3zy5S8VLVpqQ3vQkgHOehuxKNoTERFFWTW4+QLvqN20efYMvx C3unqe6ijhgX3ovf1O8T9Vz9+k4UMWd/bYoJTnhtxLRd4Kgy/9lp+XCWbYXgMalSOY nrPiTr7Yh8PLixNUIX1V0R4kTFgkjX2hngNjLdW0hS5WPPS3VopfDXq68957fjP1n+ WFiWsZyfSI61wWrG8R+nmkPCKmtMMrQguU2YX4lBdcYCQjfVs2NOssv9ZhspqUsm// Rg36srZr7tcEQ== 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> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org 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 _______________________________________________ linux-riscv mailing list linux-riscv@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-riscv 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C1C00C624D3 for ; Tue, 1 Sep 2026 17:15:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References: Message-ID:Subject:Cc:To:From:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=40DBETjlOc9P9x1xuCQIcXBbN8vnn48Md1Krz+wctcc=; b=QWv9oBjyvFRtoL bB6P22jZotSQqOb02HKvrxPPfBxjKggx1TEqQGI0FPWK7UCpLF6L/0haj/d/tL6Zbwjd+TkEZzYQC 9TC5IgL3El6GZCz7j7x99pCvu9ra9Ftm90AFtO3b8yfN4jfaZ8/Qa4W08IZNzoaKrZRBjNd4e++3n b66xii9jquYwNO4TZGbOqb01RREIkD2pCZmsG7pD73JdUNkwEASSS4QNYnZFbsnoPTx02QS8Qzwsx MfNBgO1n2U+pAGYVS5EzyRfiRlEN9E8nJQrbrgrivhkeWmMguDLlsKM+/Shq0Q/gAAMDTtFyUwkwD ERevfJuUiN9IUG8StBFw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.99.1 #2 (Red Hat Linux)) id 1x1S5I-0000000Cn5B-1xug; Tue, 01 Sep 2026 17:15:44 +0000 Received: from tor.source.kernel.org ([172.105.4.254]) by bombadil.infradead.org with esmtps (Exim 4.99.1 #2 (Red Hat Linux)) id 1x1S5G-0000000Cn4c-2Y7s for linux-snps-arc@lists.infradead.org; Tue, 01 Sep 2026 17:15:42 +0000 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by tor.source.kernel.org (Postfix) with ESMTP id CA53C6022D; Tue, 1 Sep 2026 17:15:41 +0000 (UTC) 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> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-BeenThere: linux-snps-arc@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Linux on Synopsys ARC Processors List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-snps-arc" Errors-To: linux-snps-arc-bounces+linux-snps-arc=archiver.kernel.org@lists.infradead.org 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 _______________________________________________ linux-snps-arc mailing list linux-snps-arc@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-snps-arc