From patchwork Wed Sep 4 08:40:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qi Zheng X-Patchwork-Id: 13790067 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 5F4DECA0ED3 for ; Wed, 4 Sep 2024 08:40:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BF3916B020A; Wed, 4 Sep 2024 04:40:53 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B7D886B020C; Wed, 4 Sep 2024 04:40:53 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9D0426B020B; Wed, 4 Sep 2024 04:40:53 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 7C7BB6B0206 for ; Wed, 4 Sep 2024 04:40:53 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 140C2C0CA4 for ; Wed, 4 Sep 2024 08:40:53 +0000 (UTC) X-FDA: 82526410386.14.CFEDF6A Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) by imf14.hostedemail.com (Postfix) with ESMTP id 1C7F5100004 for ; Wed, 4 Sep 2024 08:40:50 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=SlQDIomu; dmarc=pass (policy=quarantine) header.from=bytedance.com; spf=pass (imf14.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.214.171 as permitted sender) smtp.mailfrom=zhengqi.arch@bytedance.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725439227; a=rsa-sha256; cv=none; b=yXTABA/jcY/tK/eA3f3H2+ZrdThq6WatRBSprUyUynwpa1a9+m4/zQAM49A4rh357MS622 QstjUJNC4UNk+wR4NKhN3Cm71K1QP0s5MrpY0t2j2Gsb2LQBGNidX3nfBJ7+m6d39EP5/X 4OTo499Dp6WKCFqm+9QSfiXOCSfN++8= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=SlQDIomu; dmarc=pass (policy=quarantine) header.from=bytedance.com; spf=pass (imf14.hostedemail.com: domain of zhengqi.arch@bytedance.com designates 209.85.214.171 as permitted sender) smtp.mailfrom=zhengqi.arch@bytedance.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1725439227; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=3ZoTvF0msswRhisxHHwhwQPLPrn7vj0AmYqzYo7APLw=; b=Q3PnZKC9ANWF3gAAEPAtFQz+LAut60SiXXakqa46olaJI377eWgEBbOMZUzWJN+Kgx2Taq GHX7on4UHieEYH0vXc79W6GxpLASgfec3cAFSmpHgytqBa3xWEVjCc/CDBJ5IloEAr22nk xFO9CujccLW4/BAcpMd0NunT1TuFBd4= Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-2055136b612so36608815ad.0 for ; Wed, 04 Sep 2024 01:40:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1725439250; x=1726044050; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=3ZoTvF0msswRhisxHHwhwQPLPrn7vj0AmYqzYo7APLw=; b=SlQDIomuJ5jufXUySAHI3Ydq5+awYW1pG1j7XcYgxjEhTW8qX5uVCpiuFPecqf+P0e LQw5x4U940s37BcMaj+d0WTM8EqxbeJD/uQuLndZqrZcaeRVZMNMezrHZ09C2SwgPIrY IMcnxqhzF5DNnj+8j8UBKPgF72Eww4XC/UceSrwQGqirby1eREPSCvLnOpErsFfl4WFj rUsh/lcZWVDy0Rx4QJ+gZgOyPlniiBWAU8pe39bNMLDVogdZaXaIrg0myDW4alulIaSk N5CAHPgpW28c5eEWzfA52IAIwzidMvQEz+oZ6FSmLLmbZGRsUjMZff5ajwE3UFAfqYD9 gyLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725439250; x=1726044050; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=3ZoTvF0msswRhisxHHwhwQPLPrn7vj0AmYqzYo7APLw=; b=IwHmAXGLlss0T3Nb52cXDbJSrbiV3OTMrveviOogVhBhKseENOT0h/WOheApJByN/N j9oMnKR5XtD3B7w6sa+34OyTJSOLE+6hu5iyO26Z3I659QsveiyLWqfyyXbS215GBZNW Y2TuN5n1XkDBWU9yS9px0Hkvc0elLeDOgYz1G4vweyQ68h0dBoicwIVwgkZjTUrYmTIe LoT7OGoTIGRerLlYnfoijUXHa81/8hFXaJFlHuec/b1rvtv0DRZqgZTm7sfDWaJBd0MH BIYRzsGDM3H6ZvLCC3FhCfCB/4QPTMfNY+K1VDdopLu5HVOUizMIkwtOSJMxnK3gjoxH QOpg== X-Forwarded-Encrypted: i=1; AJvYcCV0IR2oKlUcPewUD0jDi9VqWAcPGfprb852C3M1F0+qQiLQBzb91HZZbw//xsiWqUJrK9UD7mcWyw==@kvack.org X-Gm-Message-State: AOJu0YyvbGicQxogwKNzsA8z94qsImm7ETkR8fxmocal9zc5OWBkkFQN qe5rdgx2fhaI+rKhUenQFywVxu2G5fmykxccqgxiD8AF+7UxIOAkuJpARQ+vVk0= X-Google-Smtp-Source: AGHT+IHLu4af/0RdmwIJrtep3I3PIUPy/4FB7EG8zsCDexMsa/uyvNrQYTXwy/3KiLtDhw4LWBklXg== X-Received: by 2002:a17:902:ce0b:b0:202:3dcf:8c38 with SMTP id d9443c01a7336-20546b35d3fmr190359795ad.44.1725439249775; Wed, 04 Sep 2024 01:40:49 -0700 (PDT) Received: from C02DW0BEMD6R.bytedance.net ([139.177.225.242]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-206ae95a51csm9414045ad.117.2024.09.04.01.40.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Sep 2024 01:40:49 -0700 (PDT) From: Qi Zheng To: david@redhat.com, hughd@google.com, willy@infradead.org, muchun.song@linux.dev, vbabka@kernel.org, akpm@linux-foundation.org, rppt@kernel.org, vishal.moola@gmail.com, peterx@redhat.com, ryan.roberts@arm.com, christophe.leroy2@cs-soprasteria.com Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-arm-kernel@lists.infradead.org, linuxppc-dev@lists.ozlabs.org, Qi Zheng Subject: [PATCH v3 01/14] mm: pgtable: introduce pte_offset_map_{ro|rw}_nolock() Date: Wed, 4 Sep 2024 16:40:09 +0800 Message-Id: <20240904084022.32728-2-zhengqi.arch@bytedance.com> X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: <20240904084022.32728-1-zhengqi.arch@bytedance.com> References: <20240904084022.32728-1-zhengqi.arch@bytedance.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: 1C7F5100004 X-Rspamd-Server: rspam01 X-Stat-Signature: 1gqb5x4u17w3rx7e8bjkzozwmtu1t97f X-HE-Tag: 1725439250-839016 X-HE-Meta: U2FsdGVkX1/SkJtGQDP34yZuKhWz+72/akEEdFy6jeY/CPRZwz1HsC9FZdO4XNeiDwwbIWqSQ6DSze0amhzOEsDh/AGxJyhTYSfA/7RZ0Ipc4k7gRO78Dvq9JHkzM9QX6QY+GwB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IuBRuhpKA/SnfNiemH4yy6tV7OGUGjADIg9WqogTd8RraQOhr00rRMJsB9Gq/BEGiJLgWGM5i01MttAldFmFb8txr96uqKT4CXP069d7UxZbamyOgfgpqdu5pmr74SdpLKGZq9wf8Zd5J1LWF2L9jV0T/nGNVMhfC7cq9ce8kmNeEeacy2ln6IlkvO7lPh7PYirhwyRONb5olsagdQucIA== 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: Currently, the usage of pte_offset_map_nolock() can be divided into the following two cases: 1) After acquiring PTL, only read-only operations are performed on the PTE page. In this case, the RCU lock in pte_offset_map_nolock() will ensure that the PTE page will not be freed, and there is no need to worry about whether the pmd entry is modified. 2) After acquiring PTL, the pte or pmd entries may be modified. At this time, we need to ensure that the pmd entry has not been modified concurrently. To more clearing distinguish between these two cases, this commit introduces two new helper functions to replace pte_offset_map_nolock(). For 1), just rename it to pte_offset_map_ro_nolock(). For 2), in addition to changing the name to pte_offset_map_rw_nolock(), it also outputs the pmdval when successful. It is applicable for may-write cases where any modification operations to the page table may happen after the corresponding spinlock is held afterwards. But the users should make sure the page table is stable like checking pte_same() or checking pmd_same() by using the output pmdval before performing the write operations. Note: "RO" / "RW" expresses the intended semantics, not that the *kmap* will be read-only/read-write protected. Subsequent commits will convert pte_offset_map_nolock() into the above two functions one by one, and finally completely delete it. Signed-off-by: Qi Zheng --- Documentation/mm/split_page_table_lock.rst | 7 +++ include/linux/mm.h | 5 +++ mm/pgtable-generic.c | 50 ++++++++++++++++++++++ 3 files changed, 62 insertions(+) diff --git a/Documentation/mm/split_page_table_lock.rst b/Documentation/mm/split_page_table_lock.rst index e4f6972eb6c04..08d0e706a32db 100644 --- a/Documentation/mm/split_page_table_lock.rst +++ b/Documentation/mm/split_page_table_lock.rst @@ -19,6 +19,13 @@ There are helpers to lock/unlock a table and other accessor functions: - pte_offset_map_nolock() maps PTE, returns pointer to PTE with pointer to its PTE table lock (not taken), or returns NULL if no PTE table; + - pte_offset_map_ro_nolock() + maps PTE, returns pointer to PTE with pointer to its PTE table + lock (not taken), or returns NULL if no PTE table; + - pte_offset_map_rw_nolock() + maps PTE, returns pointer to PTE with pointer to its PTE table + lock (not taken) and the value of its pmd entry, or returns NULL + if no PTE table; - pte_offset_map() maps PTE, returns pointer to PTE, or returns NULL if no PTE table; - pte_unmap() diff --git a/include/linux/mm.h b/include/linux/mm.h index a7c74a840249a..1fde9242231c9 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -3006,6 +3006,11 @@ static inline pte_t *pte_offset_map_lock(struct mm_struct *mm, pmd_t *pmd, pte_t *pte_offset_map_nolock(struct mm_struct *mm, pmd_t *pmd, unsigned long addr, spinlock_t **ptlp); +pte_t *pte_offset_map_ro_nolock(struct mm_struct *mm, pmd_t *pmd, + unsigned long addr, spinlock_t **ptlp); +pte_t *pte_offset_map_rw_nolock(struct mm_struct *mm, pmd_t *pmd, + unsigned long addr, pmd_t *pmdvalp, + spinlock_t **ptlp); #define pte_unmap_unlock(pte, ptl) do { \ spin_unlock(ptl); \ diff --git a/mm/pgtable-generic.c b/mm/pgtable-generic.c index a78a4adf711ac..262b7065a5a2e 100644 --- a/mm/pgtable-generic.c +++ b/mm/pgtable-generic.c @@ -317,6 +317,33 @@ pte_t *pte_offset_map_nolock(struct mm_struct *mm, pmd_t *pmd, return pte; } +pte_t *pte_offset_map_ro_nolock(struct mm_struct *mm, pmd_t *pmd, + unsigned long addr, spinlock_t **ptlp) +{ + pmd_t pmdval; + pte_t *pte; + + pte = __pte_offset_map(pmd, addr, &pmdval); + if (likely(pte)) + *ptlp = pte_lockptr(mm, &pmdval); + return pte; +} + +pte_t *pte_offset_map_rw_nolock(struct mm_struct *mm, pmd_t *pmd, + unsigned long addr, pmd_t *pmdvalp, + spinlock_t **ptlp) +{ + pmd_t pmdval; + pte_t *pte; + + VM_WARN_ON_ONCE(!pmdvalp); + pte = __pte_offset_map(pmd, addr, &pmdval); + if (likely(pte)) + *ptlp = pte_lockptr(mm, &pmdval); + *pmdvalp = pmdval; + return pte; +} + /* * pte_offset_map_lock(mm, pmd, addr, ptlp), and its internal implementation * __pte_offset_map_lock() below, is usually called with the pmd pointer for @@ -356,6 +383,29 @@ pte_t *pte_offset_map_nolock(struct mm_struct *mm, pmd_t *pmd, * recheck *pmd once the lock is taken; in practice, no callsite needs that - * either the mmap_lock for write, or pte_same() check on contents, is enough. * + * pte_offset_map_ro_nolock(mm, pmd, addr, ptlp), above, is like pte_offset_map(); + * 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_ro_nolock() provides the correct spinlock + * pointer for the page table that it returns. Even after grabbing the spinlock, + * we might be looking either at a page table that is still mapped or one that + * was unmapped and is about to get freed. But for R/O access this is sufficient. + * So it is only applicable for read-only cases where any modification operations + * to the page table are not allowed even if the corresponding spinlock is held + * afterwards. + * + * pte_offset_map_rw_nolock(mm, pmd, addr, pmdvalp, ptlp), above, is like + * pte_offset_map_ro_nolock(); but when successful, it also outputs the pdmval. + * It is applicable for may-write cases where any modification operations to the + * page table may happen after the corresponding spinlock is held afterwards. + * But the users should make sure the page table is stable like checking pte_same() + * or checking pmd_same() by using the output pmdval before performing the write + * operations. + * + * Note: "RO" / "RW" expresses the intended semantics, not that the *kmap* will + * 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