linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: "Matthew Wilcox (Oracle)" <willy@infradead.org>
To: linux-mm@kvack.org
Cc: "Matthew Wilcox (Oracle)" <willy@infradead.org>, hughd@google.com
Subject: [PATCH 53/59] migrate: Convert __unmap_and_move() to use folios
Date: Mon,  8 Aug 2022 20:34:21 +0100	[thread overview]
Message-ID: <20220808193430.3378317-54-willy@infradead.org> (raw)
In-Reply-To: <20220808193430.3378317-1-willy@infradead.org>

Removes a lot of calls to compound_head().  Also remove a VM_BUG_ON that
can never trigger as the PageAnon bit is the bottom bit of page->mapping.

Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
---
 mm/migrate.c | 75 ++++++++++++++++++++++++++--------------------------
 1 file changed, 37 insertions(+), 38 deletions(-)

diff --git a/mm/migrate.c b/mm/migrate.c
index 6a1597c92261..3cd1fdb5f572 100644
--- a/mm/migrate.c
+++ b/mm/migrate.c
@@ -976,17 +976,15 @@ static int move_to_new_folio(struct folio *dst, struct folio *src,
 	return rc;
 }
 
-static int __unmap_and_move(struct page *page, struct page *newpage,
+static int __unmap_and_move(struct folio *src, struct folio *dst,
 				int force, enum migrate_mode mode)
 {
-	struct folio *folio = page_folio(page);
-	struct folio *dst = page_folio(newpage);
 	int rc = -EAGAIN;
 	bool page_was_mapped = false;
 	struct anon_vma *anon_vma = NULL;
-	bool is_lru = !__PageMovable(page);
+	bool is_lru = !__PageMovable(&src->page);
 
-	if (!trylock_page(page)) {
+	if (!folio_trylock(src)) {
 		if (!force || mode == MIGRATE_ASYNC)
 			goto out;
 
@@ -1006,10 +1004,10 @@ static int __unmap_and_move(struct page *page, struct page *newpage,
 		if (current->flags & PF_MEMALLOC)
 			goto out;
 
-		lock_page(page);
+		folio_lock(src);
 	}
 
-	if (PageWriteback(page)) {
+	if (folio_test_writeback(src)) {
 		/*
 		 * Only in the case of a full synchronous migration is it
 		 * necessary to wait for PageWriteback. In the async case,
@@ -1026,12 +1024,12 @@ static int __unmap_and_move(struct page *page, struct page *newpage,
 		}
 		if (!force)
 			goto out_unlock;
-		wait_on_page_writeback(page);
+		folio_wait_writeback(src);
 	}
 
 	/*
-	 * By try_to_migrate(), page->mapcount goes down to 0 here. In this case,
-	 * we cannot notice that anon_vma is freed while we migrates a page.
+	 * By try_to_migrate(), src->mapcount goes down to 0 here. In this case,
+	 * we cannot notice that anon_vma is freed while we migrate a page.
 	 * This get_anon_vma() delays freeing anon_vma pointer until the end
 	 * of migration. File cache pages are no problem because of page_lock()
 	 * File Caches may use write_page() or lock_page() in migration, then,
@@ -1043,22 +1041,22 @@ static int __unmap_and_move(struct page *page, struct page *newpage,
 	 * because that implies that the anon page is no longer mapped
 	 * (and cannot be remapped so long as we hold the page lock).
 	 */
-	if (PageAnon(page) && !PageKsm(page))
-		anon_vma = page_get_anon_vma(page);
+	if (folio_test_anon(src) && !folio_test_ksm(src))
+		anon_vma = page_get_anon_vma(&src->page);
 
 	/*
 	 * Block others from accessing the new page when we get around to
 	 * establishing additional references. We are usually the only one
-	 * holding a reference to newpage at this point. We used to have a BUG
-	 * here if trylock_page(newpage) fails, but would like to allow for
-	 * cases where there might be a race with the previous use of newpage.
+	 * holding a reference to dst at this point. We used to have a BUG
+	 * here if folio_trylock(dst) fails, but would like to allow for
+	 * cases where there might be a race with the previous use of dst.
 	 * This is much like races on refcount of oldpage: just don't BUG().
 	 */
-	if (unlikely(!trylock_page(newpage)))
+	if (unlikely(!folio_trylock(dst)))
 		goto out_unlock;
 
 	if (unlikely(!is_lru)) {
-		rc = move_to_new_folio(dst, folio, mode);
+		rc = move_to_new_folio(dst, src, mode);
 		goto out_unlock_both;
 	}
 
@@ -1066,7 +1064,7 @@ static int __unmap_and_move(struct page *page, struct page *newpage,
 	 * Corner case handling:
 	 * 1. When a new swap-cache page is read into, it is added to the LRU
 	 * and treated as swapcache but it has no rmap yet.
-	 * Calling try_to_unmap() against a page->mapping==NULL page will
+	 * Calling try_to_unmap() against a src->mapping==NULL page will
 	 * trigger a BUG.  So handle it here.
 	 * 2. An orphaned page (see truncate_cleanup_page) might have
 	 * fs-private metadata. The page can be picked up due to memory
@@ -1074,57 +1072,56 @@ static int __unmap_and_move(struct page *page, struct page *newpage,
 	 * invisible to the vm, so the page can not be migrated.  So try to
 	 * free the metadata, so the page can be freed.
 	 */
-	if (!page->mapping) {
-		VM_BUG_ON_PAGE(PageAnon(page), page);
-		if (page_has_private(page)) {
-			try_to_free_buffers(folio);
+	if (!src->mapping) {
+		if (folio_test_private(src)) {
+			try_to_free_buffers(src);
 			goto out_unlock_both;
 		}
-	} else if (page_mapped(page)) {
+	} else if (folio_mapped(src)) {
 		/* Establish migration ptes */
-		VM_BUG_ON_PAGE(PageAnon(page) && !PageKsm(page) && !anon_vma,
-				page);
-		try_to_migrate(folio, 0);
+		VM_BUG_ON_FOLIO(folio_test_anon(src) &&
+			       !folio_test_ksm(src) && !anon_vma, src);
+		try_to_migrate(src, 0);
 		page_was_mapped = true;
 	}
 
-	if (!page_mapped(page))
-		rc = move_to_new_folio(dst, folio, mode);
+	if (!folio_mapped(src))
+		rc = move_to_new_folio(dst, src, mode);
 
 	/*
-	 * When successful, push newpage to LRU immediately: so that if it
+	 * When successful, push dst to LRU immediately: so that if it
 	 * turns out to be an mlocked page, remove_migration_ptes() will
-	 * automatically build up the correct newpage->mlock_count for it.
+	 * automatically build up the correct dst->mlock_count for it.
 	 *
 	 * We would like to do something similar for the old page, when
 	 * unsuccessful, and other cases when a page has been temporarily
 	 * isolated from the unevictable LRU: but this case is the easiest.
 	 */
 	if (rc == MIGRATEPAGE_SUCCESS) {
-		lru_cache_add(newpage);
+		folio_add_lru(dst);
 		if (page_was_mapped)
 			lru_add_drain();
 	}
 
 	if (page_was_mapped)
-		remove_migration_ptes(folio,
-			rc == MIGRATEPAGE_SUCCESS ? dst : folio, false);
+		remove_migration_ptes(src,
+			rc == MIGRATEPAGE_SUCCESS ? dst : src, false);
 
 out_unlock_both:
-	unlock_page(newpage);
+	folio_unlock(dst);
 out_unlock:
 	/* Drop an anon_vma reference if we took one */
 	if (anon_vma)
 		put_anon_vma(anon_vma);
-	unlock_page(page);
+	folio_unlock(src);
 out:
 	/*
-	 * If migration is successful, decrease refcount of the newpage,
+	 * If migration is successful, decrease refcount of dst,
 	 * which will not free the page because new page owner increased
 	 * refcounter.
 	 */
 	if (rc == MIGRATEPAGE_SUCCESS)
-		put_page(newpage);
+		folio_put(dst);
 
 	return rc;
 }
@@ -1140,6 +1137,7 @@ static int unmap_and_move(new_page_t get_new_page,
 				   enum migrate_reason reason,
 				   struct list_head *ret)
 {
+	struct folio *dst, *src = page_folio(page);
 	int rc = MIGRATEPAGE_SUCCESS;
 	struct page *newpage = NULL;
 
@@ -1157,9 +1155,10 @@ static int unmap_and_move(new_page_t get_new_page,
 	newpage = get_new_page(page, private);
 	if (!newpage)
 		return -ENOMEM;
+	dst = page_folio(newpage);
 
 	newpage->private = 0;
-	rc = __unmap_and_move(page, newpage, force, mode);
+	rc = __unmap_and_move(src, dst, force, mode);
 	if (rc == MIGRATEPAGE_SUCCESS)
 		set_page_owner_migrate_reason(newpage, reason);
 
-- 
2.35.1



  parent reply	other threads:[~2022-08-08 19:35 UTC|newest]

Thread overview: 62+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-08 19:33 [PATCH 00/59] MM folio changes for 6.1 Matthew Wilcox (Oracle)
2022-08-08 19:33 ` [PATCH 01/59] mm: Fix VM_BUG_ON in __delete_from_swap_cache() Matthew Wilcox (Oracle)
2022-08-08 19:33 ` [PATCH 02/59] shmem: Update folio if shmem_replace_page() updates the page Matthew Wilcox (Oracle)
2022-08-08 19:33 ` [PATCH 03/59] vmscan: Check folio_test_private(), not folio_get_private() Matthew Wilcox (Oracle)
2022-08-08 19:33 ` [PATCH 04/59] mm/vmscan: Fix a lot of comments Matthew Wilcox (Oracle)
2022-08-08 19:33 ` [PATCH 05/59] mm: Add the first tail page to struct folio Matthew Wilcox (Oracle)
2022-08-08 19:33 ` [PATCH 06/59] mm: Reimplement folio_order() and folio_nr_pages() Matthew Wilcox (Oracle)
2022-08-08 19:33 ` [PATCH 07/59] mm: Add split_folio() Matthew Wilcox (Oracle)
2022-08-08 19:33 ` [PATCH 08/59] mm: Add folio_add_lru_vma() Matthew Wilcox (Oracle)
2022-08-08 19:33 ` [PATCH 09/59] shmem: Convert shmem_writepage() to use a folio throughout Matthew Wilcox (Oracle)
2022-08-08 19:33 ` [PATCH 10/59] shmem: Convert shmem_delete_from_page_cache() to take a folio Matthew Wilcox (Oracle)
2022-08-08 19:33 ` [PATCH 11/59] shmem: Convert shmem_replace_page() to use folios throughout Matthew Wilcox (Oracle)
2022-08-08 19:33 ` [PATCH 12/59] mm/swapfile: Remove page_swapcount() Matthew Wilcox (Oracle)
2022-08-08 19:33 ` [PATCH 13/59] mm/swapfile: Convert try_to_free_swap() to folio_free_swap() Matthew Wilcox (Oracle)
2022-08-08 19:33 ` [PATCH 14/59] mm/swap: Convert __read_swap_cache_async() to use a folio Matthew Wilcox (Oracle)
2022-08-08 19:33 ` [PATCH 15/59] mm/swap: Convert add_to_swap_cache() to take " Matthew Wilcox (Oracle)
2022-08-08 19:33 ` [PATCH 16/59] mm/swap: Convert put_swap_page() to put_swap_folio() Matthew Wilcox (Oracle)
2022-08-08 19:33 ` [PATCH 17/59] mm: Convert do_swap_page() to use a folio Matthew Wilcox (Oracle)
2022-08-08 19:33 ` [PATCH 18/59] mm: Convert do_swap_page()'s swapcache variable to " Matthew Wilcox (Oracle)
2022-08-11  2:28   ` Hugh Dickins
2022-08-08 19:33 ` [PATCH 19/59] memcg: Convert mem_cgroup_swapin_charge_page() to mem_cgroup_swapin_charge_folio() Matthew Wilcox (Oracle)
2022-08-08 19:33 ` [PATCH 20/59] shmem: Convert shmem_mfill_atomic_pte() to use a folio Matthew Wilcox (Oracle)
2022-08-08 19:33 ` [PATCH 21/59] shmem: Convert shmem_replace_page() to shmem_replace_folio() Matthew Wilcox (Oracle)
2022-08-08 19:33 ` [PATCH 22/59] swap: Add swap_cache_get_folio() Matthew Wilcox (Oracle)
2022-08-08 19:33 ` [PATCH 23/59] shmem: Eliminate struct page from shmem_swapin_folio() Matthew Wilcox (Oracle)
2022-08-08 19:33 ` [PATCH 24/59] shmem: Convert shmem_getpage_gfp() to shmem_get_folio_gfp() Matthew Wilcox (Oracle)
2022-08-08 19:33 ` [PATCH 25/59] shmem: Convert shmem_fault() to use shmem_get_folio_gfp() Matthew Wilcox (Oracle)
2022-08-08 19:33 ` [PATCH 26/59] shmem: Convert shmem_read_mapping_page_gfp() " Matthew Wilcox (Oracle)
2022-08-08 19:33 ` [PATCH 27/59] shmem: Add shmem_get_folio() Matthew Wilcox (Oracle)
2022-08-08 19:33 ` [PATCH 28/59] shmem: Convert shmem_get_partial_folio() to use shmem_get_folio() Matthew Wilcox (Oracle)
2022-08-08 19:33 ` [PATCH 29/59] shmem: Convert shmem_write_begin() " Matthew Wilcox (Oracle)
2022-08-08 19:33 ` [PATCH 30/59] shmem: Convert shmem_file_read_iter() " Matthew Wilcox (Oracle)
2022-08-08 19:33 ` [PATCH 31/59] shmem: Convert shmem_fallocate() to use a folio Matthew Wilcox (Oracle)
2022-08-08 19:34 ` [PATCH 32/59] shmem: Convert shmem_symlink() " Matthew Wilcox (Oracle)
2022-08-08 19:34 ` [PATCH 33/59] shmem: Convert shmem_get_link() " Matthew Wilcox (Oracle)
2022-08-08 19:34 ` [PATCH 34/59] khugepaged: Call shmem_get_folio() Matthew Wilcox (Oracle)
2022-08-08 19:34 ` [PATCH 35/59] userfaultfd: Convert mcontinue_atomic_pte() to use a folio Matthew Wilcox (Oracle)
2022-08-08 19:34 ` [PATCH 36/59] shmem: Remove shmem_getpage() Matthew Wilcox (Oracle)
2022-08-08 19:34 ` [PATCH 37/59] swapfile: Convert try_to_unuse() to use a folio Matthew Wilcox (Oracle)
2022-08-08 19:34 ` [PATCH 38/59] swapfile: Convert __try_to_reclaim_swap() " Matthew Wilcox (Oracle)
2022-08-08 19:34 ` [PATCH 39/59] swapfile: Convert unuse_pte_range() " Matthew Wilcox (Oracle)
2022-08-08 19:34 ` [PATCH 40/59] mm: Convert do_swap_page() to use swap_cache_get_folio() Matthew Wilcox (Oracle)
2022-08-08 19:34 ` [PATCH 41/59] mm: Remove lookup_swap_cache() Matthew Wilcox (Oracle)
2022-08-08 19:34 ` [PATCH 42/59] swap_state: Convert free_swap_cache() to use a folio Matthew Wilcox (Oracle)
2022-08-08 19:34 ` [PATCH 43/59] swap: Convert swap_writepage() " Matthew Wilcox (Oracle)
2022-08-08 19:34 ` [PATCH 44/59] mm: Convert do_wp_page() " Matthew Wilcox (Oracle)
2022-08-08 19:34 ` [PATCH 45/59] huge_memory: Convert do_huge_pmd_wp_page() " Matthew Wilcox (Oracle)
2022-08-08 19:34 ` [PATCH 46/59] madvise: Convert madvise_free_pte_range() " Matthew Wilcox (Oracle)
2022-08-08 19:34 ` [PATCH 47/59] uprobes: Use folios more widely in __replace_page() Matthew Wilcox (Oracle)
2022-08-08 19:34 ` [PATCH 48/59] ksm: Use a folio in replace_page() Matthew Wilcox (Oracle)
2022-08-08 19:34 ` [PATCH 49/59] mm: Convert do_swap_page() to use folio_free_swap() Matthew Wilcox (Oracle)
2022-08-08 19:34 ` [PATCH 50/59] memcg: Convert mem_cgroup_swap_full() to take a folio Matthew Wilcox (Oracle)
2022-08-08 19:34 ` [PATCH 51/59] mm: Remove try_to_free_swap() Matthew Wilcox (Oracle)
2022-08-08 19:34 ` [PATCH 52/59] rmap: Convert page_move_anon_rmap() to use a folio Matthew Wilcox (Oracle)
2022-08-08 19:34 ` Matthew Wilcox (Oracle) [this message]
2022-08-08 19:34 ` [PATCH 54/59] migrate: Convert unmap_and_move_huge_page() to use folios Matthew Wilcox (Oracle)
2022-08-08 19:34 ` [PATCH 55/59] huge_memory: Convert split_huge_page_to_list() to use a folio Matthew Wilcox (Oracle)
2022-08-08 19:34 ` [PATCH 56/59] huge_memory: Convert unmap_page() to unmap_folio() Matthew Wilcox (Oracle)
2022-08-08 19:34 ` [PATCH 57/59] mm: Convert page_get_anon_vma() to folio_get_anon_vma() Matthew Wilcox (Oracle)
2022-08-08 19:34 ` [PATCH 58/59] rmap: Remove page_unlock_anon_vma_read() Matthew Wilcox (Oracle)
2022-08-08 19:34 ` [PATCH 59/59] uprobes: Use new_folio in __replace_page() Matthew Wilcox (Oracle)
2022-08-11  2:17 ` [PATCH 00/59] MM folio changes for 6.1 Hugh Dickins

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220808193430.3378317-54-willy@infradead.org \
    --to=willy@infradead.org \
    --cc=hughd@google.com \
    --cc=linux-mm@kvack.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).