Commit 24a95998 authored by Alistair Popple's avatar Alistair Popple Committed by Andrew Morton

mm/gup.c: simplify and fix check_and_migrate_movable_pages() return codes

When pinning pages with FOLL_LONGTERM check_and_migrate_movable_pages() is
called to migrate pages out of zones which should not contain any longterm
pinned pages.

When migration succeeds all pages will have been unpinned so pinning needs
to be retried.  This is indicated by returning zero.  When all pages are
in the correct zone the number of pinned pages is returned.

However migration can also fail, in which case pages are unpinned and
-ENOMEM is returned.  However if the failure was due to not being unable
to isolate a page zero is returned.  This leads to indefinite looping in
__gup_longterm_locked().

Fix this by simplifying the return codes such that zero indicates all
pages were successfully pinned in the correct zone while errors indicate
either pages were migrated and pinning should be retried or that migration
has failed and therefore the pinning operation should fail.

[syoshida@redhat.com: fix return value for __gup_longterm_locked()]
  Link: https://lkml.kernel.org/r/20220821183547.950370-1-syoshida@redhat.com
[akpm@linux-foundation.org: fix code layout, per John]
[yshigeru@gmail.com: fix uninitialized return value on __gup_longterm_locked()]
  Link: https://lkml.kernel.org/r/20220827230037.78876-1-syoshida@redhat.com
Link: https://lkml.kernel.org/r/20220729024645.764366-1-apopple@nvidia.comSigned-off-by: default avatarAlistair Popple <apopple@nvidia.com>
Signed-off-by: default avatarShigeru Yoshida <syoshida@redhat.com>
Cc: David Hildenbrand <david@redhat.com>
Cc: Jason Gunthorpe <jgg@nvidia.com>
Cc: Alistair Popple <apopple@nvidia.com>
Cc: John Hubbard <jhubbard@nvidia.com>
Cc: Minchan Kim <minchan@kernel.org>
Cc: Pasha Tatashin <pasha.tatashin@soleen.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
parent c37213c5
...@@ -1927,16 +1927,16 @@ struct page *get_dump_page(unsigned long addr) ...@@ -1927,16 +1927,16 @@ struct page *get_dump_page(unsigned long addr)
#ifdef CONFIG_MIGRATION #ifdef CONFIG_MIGRATION
/* /*
* Check whether all pages are pinnable, if so return number of pages. If some * Check whether all pages are pinnable. If some pages are not pinnable migrate
* pages are not pinnable, migrate them, and unpin all pages. Return zero if * them and unpin all the pages. Returns -EAGAIN if pages were unpinned or zero
* pages were migrated, or if some pages were not successfully isolated. * if all pages are pinnable and in the right zone. Other errors indicate
* Return negative error if migration fails. * migration failure.
*/ */
static long check_and_migrate_movable_pages(unsigned long nr_pages, static long check_and_migrate_movable_pages(unsigned long nr_pages,
struct page **pages, struct page **pages,
unsigned int gup_flags) unsigned int gup_flags)
{ {
unsigned long isolation_error_count = 0, i; unsigned long i;
struct folio *prev_folio = NULL; struct folio *prev_folio = NULL;
LIST_HEAD(movable_page_list); LIST_HEAD(movable_page_list);
bool drain_allow = true, coherent_pages = false; bool drain_allow = true, coherent_pages = false;
...@@ -1972,10 +1972,10 @@ static long check_and_migrate_movable_pages(unsigned long nr_pages, ...@@ -1972,10 +1972,10 @@ static long check_and_migrate_movable_pages(unsigned long nr_pages,
unpin_user_page(&folio->page); unpin_user_page(&folio->page);
} }
ret = migrate_device_coherent_page(&folio->page); if (migrate_device_coherent_page(&folio->page)) {
if (ret) ret = -EBUSY;
goto unpin_pages; break;
}
continue; continue;
} }
...@@ -1987,7 +1987,7 @@ static long check_and_migrate_movable_pages(unsigned long nr_pages, ...@@ -1987,7 +1987,7 @@ static long check_and_migrate_movable_pages(unsigned long nr_pages,
if (folio_test_hugetlb(folio)) { if (folio_test_hugetlb(folio)) {
if (isolate_hugetlb(&folio->page, if (isolate_hugetlb(&folio->page,
&movable_page_list)) &movable_page_list))
isolation_error_count++; ret = -EBUSY;
continue; continue;
} }
...@@ -1997,7 +1997,7 @@ static long check_and_migrate_movable_pages(unsigned long nr_pages, ...@@ -1997,7 +1997,7 @@ static long check_and_migrate_movable_pages(unsigned long nr_pages,
} }
if (folio_isolate_lru(folio)) { if (folio_isolate_lru(folio)) {
isolation_error_count++; ret = -EBUSY;
continue; continue;
} }
list_add_tail(&folio->lru, &movable_page_list); list_add_tail(&folio->lru, &movable_page_list);
...@@ -2006,19 +2006,18 @@ static long check_and_migrate_movable_pages(unsigned long nr_pages, ...@@ -2006,19 +2006,18 @@ static long check_and_migrate_movable_pages(unsigned long nr_pages,
folio_nr_pages(folio)); folio_nr_pages(folio));
} }
if (!list_empty(&movable_page_list) || isolation_error_count ||
coherent_pages)
goto unpin_pages;
/* /*
* If list is empty, and no isolation errors, means that all pages are * If list is empty, and no isolation errors, means that all pages are
* in the correct zone. * in the correct zone. If there were device coherent pages some pages
* have been unpinned.
*/ */
return nr_pages; if (list_empty(&movable_page_list) && !ret && !coherent_pages)
return 0;
unpin_pages:
/* /*
* pages[i] might be NULL if any device coherent pages were found. * Unpin all pages. If device coherent pages were found
* migrate_device_coherent_page() will have dropped the pin and set
* pages[i] == NULL.
*/ */
for (i = 0; i < nr_pages; i++) { for (i = 0; i < nr_pages; i++) {
if (!pages[i]) if (!pages[i])
...@@ -2045,14 +2044,15 @@ static long check_and_migrate_movable_pages(unsigned long nr_pages, ...@@ -2045,14 +2044,15 @@ static long check_and_migrate_movable_pages(unsigned long nr_pages,
if (ret && !list_empty(&movable_page_list)) if (ret && !list_empty(&movable_page_list))
putback_movable_pages(&movable_page_list); putback_movable_pages(&movable_page_list);
return ret;
return ret ? ret : -EAGAIN;
} }
#else #else
static long check_and_migrate_movable_pages(unsigned long nr_pages, static long check_and_migrate_movable_pages(unsigned long nr_pages,
struct page **pages, struct page **pages,
unsigned int gup_flags) unsigned int gup_flags)
{ {
return nr_pages; return 0;
} }
#endif /* CONFIG_MIGRATION */ #endif /* CONFIG_MIGRATION */
...@@ -2068,22 +2068,26 @@ static long __gup_longterm_locked(struct mm_struct *mm, ...@@ -2068,22 +2068,26 @@ static long __gup_longterm_locked(struct mm_struct *mm,
unsigned int gup_flags) unsigned int gup_flags)
{ {
unsigned int flags; unsigned int flags;
long rc; long rc, nr_pinned_pages;
if (!(gup_flags & FOLL_LONGTERM)) if (!(gup_flags & FOLL_LONGTERM))
return __get_user_pages_locked(mm, start, nr_pages, pages, vmas, return __get_user_pages_locked(mm, start, nr_pages, pages, vmas,
NULL, gup_flags); NULL, gup_flags);
flags = memalloc_pin_save(); flags = memalloc_pin_save();
do { do {
rc = __get_user_pages_locked(mm, start, nr_pages, pages, vmas, nr_pinned_pages = __get_user_pages_locked(mm, start, nr_pages,
NULL, gup_flags); pages, vmas, NULL,
if (rc <= 0) gup_flags);
if (nr_pinned_pages <= 0) {
rc = nr_pinned_pages;
break; break;
rc = check_and_migrate_movable_pages(rc, pages, gup_flags); }
} while (!rc); rc = check_and_migrate_movable_pages(nr_pinned_pages, pages,
gup_flags);
} while (rc == -EAGAIN);
memalloc_pin_restore(flags); memalloc_pin_restore(flags);
return rc; return rc ? rc : nr_pinned_pages;
} }
static bool is_valid_gup_flags(unsigned int gup_flags) static bool is_valid_gup_flags(unsigned int gup_flags)
......
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