Skip to content

Commit 5dfab10

Browse files
yhuang-intelakpm00
authored andcommitted
migrate_pages: batch _unmap and _move
In this patch the _unmap and _move stage of the folio migration is batched. That for, previously, it is, for each folio _unmap() _move() Now, it is, for each folio _unmap() for each folio _move() Based on this, we can batch the TLB flushing and use some hardware accelerator to copy folios between batched _unmap and batched _move stages. Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: "Huang, Ying" <[email protected]> Tested-by: Hyeonggon Yoo <[email protected]> Cc: Zi Yan <[email protected]> Cc: Yang Shi <[email protected]> Cc: Baolin Wang <[email protected]> Cc: Oscar Salvador <[email protected]> Cc: Matthew Wilcox <[email protected]> Cc: Bharata B Rao <[email protected]> Cc: Alistair Popple <[email protected]> Cc: Xin Hao <[email protected]> Cc: Minchan Kim <[email protected]> Cc: Mike Kravetz <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 64c8902 commit 5dfab10

File tree

1 file changed

+189
-25
lines changed

1 file changed

+189
-25
lines changed

mm/migrate.c

Lines changed: 189 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,6 +1051,33 @@ static void __migrate_folio_extract(struct folio *dst,
10511051
dst->private = NULL;
10521052
}
10531053

1054+
/* Restore the source folio to the original state upon failure */
1055+
static void migrate_folio_undo_src(struct folio *src,
1056+
int page_was_mapped,
1057+
struct anon_vma *anon_vma,
1058+
struct list_head *ret)
1059+
{
1060+
if (page_was_mapped)
1061+
remove_migration_ptes(src, src, false);
1062+
/* Drop an anon_vma reference if we took one */
1063+
if (anon_vma)
1064+
put_anon_vma(anon_vma);
1065+
folio_unlock(src);
1066+
list_move_tail(&src->lru, ret);
1067+
}
1068+
1069+
/* Restore the destination folio to the original state upon failure */
1070+
static void migrate_folio_undo_dst(struct folio *dst,
1071+
free_page_t put_new_page,
1072+
unsigned long private)
1073+
{
1074+
folio_unlock(dst);
1075+
if (put_new_page)
1076+
put_new_page(&dst->page, private);
1077+
else
1078+
folio_put(dst);
1079+
}
1080+
10541081
/* Cleanup src folio upon migration success */
10551082
static void migrate_folio_done(struct folio *src,
10561083
enum migrate_reason reason)
@@ -1069,8 +1096,8 @@ static void migrate_folio_done(struct folio *src,
10691096
folio_put(src);
10701097
}
10711098

