From nobody Sun Feb 8 19:39:38 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 80184EB64DD for ; Wed, 12 Jul 2023 04:46:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231532AbjGLEqg (ORCPT ); Wed, 12 Jul 2023 00:46:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57304 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230235AbjGLEqc (ORCPT ); Wed, 12 Jul 2023 00:46:32 -0400 Received: from mail-yw1-x112e.google.com (mail-yw1-x112e.google.com [IPv6:2607:f8b0:4864:20::112e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 27AC81733 for ; Tue, 11 Jul 2023 21:46:30 -0700 (PDT) Received: by mail-yw1-x112e.google.com with SMTP id 00721157ae682-579ed2829a8so68751737b3.1 for ; Tue, 11 Jul 2023 21:46:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1689137189; x=1691729189; h=mime-version:references:message-id:in-reply-to:subject:cc:to:from :date:from:to:cc:subject:date:message-id:reply-to; bh=4LOZtvGEF5g0eQvRdR0fiwT6tMgm62bdW0dz/Hhyqlk=; b=J1OyC/PzRxAcAnxReOGyZoimdKHV4sne6EV+ZF5GnSzIGkj49DIxf8CJZXSiYaLo8O rQDVnXnu0y21FRcOCZP0OeEhb8w1Ae51u8RxjVwTlnPwlHyRZu5pHWxjcdG3sLbNOkgm l9DRu2V+r91JT6sg8uxZ0xdueMEWKHVeYHw/ucTn/rvDkwQeKDUMIXdd66/4pn+ZPDqc bbexzf/2xXArSFmFzEjH5dtsWbOgASWzeyhuG3Z+bgzkYYJreHWjygSN0MaZVhpwfXUc /vz1Ww9YOcG9dsJAekHocNmUdgaeY/Jq/K6dI5tFUgzSXd3eubGhE9SWhC2zJPkylOJH UPbw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689137189; x=1691729189; h=mime-version:references:message-id:in-reply-to:subject:cc:to:from :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=4LOZtvGEF5g0eQvRdR0fiwT6tMgm62bdW0dz/Hhyqlk=; b=Gy2k/umRA/uSZTczpd1B7Y9Xg6E6JI1RON16q3XMRsjElIgcoPieT5Bxa+XMKtUmk3 qvndgF5mYoBxjgrnZd63IM+o0SJyVIJQrzzFKp2k6wCnJrD7ynUnRVLt16O8URZ6sfOM dXLAlTv/vUcsuuGyfb/bB1tJKyGAADiawKKfC2tuDz3ebDSkTHTzvL9mpyGJ7dDKMtaP MDnyX4nQffpLhlicqLSsC17wONygmQvhVj60E/dU+Fah6os3R10w0HQgRgvAMr2Mtqsn u9vDMBZh+JBbaOUo/5LJnimLxa5vusE/0Xw9cGgGcb1pCLvXRO479V7Tu4nYGtUGkYVC 0rAA== X-Gm-Message-State: ABy/qLZAJPf1+Dt3ylrErTNmNrkZnkUao96l2bbC20wT/XCnHZKzFkan HsRMrlOZTrbCMcSEDRVWTuekZQ== X-Google-Smtp-Source: APBJJlGajCIPfwdsjGp7sM9eUSR1neGCrgzZWT479LGOvJujy6hRuJqugeEnCaM3mBFakTQjRwJQ+w== X-Received: by 2002:a81:5dd6:0:b0:562:16d7:e6eb with SMTP id r205-20020a815dd6000000b0056216d7e6ebmr17894738ywb.40.1689137189022; Tue, 11 Jul 2023 21:46:29 -0700 (PDT) Received: from ripple.attlocal.net (172-10-233-147.lightspeed.sntcca.sbcglobal.net. [172.10.233.147]) by smtp.gmail.com with ESMTPSA id j126-20020a0df984000000b005772e9388cdsm969335ywf.62.2023.07.11.21.46.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Jul 2023 21:46:28 -0700 (PDT) Date: Tue, 11 Jul 2023 21:46:23 -0700 (PDT) From: Hugh Dickins X-X-Sender: hugh@ripple.attlocal.net To: Andrew Morton cc: Mike Kravetz , Mike Rapoport , "Kirill A. Shutemov" , Matthew Wilcox , David Hildenbrand , Suren Baghdasaryan , Qi Zheng , Yang Shi , Mel Gorman , Peter Xu , Peter Zijlstra , Will Deacon , Yu Zhao , Alistair Popple , Ralph Campbell , Ira Weiny , Steven Price , SeongJae Park , Lorenzo Stoakes , Huang Ying , Naoya Horiguchi , Christophe Leroy , Zack Rusin , Jason Gunthorpe , Axel Rasmussen , Anshuman Khandual , Pasha Tatashin , Miaohe Lin , Minchan Kim , Christoph Hellwig , Song Liu , Thomas Hellstrom , Russell King , "David S. Miller" , Michael Ellerman , "Aneesh Kumar K.V" , Heiko Carstens , Christian Borntraeger , Claudio Imbrenda , Alexander Gordeev , Gerald Schaefer , Vasily Gorbik , Jann Horn , Vishal Moola , Vlastimil Babka , Zi Yan , linux-arm-kernel@lists.infradead.org, sparclinux@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-s390@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v3 13/13] mm/pgtable: notes on pte_offset_map[_lock]() In-Reply-To: <7cd843a9-aa80-14f-5eb2-33427363c20@google.com> Message-ID: References: <7cd843a9-aa80-14f-5eb2-33427363c20@google.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a block of comments on pte_offset_map_lock(), pte_offset_map() and pte_offset_map_nolock() to mm/pgtable-generic.c, to help explain them. Signed-off-by: Hugh Dickins --- mm/pgtable-generic.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/mm/pgtable-generic.c b/mm/pgtable-generic.c index fa9d4d084291..4fcd959dcc4d 100644 --- a/mm/pgtable-generic.c +++ b/mm/pgtable-generic.c @@ -315,6 +315,50 @@ pte_t *pte_offset_map_nolock(struct mm_struct *mm, pmd= _t *pmd, return pte; } =20 +/* + * pte_offset_map_lock(mm, pmd, addr, ptlp), and its internal implementati= on + * __pte_offset_map_lock() below, is usually called with the pmd pointer f= or + * addr, reached by walking down the mm's pgd, p4d, pud for addr: either w= hile + * holding mmap_lock or vma lock for read or for write; or in truncate or = rmap + * context, while holding file's i_mmap_lock or anon_vma lock for read (or= for + * write). In a few cases, it may be used with pmd pointing to a pmd_t alr= eady + * copied to or constructed on the stack. + * + * When successful, it returns the pte pointer for addr, with its page tab= le + * kmapped if necessary (when CONFIG_HIGHPTE), and locked against concurre= nt + * modification by software, with a pointer to that spinlock in ptlp (in s= ome + * configs mm->page_table_lock, in SPLIT_PTLOCK configs a spinlock in tabl= e's + * struct page). pte_unmap_unlock(pte, ptl) to unlock and unmap afterward= s. + * + * But it is unsuccessful, returning NULL with *ptlp unchanged, if there i= s no + * page table at *pmd: if, for example, the page table has just been remov= ed, + * or replaced by the huge pmd of a THP. (When successful, *pmd is rechec= ked + * after acquiring the ptlock, and retried internally if it changed: so th= at a + * page table can be safely removed or replaced by THP while holding its l= ock.) + * + * pte_offset_map(pmd, addr), and its internal helper __pte_offset_map() a= bove, + * just returns the pte pointer for addr, its page table kmapped if necess= ary; + * or NULL if there is no page table at *pmd. It does not attempt to lock= the + * page table, so cannot normally be used when the page table is to be upd= ated, + * or when entries read must be stable. But it does take rcu_read_lock():= so + * that even when page table is racily removed, it remains a valid though = empty + * and disconnected table. Until pte_unmap(pte) unmaps and rcu_read_unloc= k()s + * afterwards. + * + * pte_offset_map_nolock(mm, pmd, addr, ptlp), above, is like pte_offset_m= ap(); + * but when successful, it also outputs a pointer to the spinlock in ptlp = - as + * pte_offset_map_lock() does, but in this case without locking it. This = helps + * the caller to avoid a later pte_lockptr(mm, *pmd), which might by that = time + * act on a changed *pmd: pte_offset_map_nolock() provides the correct spi= nlock + * pointer for the page table that it returns. In principle, the caller s= hould + * recheck *pmd once the lock is taken; in practice, no callsite needs tha= t - + * either the mmap_lock for write, or pte_same() check on contents, is eno= ugh. + * + * Note that free_pgtables(), used after unmapping detached vmas, or when + * exiting the whole mm, does not take page table lock before freeing a pa= ge + * table, and may not use RCU at all: "outsiders" like khugepaged should a= void + * pte_offset_map() and co once the vma is detached from mm or mm_users is= zero. + */ pte_t *__pte_offset_map_lock(struct mm_struct *mm, pmd_t *pmd, unsigned long addr, spinlock_t **ptlp) { --=20 2.35.3