@@ -1927,19 +1927,16 @@ struct page *get_dump_page(unsigned long addr)
1927
1927
1928
1928
#ifdef CONFIG_MIGRATION
1929
1929
/*
1930
- * Check whether all pages are pinnable. If some pages are not pinnable migrate
1931
- * them and unpin all the pages. Returns -EAGAIN if pages were unpinned or zero
1932
- * if all pages are pinnable and in the right zone. Other errors indicate
1933
- * migration failure.
1930
+ * Returns the number of collected pages. Return value is always >= 0.
1934
1931
*/
1935
- static long check_and_migrate_movable_pages (unsigned long nr_pages ,
1936
- struct page * * pages )
1932
+ static unsigned long collect_longterm_unpinnable_pages (
1933
+ struct list_head * movable_page_list ,
1934
+ unsigned long nr_pages ,
1935
+ struct page * * pages )
1937
1936
{
1938
- unsigned long i ;
1937
+ unsigned long i , collected = 0 ;
1939
1938
struct folio * prev_folio = NULL ;
1940
- LIST_HEAD (movable_page_list );
1941
- bool drain_allow = true, coherent_pages = false;
1942
- int ret = 0 ;
1939
+ bool drain_allow = true;
1943
1940
1944
1941
for (i = 0 ; i < nr_pages ; i ++ ) {
1945
1942
struct folio * folio = page_folio (pages [i ]);
@@ -1948,43 +1945,16 @@ static long check_and_migrate_movable_pages(unsigned long nr_pages,
1948
1945
continue ;
1949
1946
prev_folio = folio ;
1950
1947
1951
- /*
1952
- * Device coherent pages are managed by a driver and should not
1953
- * be pinned indefinitely as it prevents the driver moving the
1954
- * page. So when trying to pin with FOLL_LONGTERM instead try
1955
- * to migrate the page out of device memory.
1956
- */
1957
- if (folio_is_device_coherent (folio )) {
1958
- /*
1959
- * We always want a new GUP lookup with device coherent
1960
- * pages.
1961
- */
1962
- pages [i ] = 0 ;
1963
- coherent_pages = true;
1948
+ if (folio_is_longterm_pinnable (folio ))
1949
+ continue ;
1964
1950
1965
- /*
1966
- * Migration will fail if the page is pinned, so convert
1967
- * the pin on the source page to a normal reference.
1968
- */
1969
- get_page (& folio -> page );
1970
- unpin_user_page (& folio -> page );
1951
+ collected ++ ;
1971
1952
1972
- if (migrate_device_coherent_page (& folio -> page )) {
1973
- ret = - EBUSY ;
1974
- break ;
1975
- }
1953
+ if (folio_is_device_coherent (folio ))
1976
1954
continue ;
1977
- }
1978
1955
1979
- if (folio_is_longterm_pinnable (folio ))
1980
- continue ;
1981
- /*
1982
- * Try to move out any movable page before pinning the range.
1983
- */
1984
1956
if (folio_test_hugetlb (folio )) {
1985
- if (isolate_hugetlb (& folio -> page ,
1986
- & movable_page_list ))
1987
- ret = - EBUSY ;
1957
+ isolate_hugetlb (& folio -> page , movable_page_list );
1988
1958
continue ;
1989
1959
}
1990
1960
@@ -1993,53 +1963,118 @@ static long check_and_migrate_movable_pages(unsigned long nr_pages,
1993
1963
drain_allow = false;
1994
1964
}
1995
1965
1996
- if (folio_isolate_lru (folio )) {
1997
- ret = - EBUSY ;
1966
+ if (!folio_isolate_lru (folio ))
1998
1967
continue ;
1999
- }
2000
- list_add_tail (& folio -> lru , & movable_page_list );
1968
+
1969
+ list_add_tail (& folio -> lru , movable_page_list );
2001
1970
node_stat_mod_folio (folio ,
2002
1971
NR_ISOLATED_ANON + folio_is_file_lru (folio ),
2003
1972
folio_nr_pages (folio ));
2004
1973
}
2005
1974
2006
- /*
2007
- * If list is empty, and no isolation errors, means that all pages are
2008
- * in the correct zone. If there were device coherent pages some pages
2009
- * have been unpinned.
2010
- */
2011
- if (list_empty (& movable_page_list ) && !ret && !coherent_pages )
2012
- return 0 ;
1975
+ return collected ;
1976
+ }
1977
+
1978
+ /*
1979
+ * Unpins all pages and migrates device coherent pages and movable_page_list.
1980
+ * Returns -EAGAIN if all pages were successfully migrated or -errno for failure
1981
+ * (or partial success).
1982
+ */
1983
+ static int migrate_longterm_unpinnable_pages (
1984
+ struct list_head * movable_page_list ,
1985
+ unsigned long nr_pages ,
1986
+ struct page * * pages )
1987
+ {
1988
+ int ret ;
1989
+ unsigned long i ;
2013
1990
2014
- /*
2015
- * Unpin all pages. If device coherent pages were found
2016
- * migrate_device_coherent_page() will have dropped the pin and set
2017
- * pages[i] == NULL.
2018
- */
2019
1991
for (i = 0 ; i < nr_pages ; i ++ ) {
2020
- if (!pages [i ])
1992
+ struct folio * folio = page_folio (pages [i ]);
1993
+
1994
+ if (folio_is_device_coherent (folio )) {
1995
+ /*
1996
+ * Migration will fail if the page is pinned, so convert
1997
+ * the pin on the source page to a normal reference.
1998
+ */
1999
+ pages [i ] = NULL ;
2000
+ folio_get (folio );
2001
+ gup_put_folio (folio , 1 , FOLL_PIN );
2002
+
2003
+ if (migrate_device_coherent_page (& folio -> page )) {
2004
+ ret = - EBUSY ;
2005
+ goto err ;
2006
+ }
2007
+
2021
2008
continue ;
2009
+ }
2022
2010
2011
+ /*
2012
+ * We can't migrate pages with unexpected references, so drop
2013
+ * the reference obtained by __get_user_pages_locked().
2014
+ * Migrating pages have been added to movable_page_list after
2015
+ * calling folio_isolate_lru() which takes a reference so the
2016
+ * page won't be freed if it's migrating.
2017
+ */
2023
2018
unpin_user_page (pages [i ]);
2019
+ pages [i ] = NULL ;
2024
2020
}
2025
2021
2026
- if (!list_empty (& movable_page_list )) {
2022
+ if (!list_empty (movable_page_list )) {
2027
2023
struct migration_target_control mtc = {
2028
2024
.nid = NUMA_NO_NODE ,
2029
2025
.gfp_mask = GFP_USER | __GFP_NOWARN ,
2030
2026
};
2031
2027
2032
- ret = migrate_pages (& movable_page_list , alloc_migration_target ,
2033
- NULL , (unsigned long )& mtc , MIGRATE_SYNC ,
2034
- MR_LONGTERM_PIN , NULL );
2035
- if (ret > 0 ) /* number of pages not migrated */
2028
+ if (migrate_pages (movable_page_list , alloc_migration_target ,
2029
+ NULL , (unsigned long )& mtc , MIGRATE_SYNC ,
2030
+ MR_LONGTERM_PIN , NULL )) {
2036
2031
ret = - ENOMEM ;
2032
+ goto err ;
2033
+ }
2037
2034
}
2038
2035
2039
- if (ret && !list_empty (& movable_page_list ))
2040
- putback_movable_pages (& movable_page_list );
2036
+ putback_movable_pages (movable_page_list );
2037
+
2038
+ return - EAGAIN ;
2041
2039
2042
- return ret ? ret : - EAGAIN ;
2040
+ err :
2041
+ for (i = 0 ; i < nr_pages ; i ++ )
2042
+ if (pages [i ])
2043
+ unpin_user_page (pages [i ]);
2044
+ putback_movable_pages (movable_page_list );
2045
+
2046
+ return ret ;
2047
+ }
2048
+
2049
+ /*
2050
+ * Check whether all pages are *allowed* to be pinned. Rather confusingly, all
2051
+ * pages in the range are required to be pinned via FOLL_PIN, before calling
2052
+ * this routine.
2053
+ *
2054
+ * If any pages in the range are not allowed to be pinned, then this routine
2055
+ * will migrate those pages away, unpin all the pages in the range and return
2056
+ * -EAGAIN. The caller should re-pin the entire range with FOLL_PIN and then
2057
+ * call this routine again.
2058
+ *
2059
+ * If an error other than -EAGAIN occurs, this indicates a migration failure.
2060
+ * The caller should give up, and propagate the error back up the call stack.
2061
+ *
2062
+ * If everything is OK and all pages in the range are allowed to be pinned, then
2063
+ * this routine leaves all pages pinned and returns zero for success.
2064
+ */
2065
+ static long check_and_migrate_movable_pages (unsigned long nr_pages ,
2066
+ struct page * * pages )
2067
+ {
2068
+ unsigned long collected ;
2069
+ LIST_HEAD (movable_page_list );
2070
+
2071
+ collected = collect_longterm_unpinnable_pages (& movable_page_list ,
2072
+ nr_pages , pages );
2073
+ if (!collected )
2074
+ return 0 ;
2075
+
2076
+ return migrate_longterm_unpinnable_pages (& movable_page_list , nr_pages ,
2077
+ pages );
2043
2078
}
2044
2079
#else
2045
2080
static long check_and_migrate_movable_pages (unsigned long nr_pages ,
@@ -2066,7 +2101,15 @@ static long __gup_longterm_locked(struct mm_struct *mm,
2066
2101
if (!(gup_flags & FOLL_LONGTERM ))
2067
2102
return __get_user_pages_locked (mm , start , nr_pages , pages , vmas ,
2068
2103
NULL , gup_flags );
2069
- /* check_and_migrate_movable_pages() assumes pages have been pinned. */
2104
+
2105
+ /*
2106
+ * If we get to this point then FOLL_LONGTERM is set, and FOLL_LONGTERM
2107
+ * implies FOLL_PIN (although the reverse is not true). Therefore it is
2108
+ * correct to unconditionally call check_and_migrate_movable_pages()
2109
+ * which assumes pages have been pinned via FOLL_PIN.
2110
+ *
2111
+ * Enforce the above reasoning by asserting that FOLL_PIN is set.
2112
+ */
2070
2113
if (WARN_ON (!(gup_flags & FOLL_PIN )))
2071
2114
return - EINVAL ;
2072
2115
flags = memalloc_pin_save ();
0 commit comments