From patchwork Mon Jul 1 08:46:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qi Zheng X-Patchwork-Id: 13717667 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 EA902C2BD09 for ; Mon, 1 Jul 2024 08:48:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 54DA56B00A3; Mon, 1 Jul 2024 04:48:12 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4FFC86B00A4; Mon, 1 Jul 2024 04:48:12 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 376E06B00A5; Mon, 1 Jul 2024 04:48:12 -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 17DA66B00A3 for ; Mon, 1 Jul 2024 04:48:12 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id C6AA8121AC9 for ; Mon, 1 Jul 2024 08:48:11 +0000 (UTC) X-FDA: 82290556782.05.F0E0DB5 Received: from mail-il1-f175.google.com (mail-il1-f175.google.com [209.85.166.175]) by imf21.hostedemail.com (Postfix) with ESMTP id 088271C001F for ; Mon, 1 Jul 2024 08:48:09 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b="ko3Kd/is"; spf=none (imf21.hostedemail.com: domain of zhengqi.arch@bytedance.com has no SPF policy when checking 209.85.166.175) smtp.mailfrom=zhengqi.arch@bytedance.com; dmarc=pass (policy=quarantine) header.from=bytedance.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1719823673; 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=/FEjQRmnZwv5IniDvqPInjdkphsULH24MtgWxf3WNnc=; b=jTSDRadLMQJPisq0fRBcEuz76C+awLD1nrgH5ME9jH8VzCBKOJe2QG0mR3Izrr3zRIlOui gjVvkmQ9EgT4ZchvCT7vnjh/AX84NVWrkMiIOEMZtPDC0ReCMF2Kb22+8Fg/ZQf1qeBkxS 1P22hkZG5f74C8CsnjYTDIFbU0jVyrg= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b="ko3Kd/is"; spf=none (imf21.hostedemail.com: domain of zhengqi.arch@bytedance.com has no SPF policy when checking 209.85.166.175) smtp.mailfrom=zhengqi.arch@bytedance.com; dmarc=pass (policy=quarantine) header.from=bytedance.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1719823673; a=rsa-sha256; cv=none; b=07yMEBeCduou4p9q1NhxXLQYnPwyq1ky1bYbhvsJ+wmqWk6ZwIi4ruvBgqjD5oaiRnPsIw qQZKSv5ot1p5cTv54iltaLEItVGKJvYW/PlRI8ASLd055gKOdxEI3PKiJxvm3nN/lqzZaH 4sRY+yz7pyOBUW0V6Rua1ANIw+0SHSg= Received: by mail-il1-f175.google.com with SMTP id e9e14a558f8ab-375df7da74cso1331205ab.1 for ; Mon, 01 Jul 2024 01:48:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1719823689; x=1720428489; 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=/FEjQRmnZwv5IniDvqPInjdkphsULH24MtgWxf3WNnc=; b=ko3Kd/isyYlsyF3LpLjKoYRHSJgzUFdVwaoG429fbur1mQkGLaL/jfzxOA3b/n2hWx zkjAmTpVQS32b7i0AtlYnmHCP7bxjI2MPGwFYe3Tkvgu/FPIBSX+GmzsWi5Gwqx53408 i99CA+Oem7vFJA/eXoxs4PsJz/HyMy6ZhOTePLlXJDgfq2Jo5BdLozBXMxdh0dzWa6tx R7umjNJqi0sSK/D6FSCwSOESRUziqPSCRSFIsATAY8+p+W82S5jLZkDojjeoQbp5K5b2 RFoCllY+xGtjwvs4eilWyy5tAnW9j/r/X+HoVSLc6+vePT6iluPn4IQXkS7zWNg02doc oO6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719823689; x=1720428489; 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=/FEjQRmnZwv5IniDvqPInjdkphsULH24MtgWxf3WNnc=; b=MGdxyEX383LQ8ymSTGg2JmLR1qJCHD+VaAJQQdDNOMLg4Nudvj9HJ09SPTEp+G/Tnx dasIiLFQPbKEIk1sXq0uUlJ6nlx019BJvJeZPOnjwB8ZAcVylkUDs/tviqqm0xABUZiS f+7mB6sTCorkBlNdqGzmvKIZAiwcJVlthmOpcS2JmUpTc9eClzIj4Iz8UfHGswoYnb8D ZmC9Nvqgyoyk8KksHnEZriLy5aWT9NHRnIPBJ6oDsVt4XBV7/tsj/GEyPwgcgqS5paOw fjA/iKh4BMf6IPlEOxg1hPQIn0z4z6YGlugG8aAJTGuCxLe4wu7mPDw/C11HBqZVaDjg 9EbQ== X-Gm-Message-State: AOJu0YwI2Xu46p3l2Cw+2UINFEC1N8O96i0Anlq82Iig9w198c9pkc7C kfoX33zpIr/tAHqc+n+M+DUYTXNmOcmjzABAF7CF22o8mhbMJoJXaTW4tBOKzDU= X-Google-Smtp-Source: AGHT+IFNREJpAukQmSF+e3y8DudZ+fBumzStMqKfWS9xBvY6s+QthbSABnLkfofffgxBCHUpAfSNuw== X-Received: by 2002:a5d:9b8e:0:b0:7f3:a20e:c38f with SMTP id ca18e2360f4ac-7f62ed4cf57mr403632739f.0.1719823688743; Mon, 01 Jul 2024 01:48:08 -0700 (PDT) Received: from C02DW0BEMD6R.bytedance.net ([139.177.225.241]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-70804a7e7e0sm5932374b3a.204.2024.07.01.01.48.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Jul 2024 01:48:08 -0700 (PDT) From: Qi Zheng To: david@redhat.com, hughd@google.com, willy@infradead.org, mgorman@suse.de, muchun.song@linux.dev, akpm@linux-foundation.org Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Qi Zheng Subject: [RFC PATCH 1/7] mm: pgtable: make pte_offset_map_nolock() return pmdval Date: Mon, 1 Jul 2024 16:46:42 +0800 Message-Id: <7f5233f9f612c7f58abf218852fb1042d764940b.1719570849.git.zhengqi.arch@bytedance.com> X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: References: MIME-Version: 1.0 X-Stat-Signature: 91kgu95n9do14j484jd1hmyog9c1hfok X-Rspam-User: X-Rspamd-Queue-Id: 088271C001F X-Rspamd-Server: rspam02 X-HE-Tag: 1719823689-878206 X-HE-Meta: U2FsdGVkX19XRygofGjsR9597yLcx3fXeERasVDGARCydqKygsYdr+PlcpLv+3cqOw/YQDf2zMnq0lNJNFBcoZhSZWGItMyRIKNvqOm9gLUaX3HfRNcxOkuha8qmWywgHCRLJKRiMa/S6KUd83dEzM9Sm7dCHmhdM6Il+gFLfsVCkDeJ6xSvT4j7pu5e2ttSbus4z+I/rtauV7pDmTvSJAf0IoiJzf8PNiTrDDWYTbcEFU+DJKNi8lyXKY2Cw79ih6uzA3T33gncACtqWz17bXbKlPN/a+/HXSay6+cc2hmwd9ME5NCN9h4costRCjp0lqVwxdv0ZDViHhvwFd4y0lTMqW7xkmdA0cfuyli5Lgayk5Gb/PowlaN5sDe2eD95DOJ2B2VaRA3OTWSTB5iWlOoOG9XGvFJxuGJQDYxB3T/zaT6dHkWvintbv7sZVQrL01R+RQ5DvTffkBR+L4MukRQhczB0iEIKeewBxp/RtUD5yTgGywsESs6sLOAQ1W2sXC4WwE+3j/QgwwxuehZ6CWQ/iuG5YV/aGFWq+M/mbhxFVoInUyjj2D1qHR/kKN3BYB7s/YgW1ErXpYLlSxlsjxLhlINkqFD8xD+db1grtVeBIOrR8v+ocjPd4DZMlfzvU7nczLDz95V07wEuCxKwhMcfHG3TOcXYboCezhnqabDYHGKgid+gOmCH+x+vZ0/jIjVYcZm9hOdNyibC7yRxWIExP0Tn+GmryZOd4AkF1NlOXJgCFW0XpGzBTjxr/4lRbEjui+QBxCr42uQgtvJnzuGrJRZfjEGkeX9RXlCajPDzT3djlf/lbNBlVxlhWIzMO1I6KjzkvurSvHWmtlqoqZqVdyiIsAwwyo6JAisWen3d2hYyKmyY3XU9GZTxeo+yrAPdm7ErIYnIKLtuDR+vXd8X0eVRqAb7zXv+WnF6FEH7v23uv8vBe0j370SEtqYRi80AnKAN1K16q5kwM4L FSwj53Os tOyFj0uavdERIG3w1E7L1WEFWa9ddhHSNXc0RXk9SFkChlsupVXpptN3/dyA8OauWCmzwugvOWpBPEu6FHYIo1xVAoLywD3KCpseTZ3mmk9FVHhTXK4WRgPPe3vwkRCHRJfkZZbkBP1psDRAedAB9bCKWxk1xuoYKfDMDPE/xj49f7m0NJFU4mZhRAZegICSLMJ9O1YdNEqdADcuVJ8hIxUFV02El98Ct//amYs/CzEzi6iCMELZ86r/WlqyJwExfIuLKHlrLIom0be/ebdu7ELFXYUAGgY6IJN5lWZu3FCM/xhW636LueJIblSVJTkkBvhV3C8p+onoBOfa3MJXBthOXNmTfn5L2G47V X-Bogosity: Ham, tests=bogofilter, spamicity=0.000001, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Make pte_offset_map_nolock() return pmdval so that we can recheck the *pmd once the lock is taken. This is a preparation for freeing empty PTE pages, no functional changes are expected. Signed-off-by: Qi Zheng --- Documentation/mm/split_page_table_lock.rst | 3 ++- arch/arm/mm/fault-armv.c | 2 +- arch/powerpc/mm/pgtable.c | 2 +- include/linux/mm.h | 4 ++-- mm/filemap.c | 2 +- mm/khugepaged.c | 4 ++-- mm/memory.c | 4 ++-- mm/mremap.c | 2 +- mm/page_vma_mapped.c | 2 +- mm/pgtable-generic.c | 21 ++++++++++++--------- mm/userfaultfd.c | 4 ++-- mm/vmscan.c | 2 +- 12 files changed, 28 insertions(+), 24 deletions(-) diff --git a/Documentation/mm/split_page_table_lock.rst b/Documentation/mm/split_page_table_lock.rst index e4f6972eb6c0..e6a47d57531c 100644 --- a/Documentation/mm/split_page_table_lock.rst +++ b/Documentation/mm/split_page_table_lock.rst @@ -18,7 +18,8 @@ There are helpers to lock/unlock a table and other accessor functions: pointer to its PTE table lock, or returns NULL if no PTE table; - 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; + 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/arch/arm/mm/fault-armv.c b/arch/arm/mm/fault-armv.c index 2286c2ea60ec..3e4ed99b9330 100644 --- a/arch/arm/mm/fault-armv.c +++ b/arch/arm/mm/fault-armv.c @@ -117,7 +117,7 @@ static int adjust_pte(struct vm_area_struct *vma, unsigned long address, * must use the nested version. This also means we need to * open-code the spin-locking. */ - pte = pte_offset_map_nolock(vma->vm_mm, pmd, address, &ptl); + pte = pte_offset_map_nolock(vma->vm_mm, pmd, NULL, address, &ptl); if (!pte) return 0; diff --git a/arch/powerpc/mm/pgtable.c b/arch/powerpc/mm/pgtable.c index 9e7ba9c3851f..ab0250f1b226 100644 --- a/arch/powerpc/mm/pgtable.c +++ b/arch/powerpc/mm/pgtable.c @@ -350,7 +350,7 @@ void assert_pte_locked(struct mm_struct *mm, unsigned long addr) */ if (pmd_none(*pmd)) return; - pte = pte_offset_map_nolock(mm, pmd, addr, &ptl); + pte = pte_offset_map_nolock(mm, pmd, NULL, addr, &ptl); BUG_ON(!pte); assert_spin_locked(ptl); pte_unmap(pte); diff --git a/include/linux/mm.h b/include/linux/mm.h index 7d044e737dba..396bdc3b3726 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2979,8 +2979,8 @@ static inline pte_t *pte_offset_map_lock(struct mm_struct *mm, pmd_t *pmd, return pte; } -pte_t *pte_offset_map_nolock(struct mm_struct *mm, pmd_t *pmd, - unsigned long addr, spinlock_t **ptlp); +pte_t *pte_offset_map_nolock(struct mm_struct *mm, pmd_t *pmd, pmd_t *pmdvalp, + unsigned long addr, spinlock_t **ptlp); #define pte_unmap_unlock(pte, ptl) do { \ spin_unlock(ptl); \ diff --git a/mm/filemap.c b/mm/filemap.c index 6835977ee99a..35bbba960447 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -3231,7 +3231,7 @@ static vm_fault_t filemap_fault_recheck_pte_none(struct vm_fault *vmf) if (!(vmf->flags & FAULT_FLAG_ORIG_PTE_VALID)) return 0; - ptep = pte_offset_map_nolock(vma->vm_mm, vmf->pmd, vmf->address, + ptep = pte_offset_map_nolock(vma->vm_mm, vmf->pmd, NULL, vmf->address, &vmf->ptl); if (unlikely(!ptep)) return VM_FAULT_NOPAGE; diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 2e017585f813..7b7c858d5f99 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -989,7 +989,7 @@ static int __collapse_huge_page_swapin(struct mm_struct *mm, }; if (!pte++) { - pte = pte_offset_map_nolock(mm, pmd, address, &ptl); + pte = pte_offset_map_nolock(mm, pmd, NULL, address, &ptl); if (!pte) { mmap_read_unlock(mm); result = SCAN_PMD_NULL; @@ -1578,7 +1578,7 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr, if (userfaultfd_armed(vma) && !(vma->vm_flags & VM_SHARED)) pml = pmd_lock(mm, pmd); - start_pte = pte_offset_map_nolock(mm, pmd, haddr, &ptl); + start_pte = pte_offset_map_nolock(mm, pmd, NULL, haddr, &ptl); if (!start_pte) /* mmap_lock + page lock should prevent this */ goto abort; if (!pml) diff --git a/mm/memory.c b/mm/memory.c index 0a769f34bbb2..1c9068b0b067 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -1108,7 +1108,7 @@ copy_pte_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma, ret = -ENOMEM; goto out; } - src_pte = pte_offset_map_nolock(src_mm, src_pmd, addr, &src_ptl); + src_pte = pte_offset_map_nolock(src_mm, src_pmd, NULL, addr, &src_ptl); if (!src_pte) { pte_unmap_unlock(dst_pte, dst_ptl); /* ret == 0 */ @@ -5507,7 +5507,7 @@ static vm_fault_t handle_pte_fault(struct vm_fault *vmf) * it into a huge pmd: just retry later if so. */ vmf->pte = pte_offset_map_nolock(vmf->vma->vm_mm, vmf->pmd, - vmf->address, &vmf->ptl); + NULL, vmf->address, &vmf->ptl); if (unlikely(!vmf->pte)) return 0; vmf->orig_pte = ptep_get_lockless(vmf->pte); diff --git a/mm/mremap.c b/mm/mremap.c index e7ae140fc640..f672d0218a6f 100644 --- a/mm/mremap.c +++ b/mm/mremap.c @@ -175,7 +175,7 @@ static int move_ptes(struct vm_area_struct *vma, pmd_t *old_pmd, err = -EAGAIN; goto out; } - new_pte = pte_offset_map_nolock(mm, new_pmd, new_addr, &new_ptl); + new_pte = pte_offset_map_nolock(mm, new_pmd, NULL, new_addr, &new_ptl); if (!new_pte) { pte_unmap_unlock(old_pte, old_ptl); err = -EAGAIN; diff --git a/mm/page_vma_mapped.c b/mm/page_vma_mapped.c index ae5cc42aa208..507701b7bcc1 100644 --- a/mm/page_vma_mapped.c +++ b/mm/page_vma_mapped.c @@ -33,7 +33,7 @@ static bool map_pte(struct page_vma_mapped_walk *pvmw, spinlock_t **ptlp) * Though, in most cases, page lock already protects this. */ pvmw->pte = pte_offset_map_nolock(pvmw->vma->vm_mm, pvmw->pmd, - pvmw->address, ptlp); + NULL, pvmw->address, ptlp); if (!pvmw->pte) return false; diff --git a/mm/pgtable-generic.c b/mm/pgtable-generic.c index a78a4adf711a..443e3b34434a 100644 --- a/mm/pgtable-generic.c +++ b/mm/pgtable-generic.c @@ -305,7 +305,7 @@ pte_t *__pte_offset_map(pmd_t *pmd, unsigned long addr, pmd_t *pmdvalp) return NULL; } -pte_t *pte_offset_map_nolock(struct mm_struct *mm, pmd_t *pmd, +pte_t *pte_offset_map_nolock(struct mm_struct *mm, pmd_t *pmd, pmd_t *pmdvalp, unsigned long addr, spinlock_t **ptlp) { pmd_t pmdval; @@ -314,6 +314,8 @@ pte_t *pte_offset_map_nolock(struct mm_struct *mm, pmd_t *pmd, pte = __pte_offset_map(pmd, addr, &pmdval); if (likely(pte)) *ptlp = pte_lockptr(mm, &pmdval); + if (pmdvalp) + *pmdvalp = pmdval; return pte; } @@ -347,14 +349,15 @@ pte_t *pte_offset_map_nolock(struct mm_struct *mm, pmd_t *pmd, * and disconnected table. Until pte_unmap(pte) unmaps and rcu_read_unlock()s * afterwards. * - * pte_offset_map_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_nolock() provides the correct spinlock - * pointer for the page table that it returns. In principle, the caller should - * 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_nolock(mm, pmd, pmdvalp, 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_nolock() + * provides the correct spinlock pointer for the page table that it returns. + * In principle, the caller should recheck *pmd once the lock is taken; But in + * most cases, either the mmap_lock for write, or pte_same() check on contents, + * is enough. * * 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 diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c index 8dedaec00486..61c1d228d239 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -1143,7 +1143,7 @@ static int move_pages_pte(struct mm_struct *mm, pmd_t *dst_pmd, pmd_t *src_pmd, src_addr, src_addr + PAGE_SIZE); mmu_notifier_invalidate_range_start(&range); retry: - dst_pte = pte_offset_map_nolock(mm, dst_pmd, dst_addr, &dst_ptl); + dst_pte = pte_offset_map_nolock(mm, dst_pmd, NULL, dst_addr, &dst_ptl); /* Retry if a huge pmd materialized from under us */ if (unlikely(!dst_pte)) { @@ -1151,7 +1151,7 @@ static int move_pages_pte(struct mm_struct *mm, pmd_t *dst_pmd, pmd_t *src_pmd, goto out; } - src_pte = pte_offset_map_nolock(mm, src_pmd, src_addr, &src_ptl); + src_pte = pte_offset_map_nolock(mm, src_pmd, NULL, src_addr, &src_ptl); /* * We held the mmap_lock for reading so MADV_DONTNEED diff --git a/mm/vmscan.c b/mm/vmscan.c index 3d4c681c6d40..c9a4cd31e6b4 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -3373,7 +3373,7 @@ static bool walk_pte_range(pmd_t *pmd, unsigned long start, unsigned long end, DEFINE_MAX_SEQ(walk->lruvec); int old_gen, new_gen = lru_gen_from_seq(max_seq); - pte = pte_offset_map_nolock(args->mm, pmd, start & PMD_MASK, &ptl); + pte = pte_offset_map_nolock(args->mm, pmd, NULL, start & PMD_MASK, &ptl); if (!pte) return false; if (!spin_trylock(ptl)) {