From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9A0AC606A32D for ; Wed, 2 Dec 2026 09:56:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=sZqzEKKg Received: by kanga.kvack.org (Postfix) id 6DDF36B0612; Wed, 2 Dec 2026 04:56:37 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6B57D6B0616; Wed, 2 Dec 2026 04:56:37 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5CB0D6B0617; Wed, 2 Dec 2026 04:56:37 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 387DA6B0612 for ; Wed, 2 Dec 2026 04:56:37 -0500 (EST) Received: from smtpin09.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay07.hostedemail.com (Postfix) with ESMTP id C6EA5160232 for ; Wed, 2 Dec 2026 09:56:35 +0000 (UTC) X-FDA: 85498588350.09.37CD700 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf24.hostedemail.com (Postfix) with ESMTP id A3652180003 for ; Wed, 2 Dec 2026 09:56:33 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=arm.com header.s=foss header.b=sZqzEKKg; spf=pass (imf24.hostedemail.com: domain of dev.jain@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=dev.jain@arm.com; dmarc=pass (policy=none) header.from=arm.com ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796205394; b=kLpcJogw3PCIjcnueY/KaVielFe7Dsjyh1H5I7opJNY2MjWVtEzJXnuzTrsMVk/S6ZvMhy pmjxOAs3d/GVufF1/3sJXrT8rv//rbjMgEpZ9iUNJ/x1xMbVtPko7GA6hQel4rO1qj0oZg Fk5xAWMihvzRKyxF0JByEQv7lag5z8E= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796205394; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=xOW4VYWGAjNP1Hc47Cv5b/Dc00Rfa+HT8v8/dYY1njU=; b=Ato1W+N6ALxICkL+ckUx4I5w6moJPwsyXiQA+RgrhK2QHKZopY4r4KhK/m4hwTJeGMdsPV +IXTIBeV44MwVwKlIXH33AyZOpu2Ip+61i4VC/R4oY8rncCYzUdnccJQ0Z/C45ZgiQRdEm eZzFn1suTJv1umy+HHd+0gM9NCs3Cik= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=arm.com header.s=foss header.b=sZqzEKKg; spf=pass (imf24.hostedemail.com: domain of dev.jain@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=dev.jain@arm.com; dmarc=pass (policy=none) header.from=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 D73E51570; Wed, 2 Dec 2026 01:56:29 -0800 (PST) Received: from [10.164.19.62] (unknown [10.164.19.62]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 645C03F7D8; Wed, 2 Dec 2026 01:56:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1796205392; bh=gupAwi2tKWKV9tdtTSEu5+hhm7Y/5/dIIsz5d5/tz5E=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=sZqzEKKgxUK4TvLbNyrYq7qnBq8lihB7bYjum6ftiqS4ITK5aFoVNvqj7+G3H+pYX 4nyxElXsmBkXN6GKrmg+mykdcLkj+JVIB7VTF82zZrcKBk/J2VQgJ9Vlug/cLpQxA9 Oj99TufjCEZaPimz6/nh0x+tJh70M0YSlV2XcmUs= Message-ID: <6df7a7fd-8e82-40b8-ade0-74147a4e2706@arm.com> Date: Wed, 2 Dec 2026 15:26:23 +0530 X-Archived-By: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v5 8/8] mm/rmap: batch unmap anonymous swap-backed large folios To: "David Hildenbrand (Arm)" , ljs@kernel.org, akpm@linux-foundation.org, hughd@google.com, chrisl@kernel.org, kasong@tencent.com Cc: riel@surriel.com, liam@infradead.org, vbabka@kernel.org, harry@kernel.org, jannh@google.com, lance.yang@linux.dev, baolin.wang@linux.alibaba.com, shikemeng@huaweicloud.com, nphamcs@gmail.com, baoquan.he@linux.dev, baohua@kernel.org, youngjun.park@lge.com, linux-mm@kvack.org, rppt@kernel.org, surenb@google.com, mhocko@suse.com, pfalcato@suse.de, ryan.roberts@arm.com, anshuman.khandual@arm.com References: <20261123053543.295467-1-dev.jain@arm.com> <20261123053543.295467-9-dev.jain@arm.com> Content-Language: en-US From: Dev Jain In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: A3652180003 X-Stat-Signature: kogoaz4iamu79qf76zirn8c5zw8psqn8 X-Rspam-User: X-Spam-Status: No, score=-17.3 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS, SPF_PASS,USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=4.0.1 X-HE-Tag: 1796205393-67684 X-HE-Meta: U2FsdGVkX18pvlcyaag6jDamANqSfJTvoWeNJTIj94o6Y35YYG5ezh2nI8R3M9TrvFX71xb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6dooxJQq/KAxbO05QjUZ9a67ZhfFyp0KkpN9Gr55SsDjpONQJa6Q0plAJlgtrJyNjPG1qFvpT7uRqIklR5mg5eRhxRfB6W3iCfbdAqQWBvM0LqpIGB+YIo0CCGOAAQHkYizzrqVo6iZ7Yx0V74Kd6mgr0fsbU8fQyyvigKQXtFuuZXL6h3jX4vEG+nmR9nORfPMUmkXNnloKIVZ5Z79ospePKLHvkGDqw2Kk9zBBUdDcAk6gVY79kOKsDW6OLbp/zEtlys7ijFk5KnCiQMYXek3+25xoOyJFYo34dr/86oDFW5cM0mUAdc/SjoJmjj4KGhGLOJTWk342yCmn+JphQnRWrt9I= Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 On 26/11/26 5:50 pm, David Hildenbrand (Arm) wrote: > On 11/23/26 06:35, Dev Jain wrote: >> Enable batch clearing of PTEs and batch swap setting of PTEs for anon >> swap-backed folio unmapping. >> >> Processing all PTEs of a large folio in one go helps us batch across >> atomics (add_mm_counter() etc), barriers in __folio_try_share_anon_rmap(), >> and repeated calls to page_vma_mapped_walk(). In general, batching helps >> execute similar code together, making the path more memory and CPU >> friendly. >> >> On arm64-contpte, batching also helps avoid redundant ptep_get() calls >> and TLB flushes while breaking the contpte mapping. >> >> The handling of anon-exclusivity is very similar to commit cac1db8c3aad >> ("mm: optimize mprotect() by PTE batching"). Since >> folio_unmap_pte_batch() does not look at the bits of the underlying page, >> process sub-batches of PTEs pointing to pages with the same exclusivity >> state, and batch set only those PTEs to swap PTEs in one go. >> >> Disable batching for sparc (because of arch_unmap_one), we will batch this >> later. >> >> Rmap accounting and reference accounting must happen when anon folio unmap >> succeeds. If a large folio is only partially batched or a later sub-batch >> fails, account only the pages that were actually unmapped. >> >> If the batch length is less than the number of pages in the folio, skip >> over this batch. page_vma_mapped_walk() handles this: check_pte() returns >> true only if any of [pvmw->pfn, pvmw->pfn + nr_pages) is mapped by the >> PTE. Swap PTEs have no underlying PFN, so check_pte() returns false until >> the walk reaches the next present PTE to unmap. >> >> Remove the finish_unmap label since no goto callers are left now. >> >> Signed-off-by: Dev Jain >> --- >> mm/rmap.c | 118 +++++++++++++++++++++++++++++++++++++++--------------- >> 1 file changed, 86 insertions(+), 32 deletions(-) >> >> diff --git a/mm/rmap.c b/mm/rmap.c >> index 98690947ef19..818aecfa820b 100644 >> --- a/mm/rmap.c >> +++ b/mm/rmap.c >> @@ -1982,12 +1982,13 @@ static inline unsigned int folio_unmap_pte_batch(struct folio *folio, >> end_addr = pte_table_clamped_end(addr, vma->vm_end); >> max_nr = (end_addr - addr) >> PAGE_SHIFT; >> >> - /* We only support lazyfree or file folios batching for now ... */ >> - if (folio_test_anon(folio) && folio_test_swapbacked(folio)) >> + if (pte_unused(pte)) >> return 1; >> >> - if (pte_unused(pte)) >> +#ifdef __HAVE_ARCH_UNMAP_ONE >> + if (folio_test_anon(folio) && folio_test_swapbacked(folio)) >> return 1; >> +#endif > > Oh no, what about performance on sparc64 ;) > >> >> /* >> * If unmap fails, we need to restore the ptes. To avoid accidentally >> @@ -2158,16 +2159,25 @@ static pte_t swp_pte_prepare(swp_entry_t entry, pte_t old_pte, >> return swp_pte; >> } >> >> -static bool ttu_anon_swapbacked_folio(struct vm_area_struct *vma, >> +static void finish_folio_unmap_batch(struct vm_area_struct *vma, >> + struct folio *folio, struct page *page, unsigned long nr_pages) >> +{ >> + folio_remove_rmap_ptes(folio, page, nr_pages, vma); >> + if (vma_test_any_mask(vma, VMA_LOCKED_MASK)) >> + mlock_drain_local(); >> + folio_put_refs(folio, nr_pages); >> +} > > I think this cleanup can go into a separate cleanup patch to reduce the churn in > this patch. Okay. > >> + >> +static bool __ttu_anon_swapbacked_folio(struct vm_area_struct *vma, >> struct folio *folio, struct page *page, unsigned long address, >> - pte_t *ptep, pte_t pteval) >> + pte_t *ptep, pte_t pteval, unsigned long nr_pages, >> + bool anon_exclusive) >> { >> - const bool anon_exclusive = folio_test_anon(folio) && >> - PageAnonExclusive(page); >> swp_entry_t entry = folio_page_swap_entry(folio, page); >> struct mm_struct *mm = vma->vm_mm; >> + pte_t swp_pte; >> >> - if (folio_dup_swap_pages(folio, page, 1) < 0) >> + if (folio_dup_swap_pages(folio, page, nr_pages) < 0) >> return false; >> >> /* >> @@ -2176,25 +2186,61 @@ static bool ttu_anon_swapbacked_folio(struct vm_area_struct *vma, >> * so we'll not check/care. >> */ >> if (arch_unmap_one(mm, vma, address, pteval) < 0) { >> - folio_put_swap_pages(folio, page, 1); >> + VM_WARN_ON(nr_pages != 1); >> + folio_put_swap_pages(folio, page, nr_pages); >> return false; >> } >> >> /* See folio_try_share_anon_rmap(): clear PTE first. */ >> - if (anon_exclusive && folio_try_share_anon_rmap_pte(folio, page)) { >> - folio_put_swap_pages(folio, page, 1); >> + if (anon_exclusive && >> + folio_try_share_anon_rmap_ptes(folio, page, nr_pages)) { >> + folio_put_swap_pages(folio, page, nr_pages); >> return false; >> } >> >> mm_prepare_for_swap_entries(mm); >> - dec_mm_counter(mm, MM_ANONPAGES); >> - inc_mm_counter(mm, MM_SWAPENTS); >> - set_pte_at(mm, address, ptep, >> - swp_pte_prepare(entry, pteval, anon_exclusive)); >> + add_mm_counter(mm, MM_ANONPAGES, -nr_pages); >> + add_mm_counter(mm, MM_SWAPENTS, nr_pages); >> + swp_pte = swp_pte_prepare(entry, pteval, anon_exclusive); >> + set_softleaf_ptes(mm, address, ptep, swp_pte, nr_pages); >> return true; >> } > > I think this change (make it consume nr_pages and anon_exclusive) can also go > into an earlier cleanup patch to make this patch easier to consume. Okay > >> >> -static bool ttu_anon_folio(struct vm_area_struct *vma, struct folio *folio, >> +static unsigned long ttu_anon_swapbacked_folio(struct vm_area_struct *vma, >> + struct folio *folio, struct page *first_page, >> + unsigned long address, pte_t *ptep, pte_t pteval, >> + unsigned long nr_pages) >> +{ >> + unsigned long batch_idx = 0; >> + unsigned long nr_to_unmap = nr_pages; >> + >> + while (nr_pages) { >> + bool exclusive; >> + unsigned long len = page_anon_exclusive_batch(batch_idx, >> + nr_pages, first_page, &exclusive); > > See below. This style of indenting creates an unreadable blob. Struggling to understand how I can improve these. How about: unsigned long len; len = page_anon_exclusive_batch(batch_idx, nr_pages, first_page, &exclusive); > >> + >> + if (!__ttu_anon_swapbacked_folio(vma, folio, >> + first_page + batch_idx, address, ptep, pteval, >> + len, exclusive)) { > > Same here. Not pleasing to the eye. > struct page *cur_page; cur_page = first_page + batch_idx; if (!__ttu_anon_swapbacked_folio(vma, folio, cur_page, address, ptep, pteval, len, exclusive) ? >> + /* Restore the remaining PTEs that were cleared. */ >> + set_ptes(vma->vm_mm, address, ptep, pteval, nr_pages); >> + return nr_to_unmap - nr_pages; >> + } >> + >> + nr_pages -= len; >> + if (!nr_pages) >> + break; >> + >> + pteval = pte_advance_pfn(pteval, len); >> + address += len * PAGE_SIZE; >> + batch_idx += len; >> + ptep += len; >> + } >> + >> + return nr_to_unmap; >> +} >> + >> +static unsigned long ttu_anon_folio(struct vm_area_struct *vma, struct folio *folio, >> struct page *page, unsigned long address, pte_t *ptep, >> pte_t pteval, unsigned long nr_pages) >> { >> @@ -2203,15 +2249,21 @@ static bool ttu_anon_folio(struct vm_area_struct *vma, struct folio *folio, >> * See handle_pte_fault() ... >> */ >> if (WARN_ON_ONCE(folio_test_swapbacked(folio) != >> - folio_test_swapcache(folio))) >> - return false; >> + folio_test_swapcache(folio))) { >> + set_ptes(vma->vm_mm, address, ptep, pteval, nr_pages); >> + return 0; >> + } >> >> - if (!folio_test_swapbacked(folio)) >> - return ttu_anon_lazyfree_folio(vma, folio, nr_pages); >> + if (!folio_test_swapbacked(folio)) { >> + if (!ttu_anon_lazyfree_folio(vma, folio, nr_pages)) { >> + set_ptes(vma->vm_mm, address, ptep, pteval, nr_pages); >> + return 0; >> + } >> + return nr_pages; >> + } >> >> - /* nr_pages > 1 not supported yet */ >> return ttu_anon_swapbacked_folio(vma, folio, page, address, ptep, >> - pteval); >> + pteval, nr_pages); >> } >> >> /* >> @@ -2380,13 +2432,18 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, >> */ >> dec_mm_counter(mm, mm_counter(folio)); >> } else if (folio_test_anon(folio)) { >> - if (!ttu_anon_folio(vma, folio, page, address, >> - pvmw.pte, pteval, nr_pages)) { >> - set_ptes(mm, address, pvmw.pte, pteval, nr_pages); >> + unsigned long nr_unmapped = ttu_anon_folio(vma, folio, >> + page, address, pvmw.pte, >> + pteval, nr_pages); > > unreadable. Feel free to use > 80c and properly indent. unsigned long nr_unmapped; nr_unmapped = ttu_anon_folio(vma, folio, page, address, pvmw.pte, pteval, nr_pages); ? > > Maybe you can split this patch reasonable up further, there is jsut too much > happening at once. I think I can also make ttu_anon_folio() return unsigned long in the prep patch itself, I'll see. >