From nobody Sat Dec 27 18:53:30 2025 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 909BB199CC for ; Mon, 18 Dec 2023 10:51:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 754E92F4; Mon, 18 Dec 2023 02:52:06 -0800 (PST) Received: from e125769.cambridge.arm.com (e125769.cambridge.arm.com [10.1.196.26]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 9BE873F738; Mon, 18 Dec 2023 02:51:18 -0800 (PST) From: Ryan Roberts To: Catalin Marinas , Will Deacon , Ard Biesheuvel , Marc Zyngier , Oliver Upton , James Morse , Suzuki K Poulose , Zenghui Yu , Andrey Ryabinin , Alexander Potapenko , Andrey Konovalov , Dmitry Vyukov , Vincenzo Frascino , Andrew Morton , Anshuman Khandual , Matthew Wilcox , Yu Zhao , Mark Rutland , David Hildenbrand , Kefeng Wang , John Hubbard , Zi Yan , Barry Song <21cnbao@gmail.com>, Alistair Popple , Yang Shi Cc: Ryan Roberts , linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 01/16] mm: thp: Batch-collapse PMD with set_ptes() Date: Mon, 18 Dec 2023 10:50:45 +0000 Message-Id: <20231218105100.172635-2-ryan.roberts@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20231218105100.172635-1-ryan.roberts@arm.com> References: <20231218105100.172635-1-ryan.roberts@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Refactor __split_huge_pmd_locked() so that a present PMD can be collapsed to PTEs in a single batch using set_ptes(). It also provides a future opportunity to batch-add the folio to the rmap using David's new batched rmap APIs. This should improve performance a little bit, but the real motivation is to remove the need for the arm64 backend to have to fold the contpte entries. Instead, since the ptes are set as a batch, the contpte blocks can be initially set up pre-folded (once the arm64 contpte support is added in the next few patches). This leads to noticeable performance improvement during split. Signed-off-by: Ryan Roberts Acked-by: David Hildenbrand --- mm/huge_memory.c | 59 ++++++++++++++++++++++++++++-------------------- 1 file changed, 34 insertions(+), 25 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 6be1a380a298..fbf7e95ea983 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -2535,15 +2535,16 @@ static void __split_huge_pmd_locked(struct vm_area_= struct *vma, pmd_t *pmd, =20 pte =3D pte_offset_map(&_pmd, haddr); VM_BUG_ON(!pte); - for (i =3D 0, addr =3D haddr; i < HPAGE_PMD_NR; i++, addr +=3D PAGE_SIZE)= { - pte_t entry; - /* - * Note that NUMA hinting access restrictions are not - * transferred to avoid any possibility of altering - * permissions across VMAs. - */ - if (freeze || pmd_migration) { + + /* + * Note that NUMA hinting access restrictions are not transferred to + * avoid any possibility of altering permissions across VMAs. + */ + if (freeze || pmd_migration) { + for (i =3D 0, addr =3D haddr; i < HPAGE_PMD_NR; i++, addr +=3D PAGE_SIZE= ) { + pte_t entry; swp_entry_t swp_entry; + if (write) swp_entry =3D make_writable_migration_entry( page_to_pfn(page + i)); @@ -2562,28 +2563,36 @@ static void __split_huge_pmd_locked(struct vm_area_= struct *vma, pmd_t *pmd, entry =3D pte_swp_mksoft_dirty(entry); if (uffd_wp) entry =3D pte_swp_mkuffd_wp(entry); - } else { - entry =3D mk_pte(page + i, READ_ONCE(vma->vm_page_prot)); - if (write) - entry =3D pte_mkwrite(entry, vma); + + VM_WARN_ON(!pte_none(ptep_get(pte + i))); + set_pte_at(mm, addr, pte + i, entry); + } + } else { + pte_t entry; + + entry =3D mk_pte(page, READ_ONCE(vma->vm_page_prot)); + if (write) + entry =3D pte_mkwrite(entry, vma); + if (!young) + entry =3D pte_mkold(entry); + /* NOTE: this may set soft-dirty too on some archs */ + if (dirty) + entry =3D pte_mkdirty(entry); + if (soft_dirty) + entry =3D pte_mksoft_dirty(entry); + if (uffd_wp) + entry =3D pte_mkuffd_wp(entry); + + for (i =3D 0, addr =3D haddr; i < HPAGE_PMD_NR; i++, addr +=3D PAGE_SIZE= ) { if (anon_exclusive) SetPageAnonExclusive(page + i); - if (!young) - entry =3D pte_mkold(entry); - /* NOTE: this may set soft-dirty too on some archs */ - if (dirty) - entry =3D pte_mkdirty(entry); - if (soft_dirty) - entry =3D pte_mksoft_dirty(entry); - if (uffd_wp) - entry =3D pte_mkuffd_wp(entry); page_add_anon_rmap(page + i, vma, addr, RMAP_NONE); + VM_WARN_ON(!pte_none(ptep_get(pte + i))); } - VM_BUG_ON(!pte_none(ptep_get(pte))); - set_pte_at(mm, addr, pte, entry); - pte++; + + set_ptes(mm, haddr, pte, entry, HPAGE_PMD_NR); } - pte_unmap(pte - 1); + pte_unmap(pte); =20 if (!pmd_migration) page_remove_rmap(page, vma, true); --=20 2.25.1