From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 51BE63FE357 for ; Tue, 7 Jul 2026 12:12:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783426325; cv=none; b=F2BDiKWu+ImC3btcxOhzft+GQjh9R8FHC7IcF4FBxnT2c3KTdUwvPRfd3w3Chq3pWheCVlG/GEiNc8t58B8pzlxmtTXtnakc8oflSgtvkbcGFk2AO4B2aMyls6pFz1i3iFxYGNWpWVsnVEhFgNvUL5AD0bPU404PW5CUAFAaGuU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783426325; c=relaxed/simple; bh=2GK8o4qasnLQawbaz76qEMGQ3KvGxUxUcAMZU7uCfZ8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=J43+z3LNI3IkaIdk5H7948HvWW+55NWY6O4UicBw0F1/gnUUhgMaxcRf1P5+O1Zg7WgUPEQUXpn0Fzbq9Gu5oAwbIosAdpxBDW5YR1+JNqLeXzDxWzjf+ZWKuQA1FtA1mAGmXcxlzf4+wjH1A18esJqHXQJyNRTBIDy76XVPScM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b=WGCeZKWz; arc=none smtp.client-ip=217.140.110.172 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b="WGCeZKWz" 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 4A36C1E8D; Tue, 7 Jul 2026 05:11:59 -0700 (PDT) Received: from cesw-amp-gbt-1s-m12830-01.blr.arm.com (cesw-amp-gbt-1s-m12830-01.blr.arm.com [10.164.195.33]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 617EB3F7B4; Tue, 7 Jul 2026 05:11:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1783426323; bh=2GK8o4qasnLQawbaz76qEMGQ3KvGxUxUcAMZU7uCfZ8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WGCeZKWzreSAJh46bIERTCb4XKHYKAq3N4dMnlHNuAPxmjof1N3MMEBtlR7oNAa6I H/TRZQWfynEZ2/9Kv3qXxjzenKrBPrKCFDa280hBMnS5f2Jlqx60SZwSRH2FMhLk+s 3Yy4B5BHuLoRu/UeN+S798X2rrm92v0nSQtQm3ZY= From: Dev Jain To: akpm@linux-foundation.org, david@kernel.org, ljs@kernel.org, muchun.song@linux.dev, osalvador@suse.de Cc: Dev Jain , riel@surriel.com, liam@infradead.org, vbabka@kernel.org, harry@kernel.org, jannh@google.com, lance.yang@linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org, ryan.roberts@arm.com, anshuman.khandual@arm.com Subject: [PATCH 4/5] mm/rmap: refactor anon swapbacked folio unmap in try_to_unmap_one Date: Tue, 7 Jul 2026 12:11:22 +0000 Message-ID: <20260707121128.373192-5-dev.jain@arm.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260707121128.373192-1-dev.jain@arm.com> References: <20260707121128.373192-1-dev.jain@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: 8bit Refactor anonymous swapbacked folio unmap to ttu_anon_swapbacked_folio(). No functional change intended. Signed-off-by: Dev Jain --- mm/rmap.c | 105 ++++++++++++++++++++++++++++++++---------------------- 1 file changed, 62 insertions(+), 43 deletions(-) diff --git a/mm/rmap.c b/mm/rmap.c index 00b571c2a1bab..ade78df5be2bd 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -2113,6 +2113,64 @@ static inline bool ttu_anon_lazyfree_folio(struct vm_area_struct *vma, return true; } +static inline void set_swp_pte_at(struct mm_struct *mm, unsigned long address, + pte_t *ptep, swp_entry_t entry, pte_t pteval, bool anon_exclusive) +{ + pte_t swp_pte = swp_entry_to_pte(entry); + + if (anon_exclusive) + swp_pte = pte_swp_mkexclusive(swp_pte); + + if (likely(pte_present(pteval))) { + if (pte_soft_dirty(pteval)) + swp_pte = pte_swp_mksoft_dirty(swp_pte); + if (pte_uffd_wp(pteval)) + swp_pte = pte_swp_mkuffd_wp(swp_pte); + } else { + /* Device-exclusive entry */ + if (pte_swp_soft_dirty(pteval)) + swp_pte = pte_swp_mksoft_dirty(swp_pte); + if (pte_swp_uffd_wp(pteval)) + swp_pte = pte_swp_mkuffd_wp(swp_pte); + } + + set_pte_at(mm, address, ptep, swp_pte); +} + +static inline bool ttu_anon_swapbacked_folio(struct vm_area_struct *vma, struct folio *folio, + struct page *subpage, unsigned long address, pte_t *ptep, + pte_t pteval) +{ + bool anon_exclusive = folio_test_anon(folio) && PageAnonExclusive(subpage); + swp_entry_t entry = page_swap_entry(subpage); + struct mm_struct *mm = vma->vm_mm; + + if (folio_dup_swap(folio, subpage) < 0) + return false; + + /* + * arch_unmap_one() is expected to be a NOP on + * architectures where we could have PFN swap PTEs, + * so we'll not check/care. + */ + if (arch_unmap_one(mm, vma, address, pteval) < 0) { + folio_put_swap(folio, subpage); + return false; + } + + /* See folio_try_share_anon_rmap(): clear PTE first. */ + if (anon_exclusive && folio_try_share_anon_rmap_pte(folio, subpage)) { + folio_put_swap(folio, subpage); + return false; + } + + mm_prepare_for_swap_entries(mm); + dec_mm_counter(mm, MM_ANONPAGES); + inc_mm_counter(mm, MM_SWAPENTS); + set_swp_pte_at(mm, address, ptep, entry, pteval, anon_exclusive); + return true; +} + /* * @arg: enum ttu_flags will be passed to this argument */ @@ -2121,7 +2179,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, { struct mm_struct *mm = vma->vm_mm; DEFINE_FOLIO_VMA_WALK(pvmw, folio, vma, address, 0); - bool anon_exclusive, ret = true; + bool ret = true; pte_t pteval; struct page *subpage; struct mmu_notifier_range range; @@ -2235,8 +2293,6 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, } subpage = folio_page(folio, pfn - folio_pfn(folio)); - anon_exclusive = folio_test_anon(folio) && - PageAnonExclusive(subpage); if (likely(pte_present(pteval))) { nr_pages = folio_unmap_pte_batch(folio, &pvmw, flags, pteval); @@ -2295,8 +2351,6 @@ 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)) { - swp_entry_t entry = page_swap_entry(subpage); - pte_t swp_pte; /* * Store the swap location in the pte. * See handle_pte_fault() ... @@ -2317,47 +2371,12 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, goto finish_unmap; } - if (folio_dup_swap(folio, subpage) < 0) { - set_pte_at(mm, address, pvmw.pte, pteval); - goto walk_abort; - } - - /* - * arch_unmap_one() is expected to be a NOP on - * architectures where we could have PFN swap PTEs, - * so we'll not check/care. - */ - if (arch_unmap_one(mm, vma, address, pteval) < 0) { - folio_put_swap(folio, subpage); - set_pte_at(mm, address, pvmw.pte, pteval); - goto walk_abort; - } - - /* See folio_try_share_anon_rmap(): clear PTE first. */ - if (anon_exclusive && - folio_try_share_anon_rmap_pte(folio, subpage)) { - folio_put_swap(folio, subpage); + if (!ttu_anon_swapbacked_folio(vma, folio, subpage, address, + pvmw.pte, pteval)) { set_pte_at(mm, address, pvmw.pte, pteval); goto walk_abort; } - mm_prepare_for_swap_entries(mm); - dec_mm_counter(mm, MM_ANONPAGES); - inc_mm_counter(mm, MM_SWAPENTS); - swp_pte = swp_entry_to_pte(entry); - if (anon_exclusive) - swp_pte = pte_swp_mkexclusive(swp_pte); - if (likely(pte_present(pteval))) { - if (pte_soft_dirty(pteval)) - swp_pte = pte_swp_mksoft_dirty(swp_pte); - if (pte_uffd_wp(pteval)) - swp_pte = pte_swp_mkuffd_wp(swp_pte); - } else { - if (pte_swp_soft_dirty(pteval)) - swp_pte = pte_swp_mksoft_dirty(swp_pte); - if (pte_swp_uffd_wp(pteval)) - swp_pte = pte_swp_mkuffd_wp(swp_pte); - } - set_pte_at(mm, address, pvmw.pte, swp_pte); + goto finish_unmap; } else { /* * This is a locked file-backed folio, -- 2.43.0