1072-
static int __migrate_folio_unmap(struct folio *src, struct folio *dst,
1073-
int force, enum migrate_mode mode)
1099+
static int __migrate_folio_unmap(struct folio *src, struct folio *dst, int force,
1100+
bool avoid_force_lock, enum migrate_mode mode)
10741101
{
10751102
int rc = -EAGAIN;
10761103
int page_was_mapped = 0;
@@ -1097,6 +1124,17 @@ static int __migrate_folio_unmap(struct folio *src, struct folio *dst,
10971124
if (current->flags & PF_MEMALLOC)
10981125
goto out;
10991126

1127+
/*
1128+
* We have locked some folios and are going to wait to lock
1129+
* this folio. To avoid a potential deadlock, let's bail
1130+
* out and not do that. The locked folios will be moved and
1131+
* unlocked, then we can wait to lock this folio.
1132+
*/
1133+
if (avoid_force_lock) {
1134+
rc = -EDEADLOCK;
1135+
goto out;
1136+
}
1137+
11001138
folio_lock(src);
11011139
}
11021140

@@ -1205,10 +1243,20 @@ static int __migrate_folio_move(struct folio *src, struct folio *dst,
12051243
int page_was_mapped = 0;
12061244
struct anon_vma *anon_vma = NULL;
12071245
bool is_lru = !__PageMovable(&src->page);
1246+
struct list_head *prev;
12081247

12091248
__migrate_folio_extract(dst, &page_was_mapped, &anon_vma);
1249+
prev = dst->lru.prev;
1250+
list_del(&dst->lru);
12101251

12111252
rc = move_to_new_folio(dst, src, mode);
1253+
1254+
if (rc == -EAGAIN) {
1255+
list_add(&dst->lru, prev);
1256+
__migrate_folio_record(dst, page_was_mapped, anon_vma);
1257+
return rc;
1258+
}
1259+
12121260
if (unlikely(!is_lru))
12131261
goto out_unlock_both;
12141262

@@ -1251,7 +1299,7 @@ static int __migrate_folio_move(struct folio *src, struct folio *dst,
12511299
/* Obtain the lock on page, remove all ptes. */
12521300
static int migrate_folio_unmap(new_page_t get_new_page, free_page_t put_new_page,
12531301
unsigned long private, struct folio *src,
1254-
struct folio **dstp, int force,
1302+
struct folio **dstp, int force, bool avoid_force_lock,
12551303
enum migrate_mode mode, enum migrate_reason reason,
12561304
struct list_head *ret)
12571305
{
@@ -1279,15 +1327,15 @@ static int migrate_folio_unmap(new_page_t get_new_page, free_page_t put_new_page
12791327
*dstp = dst;
12801328

12811329
dst->private = NULL;
1282-
rc = __migrate_folio_unmap(src, dst, force, mode);
1330+
rc = __migrate_folio_unmap(src, dst, force, avoid_force_lock, mode);
12831331
if (rc == MIGRATEPAGE_UNMAP)
12841332
return rc;
12851333

12861334
/*
12871335
* A folio that has not been unmapped will be restored to
12881336
* right list unless we want to retry.
12891337
*/
1290-
if (rc != -EAGAIN)
1338+
if (rc != -EAGAIN && rc != -EDEADLOCK)
12911339
list_move_tail(&src->lru, ret);
12921340

12931341
if (put_new_page)
@@ -1326,9 +1374,8 @@ static int migrate_folio_move(free_page_t put_new_page, unsigned long private,
13261374
*/
13271375
if (rc == MIGRATEPAGE_SUCCESS) {
13281376
migrate_folio_done(src, reason);
1329-
} else {
1330-
if (rc != -EAGAIN)
1331-
list_add_tail(&src->lru, ret);
1377+
} else if (rc != -EAGAIN) {
1378+
list_add_tail(&src->lru, ret);
13321379

13331380
if (put_new_page)
13341381
put_new_page(&dst->page, private);
@@ -1603,12 +1650,16 @@ static int migrate_hugetlbs(struct list_head *from, new_page_t get_new_page,
16031650
return nr_failed;
16041651
}
16051652

1653+
/*
1654+
* migrate_pages_batch() first unmaps folios in the from list as many as
1655+
* possible, then move the unmapped folios.
1656+
*/
16061657
static int migrate_pages_batch(struct list_head *from, new_page_t get_new_page,
16071658
free_page_t put_new_page, unsigned long private,
16081659
enum migrate_mode mode, int reason, struct list_head *ret_folios,
16091660
struct migrate_pages_stats *stats)
16101661
{
1611-
int retry = 1;
1662+
int retry;
16121663
int large_retry = 1;
16131664
int thp_retry = 1;
16141665
int nr_failed = 0;
@@ -1617,13 +1668,19 @@ static int migrate_pages_batch(struct list_head *from, new_page_t get_new_page,
16171668
int pass = 0;
16181669
bool is_large = false;
16191670
bool is_thp = false;
1620-
struct folio *folio, *folio2, *dst = NULL;
1621-
int rc, nr_pages;
1671+
struct folio *folio, *folio2, *dst = NULL, *dst2;
1672+
int rc, rc_saved, nr_pages;
16221673
LIST_HEAD(split_folios);
1674+
LIST_HEAD(unmap_folios);
1675+
LIST_HEAD(dst_folios);
16231676
bool nosplit = (reason == MR_NUMA_MISPLACED);
16241677
bool no_split_folio_counting = false;
1678+
bool avoid_force_lock;
16251679

1626-
split_folio_migration:
1680+
retry:
1681+
rc_saved = 0;
1682+
avoid_force_lock = false;
1683+
retry = 1;
16271684
for (pass = 0;
16281685
pass < NR_MAX_MIGRATE_PAGES_RETRY && (retry || large_retry);
16291686
pass++) {
@@ -1645,16 +1702,15 @@ static int migrate_pages_batch(struct list_head *from, new_page_t get_new_page,
16451702
cond_resched();
16461703

16471704
rc = migrate_folio_unmap(get_new_page, put_new_page, private,
1648-
folio, &dst, pass > 2, mode,
1649-
reason, ret_folios);
1650-
if (rc == MIGRATEPAGE_UNMAP)
1651-
rc = migrate_folio_move(put_new_page, private,
1652-
folio, dst, mode,
1653-
reason, ret_folios);
1705+
folio, &dst, pass > 2, avoid_force_lock,
1706+
mode, reason, ret_folios);
16541707
/*
16551708
* The rules are:
16561709
* Success: folio will be freed
1710+
* Unmap: folio will be put on unmap_folios list,
1711+
* dst folio put on dst_folios list
16571712
* -EAGAIN: stay on the from list
1713+
* -EDEADLOCK: stay on the from list
16581714
* -ENOMEM: stay on the from list
16591715
* -ENOSYS: stay on the from list
16601716
* Other errno: put on ret_folios list
@@ -1689,7 +1745,7 @@ static int migrate_pages_batch(struct list_head *from, new_page_t get_new_page,
16891745
case -ENOMEM:
16901746
/*
16911747
* When memory is low, don't bother to try to migrate
1692-
* other folios, just exit.
1748+
* other folios, move unmapped folios, then exit.
16931749
*/
16941750
if (is_large) {
16951751
nr_large_failed++;
@@ -1728,7 +1784,19 @@ static int migrate_pages_batch(struct list_head *from, new_page_t get_new_page,
17281784
/* nr_failed isn't updated for not used */
17291785
nr_large_failed += large_retry;
17301786
stats->nr_thp_failed += thp_retry;
1731-
goto out;
1787+
rc_saved = rc;
1788+
if (list_empty(&unmap_folios))
1789+
goto out;
1790+
else
1791+
goto move;
1792+
case -EDEADLOCK:
1793+
/*
1794+
* The folio cannot be locked for potential deadlock.
1795+
* Go move (and unlock) all locked folios. Then we can
1796+
* try again.
1797+
*/
1798+
rc_saved = rc;
1799+
goto move;
17321800
case -EAGAIN:
17331801
if (is_large) {
17341802
large_retry++;
@@ -1742,6 +1810,15 @@ static int migrate_pages_batch(struct list_head *from, new_page_t get_new_page,
17421810
stats->nr_succeeded += nr_pages;
17431811
stats->nr_thp_succeeded += is_thp;
17441812
break;
1813+
case MIGRATEPAGE_UNMAP:
1814+
/*
1815+
* We have locked some folios, don't force lock
1816+
* to avoid deadlock.
1817+
*/
1818+
avoid_force_lock = true;
1819+
list_move_tail(&folio->lru, &unmap_folios);
1820+
list_add_tail(&dst->lru, &dst_folios);
1821+
break;
17451822
default:
17461823
/*
17471824
* Permanent failure (-EBUSY, etc.):
@@ -1765,25 +1842,112 @@ static int migrate_pages_batch(struct list_head *from, new_page_t get_new_page,
17651842
nr_large_failed += large_retry;
17661843
stats->nr_thp_failed += thp_retry;
17671844
stats->nr_failed_pages += nr_retry_pages;
1845+
move:
1846+
retry = 1;
1847+
for (pass = 0;
1848+
pass < NR_MAX_MIGRATE_PAGES_RETRY && (retry || large_retry);
1849+
pass++) {
1850+
retry = 0;
1851+
large_retry = 0;
1852+
thp_retry = 0;
1853+
nr_retry_pages = 0;
1854+
1855+
dst = list_first_entry(&dst_folios, struct folio, lru);
1856+
dst2 = list_next_entry(dst, lru);
1857+
list_for_each_entry_safe(folio, folio2, &unmap_folios, lru) {
1858+
is_large = folio_test_large(folio);
1859+
is_thp = is_large && folio_test_pmd_mappable(folio);
1860+
nr_pages = folio_nr_pages(folio);
1861+
1862+
cond_resched();
1863+
1864+
rc = migrate_folio_move(put_new_page, private,
1865+
folio, dst, mode,
1866+
reason, ret_folios);
1867+
/*
1868+
* The rules are:
1869+
* Success: folio will be freed
1870+
* -EAGAIN: stay on the unmap_folios list
1871+
* Other errno: put on ret_folios list
1872+
*/
1873+
switch(rc) {
1874+
case -EAGAIN:
1875+
if (is_large) {
1876+
large_retry++;
1877+
thp_retry += is_thp;
1878+
} else if (!no_split_folio_counting) {
1879+
retry++;
1880+
}
1881+
nr_retry_pages += nr_pages;
1882+
break;
1883+
case MIGRATEPAGE_SUCCESS:
1884+
stats->nr_succeeded += nr_pages;
1885+
stats->nr_thp_succeeded += is_thp;
1886+
break;
1887+
default:
1888+
if (is_large) {
1889+
nr_large_failed++;
1890+
stats->nr_thp_failed += is_thp;
1891+
} else if (!no_split_folio_counting) {
1892+
nr_failed++;
1893+
}
1894+
1895+
stats->nr_failed_pages += nr_pages;
1896+
break;
1897+
}
1898+
dst = dst2;
1899+
dst2 = list_next_entry(dst, lru);
1900+
}
1901+
}
1902+
nr_failed += retry;
1903+
nr_large_failed += large_retry;
1904+
stats->nr_thp_failed += thp_retry;
1905+
stats->nr_failed_pages += nr_retry_pages;
1906+
1907+
if (rc_saved)
1908+
rc = rc_saved;
1909+
else
1910+
rc = nr_failed + nr_large_failed;
1911+
out:
1912+
/* Cleanup remaining folios */
1913+
dst = list_first_entry(&dst_folios, struct folio, lru);
1914+
dst2 = list_next_entry(dst, lru);
1915+
list_for_each_entry_safe(folio, folio2, &unmap_folios, lru) {
1916+
int page_was_mapped = 0;
1917+
struct anon_vma *anon_vma = NULL;
1918+
1919+
__migrate_folio_extract(dst, &page_was_mapped, &anon_vma);
1920+
migrate_folio_undo_src(folio, page_was_mapped, anon_vma,
1921+
ret_folios);
1922+
list_del(&dst->lru);
1923+
migrate_folio_undo_dst(dst, put_new_page, private);
1924+
dst = dst2;
1925+
dst2 = list_next_entry(dst, lru);
1926+
}
1927+
17681928
/*
17691929
* Try to migrate split folios of fail-to-migrate large folios, no
17701930
* nr_failed counting in this round, since all split folios of a
17711931
* large folio is counted as 1 failure in the first round.
17721932
*/
1773-
if (!list_empty(&split_folios)) {
1933+
if (rc >= 0 && !list_empty(&split_folios)) {
17741934
/*
17751935
* Move non-migrated folios (after NR_MAX_MIGRATE_PAGES_RETRY
17761936
* retries) to ret_folios to avoid migrating them again.
17771937
*/
17781938
list_splice_init(from, ret_folios);
17791939
list_splice_init(&split_folios, from);
17801940
no_split_folio_counting = true;
1781-
retry = 1;
1782-
goto split_folio_migration;
1941+
goto retry;
17831942
}
17841943

1785-
rc = nr_failed + nr_large_failed;
1786-
out:
1944+
/*
1945+
* We have unlocked all locked folios, so we can force lock now, let's
1946+
* try again.
1947+
*/
1948+
if (rc == -EDEADLOCK)
1949+
goto retry;
1950+
17871951
return rc;
17881952
}
17891953

0 commit comments

Comments
 (0)