Commit 682a71a1 authored by Matthew Wilcox (Oracle)'s avatar Matthew Wilcox (Oracle) Committed by Andrew Morton

migrate: convert __unmap_and_move() to use folios

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.

Link: https://lkml.kernel.org/r/20220902194653.1739778-51-willy@infradead.orgSigned-off-by: default avatarMatthew Wilcox (Oracle) <willy@infradead.org>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
parent 595af4c9
...@@ -993,17 +993,15 @@ static int move_to_new_folio(struct folio *dst, struct folio *src, ...@@ -993,17 +993,15 @@ static int move_to_new_folio(struct folio *dst, struct folio *src,
return rc; 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) int force, enum migrate_mode mode)
{ {
struct folio *folio = page_folio(page);
struct folio *dst = page_folio(newpage);
int rc = -EAGAIN; int rc = -EAGAIN;
bool page_was_mapped = false; bool page_was_mapped = false;
struct anon_vma *anon_vma = NULL; 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) if (!force || mode == MIGRATE_ASYNC)
goto out; goto out;
...@@ -1023,10 +1021,10 @@ static int __unmap_and_move(struct page *page, struct page *newpage, ...@@ -1023,10 +1021,10 @@ static int __unmap_and_move(struct page *page, struct page *newpage,
if (current->flags & PF_MEMALLOC) if (current->flags & PF_MEMALLOC)
goto out; 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 * Only in the case of a full synchronous migration is it
* necessary to wait for PageWriteback. In the async case, * necessary to wait for PageWriteback. In the async case,
...@@ -1043,12 +1041,12 @@ static int __unmap_and_move(struct page *page, struct page *newpage, ...@@ -1043,12 +1041,12 @@ static int __unmap_and_move(struct page *page, struct page *newpage,
} }
if (!force) if (!force)
goto out_unlock; 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, * By try_to_migrate(), src->mapcount goes down to 0 here. In this case,
* we cannot notice that anon_vma is freed while we migrates a page. * 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 * This get_anon_vma() delays freeing anon_vma pointer until the end
* of migration. File cache pages are no problem because of page_lock() * of migration. File cache pages are no problem because of page_lock()
* File Caches may use write_page() or lock_page() in migration, then, * File Caches may use write_page() or lock_page() in migration, then,
...@@ -1060,22 +1058,22 @@ static int __unmap_and_move(struct page *page, struct page *newpage, ...@@ -1060,22 +1058,22 @@ static int __unmap_and_move(struct page *page, struct page *newpage,
* because that implies that the anon page is no longer mapped * because that implies that the anon page is no longer mapped
* (and cannot be remapped so long as we hold the page lock). * (and cannot be remapped so long as we hold the page lock).
*/ */
if (PageAnon(page) && !PageKsm(page)) if (folio_test_anon(src) && !folio_test_ksm(src))
anon_vma = page_get_anon_vma(page); anon_vma = page_get_anon_vma(&src->page);
/* /*
* Block others from accessing the new page when we get around to * Block others from accessing the new page when we get around to
* establishing additional references. We are usually the only one * establishing additional references. We are usually the only one
* holding a reference to newpage at this point. We used to have a BUG * holding a reference to dst at this point. We used to have a BUG
* here if trylock_page(newpage) fails, but would like to allow for * here if folio_trylock(dst) fails, but would like to allow for
* cases where there might be a race with the previous use of newpage. * 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(). * 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; goto out_unlock;
if (unlikely(!is_lru)) { if (unlikely(!is_lru)) {
rc = move_to_new_folio(dst, folio, mode); rc = move_to_new_folio(dst, src, mode);
goto out_unlock_both; goto out_unlock_both;
} }
...@@ -1083,7 +1081,7 @@ static int __unmap_and_move(struct page *page, struct page *newpage, ...@@ -1083,7 +1081,7 @@ static int __unmap_and_move(struct page *page, struct page *newpage,
* Corner case handling: * Corner case handling:
* 1. When a new swap-cache page is read into, it is added to the LRU * 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. * 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. * trigger a BUG. So handle it here.
* 2. An orphaned page (see truncate_cleanup_page) might have * 2. An orphaned page (see truncate_cleanup_page) might have
* fs-private metadata. The page can be picked up due to memory * fs-private metadata. The page can be picked up due to memory
...@@ -1091,57 +1089,56 @@ static int __unmap_and_move(struct page *page, struct page *newpage, ...@@ -1091,57 +1089,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 * invisible to the vm, so the page can not be migrated. So try to
* free the metadata, so the page can be freed. * free the metadata, so the page can be freed.
*/ */
if (!page->mapping) { if (!src->mapping) {
VM_BUG_ON_PAGE(PageAnon(page), page); if (folio_test_private(src)) {
if (page_has_private(page)) { try_to_free_buffers(src);
try_to_free_buffers(folio);
goto out_unlock_both; goto out_unlock_both;
} }
} else if (page_mapped(page)) { } else if (folio_mapped(src)) {
/* Establish migration ptes */ /* Establish migration ptes */
VM_BUG_ON_PAGE(PageAnon(page) && !PageKsm(page) && !anon_vma, VM_BUG_ON_FOLIO(folio_test_anon(src) &&
page); !folio_test_ksm(src) && !anon_vma, src);
try_to_migrate(folio, 0); try_to_migrate(src, 0);
page_was_mapped = true; page_was_mapped = true;
} }
if (!page_mapped(page)) if (!folio_mapped(src))
rc = move_to_new_folio(dst, folio, mode); 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 * 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 * We would like to do something similar for the old page, when
* unsuccessful, and other cases when a page has been temporarily * unsuccessful, and other cases when a page has been temporarily
* isolated from the unevictable LRU: but this case is the easiest. * isolated from the unevictable LRU: but this case is the easiest.
*/ */
if (rc == MIGRATEPAGE_SUCCESS) { if (rc == MIGRATEPAGE_SUCCESS) {
lru_cache_add(newpage); folio_add_lru(dst);
if (page_was_mapped) if (page_was_mapped)
lru_add_drain(); lru_add_drain();
} }
if (page_was_mapped) if (page_was_mapped)
remove_migration_ptes(folio, remove_migration_ptes(src,
rc == MIGRATEPAGE_SUCCESS ? dst : folio, false); rc == MIGRATEPAGE_SUCCESS ? dst : src, false);
out_unlock_both: out_unlock_both:
unlock_page(newpage); folio_unlock(dst);
out_unlock: out_unlock:
/* Drop an anon_vma reference if we took one */ /* Drop an anon_vma reference if we took one */
if (anon_vma) if (anon_vma)
put_anon_vma(anon_vma); put_anon_vma(anon_vma);
unlock_page(page); folio_unlock(src);
out: 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 * which will not free the page because new page owner increased
* refcounter. * refcounter.
*/ */
if (rc == MIGRATEPAGE_SUCCESS) if (rc == MIGRATEPAGE_SUCCESS)
put_page(newpage); folio_put(dst);
return rc; return rc;
} }
...@@ -1157,6 +1154,7 @@ static int unmap_and_move(new_page_t get_new_page, ...@@ -1157,6 +1154,7 @@ static int unmap_and_move(new_page_t get_new_page,
enum migrate_reason reason, enum migrate_reason reason,
struct list_head *ret) struct list_head *ret)
{ {
struct folio *dst, *src = page_folio(page);
int rc = MIGRATEPAGE_SUCCESS; int rc = MIGRATEPAGE_SUCCESS;
struct page *newpage = NULL; struct page *newpage = NULL;
...@@ -1174,9 +1172,10 @@ static int unmap_and_move(new_page_t get_new_page, ...@@ -1174,9 +1172,10 @@ static int unmap_and_move(new_page_t get_new_page,
newpage = get_new_page(page, private); newpage = get_new_page(page, private);
if (!newpage) if (!newpage)
return -ENOMEM; return -ENOMEM;
dst = page_folio(newpage);
newpage->private = 0; newpage->private = 0;
rc = __unmap_and_move(page, newpage, force, mode); rc = __unmap_and_move(src, dst, force, mode);
if (rc == MIGRATEPAGE_SUCCESS) if (rc == MIGRATEPAGE_SUCCESS)
set_page_owner_migrate_reason(newpage, reason); set_page_owner_migrate_reason(newpage, reason);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment