Commit 2b9b256f authored by Zhaoyang Huang's avatar Zhaoyang Huang Committed by Wentao Guan
Browse files

mm: gup: fix infinite loop within __get_longterm_locked

stable inclusion
from stable-v6.6.79
commit 933b08c0edfa5c07408d3020cffb97115544e34a
category: bugfix
bugzilla: https://gitee.com/openeuler/kernel/issues/IBXANC

Reference: https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?id=933b08c0edfa5c07408d3020cffb97115544e34a

--------------------------------

commit 1aaf8c122918aa8897605a9aa1e8ed6600d6f930 upstream.

We can run into an infinite loop in __get_longterm_locked() when
collect_longterm_unpinnable_folios() finds only folios that are isolated
from the LRU or were never added to the LRU.  This can happen when all
folios to be pinned are never added to the LRU, for example when
vm_ops->fault allocated pages using cma_alloc() and never added them to
the LRU.

Fix it by simply taking a look at the list in the single caller, to see if
anything was added.

[zhaoyang.huang@unisoc.com: move definition of local]
  Link: https://lkml.kernel.org/r/20250122012604.3654667-1-zhaoyang.huang@unisoc.com
Link: https://lkml.kernel.org/r/20250121020159.3636477-1-zhaoyang.huang@unisoc.com


Fixes: 67e139b0 ("mm/gup.c: refactor check_and_migrate_movable_pages()")
Signed-off-by: default avatarZhaoyang Huang <zhaoyang.huang@unisoc.com>
Reviewed-by: default avatarJohn Hubbard <jhubbard@nvidia.com>
Reviewed-by: default avatarDavid Hildenbrand <david@redhat.com>
Suggested-by: default avatarDavid Hildenbrand <david@redhat.com>
Acked-by: default avatarDavid Hildenbrand <david@redhat.com>
Cc: Aijun Sun <aijun.sun@unisoc.com>
Cc: Alistair Popple <apopple@nvidia.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarWentao Guan <guanwentao@uniontech.com>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
(cherry picked from commit 933b08c0edfa5c07408d3020cffb97115544e34a)
Signed-off-by: default avatarWentao Guan <guanwentao@uniontech.com>
parent dbec0afc
Loading
Loading
Loading
Loading
+4 −10
Original line number Diff line number Diff line
@@ -1951,14 +1951,14 @@ struct page *get_dump_page(unsigned long addr)
/*
 * Returns the number of collected pages. Return value is always >= 0.
 */
static unsigned long collect_longterm_unpinnable_pages(
static void collect_longterm_unpinnable_pages(
					struct list_head *movable_page_list,
					unsigned long nr_pages,
					struct page **pages)
{
	unsigned long i, collected = 0;
	struct folio *prev_folio = NULL;
	bool drain_allow = true;
	unsigned long i;

	for (i = 0; i < nr_pages; i++) {
		struct folio *folio = page_folio(pages[i]);
@@ -1970,8 +1970,6 @@ static unsigned long collect_longterm_unpinnable_pages(
		if (folio_is_longterm_pinnable(folio))
			continue;

		collected++;

		if (folio_is_device_coherent(folio))
			continue;

@@ -1993,8 +1991,6 @@ static unsigned long collect_longterm_unpinnable_pages(
				    NR_ISOLATED_ANON + folio_is_file_lru(folio),
				    folio_nr_pages(folio));
	}

	return collected;
}

/*
@@ -2087,12 +2083,10 @@ static int migrate_longterm_unpinnable_pages(
static long check_and_migrate_movable_pages(unsigned long nr_pages,
					    struct page **pages)
{
	unsigned long collected;
	LIST_HEAD(movable_page_list);

	collected = collect_longterm_unpinnable_pages(&movable_page_list,
						nr_pages, pages);
	if (!collected)
	collect_longterm_unpinnable_pages(&movable_page_list, nr_pages, pages);
	if (list_empty(&movable_page_list))
		return 0;

	return migrate_longterm_unpinnable_pages(&movable_page_list, nr_pages,