From nobody Mon Feb 9 02:33:56 2026 Received: from SHSQR01.spreadtrum.com (mx1.unisoc.com [222.66.158.135]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CEB2B1D554 for ; Tue, 21 Jan 2025 02:02:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=222.66.158.135 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737424975; cv=none; b=dS9bwzccnTAOjt+NF0PMUTU9+/Q4YOy3XpZ3wvf2bMNpFW5fO/YCCrgN1pXYJn3KzuPcOIKaUdPNy6+ITPArD2xfw+9tAVWY8lddo5U0SSG8V5bQZigNMRsDEW1zPBwAHeJP5jKUnhpNrnkz49OsjblUPKrYGJcc18Cd/gOZkKY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737424975; c=relaxed/simple; bh=nks6mNtMyB8a+sd5BUCuTT9LPyJjUq1sR0ZcP233eUI=; h=From:To:Subject:Date:Message-ID:MIME-Version:Content-Type; b=RDt3XSDKT1yEFXw4L6dTJRQzxvV9Vq4O456zXJFpFtIE56KfMvINx/hsEtN5Xu+wwqA5S8e407mm1rLy4nHkfpkJbVYi6wXmAyrlWDlZQtRKN5nPZaOdTPbyHNDH3S5UfJAH4YoBZmgwA6u6ZSUvmtHdvkoHcM8mpeWjg9inZnI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=unisoc.com; spf=pass smtp.mailfrom=unisoc.com; arc=none smtp.client-ip=222.66.158.135 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=unisoc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=unisoc.com Received: from dlp.unisoc.com ([10.29.3.86]) by SHSQR01.spreadtrum.com with ESMTP id 50L22Aue013285; Tue, 21 Jan 2025 10:02:10 +0800 (+08) (envelope-from zhaoyang.huang@unisoc.com) Received: from SHDLP.spreadtrum.com (bjmbx01.spreadtrum.com [10.0.64.7]) by dlp.unisoc.com (SkyGuard) with ESMTPS id 4YcVjz0XCxz2PGlPD; Tue, 21 Jan 2025 09:58:47 +0800 (CST) Received: from bj03382pcu01.spreadtrum.com (10.0.73.40) by BJMBX01.spreadtrum.com (10.0.64.7) with Microsoft SMTP Server (TLS) id 15.0.1497.23; Tue, 21 Jan 2025 10:02:07 +0800 From: "zhaoyang.huang" To: Andrew Morton , Alistair Popple , John Hubbard , , , Zhaoyang Huang , , Subject: [PATCH v2] mm: gup: fix infinite loop within __get_longterm_locked Date: Tue, 21 Jan 2025 10:01:59 +0800 Message-ID: <20250121020159.3636477-1-zhaoyang.huang@unisoc.com> X-Mailer: git-send-email 2.25.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: SHCAS03.spreadtrum.com (10.0.1.207) To BJMBX01.spreadtrum.com (10.0.64.7) X-MAIL: SHSQR01.spreadtrum.com 50L22Aue013285 Content-Type: text/plain; charset="utf-8" From: Zhaoyang Huang 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. We incorrectly update the "collected" variable even if nothing was collected. Fix it by incrementing "collected" only when we isolated a folio and added it to the list of folios to migrate. Fixes: 67e139b02d99 ("mm/gup.c: refactor check_and_migrate_movable_pages()") Reviewed-by: John Hubbard Suggested-by: David Hildenbrand Signed-off-by: Zhaoyang Huang Reviewed-by: David Hildenbrand --- v2: judge if there are unpinnable pages via list_empty and update commit message --- --- mm/gup.c | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/mm/gup.c b/mm/gup.c index 3b75e631f369..37951fba929a 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -2323,11 +2323,11 @@ static void pofs_unpin(struct pages_or_folios *pofs) /* * Returns the number of collected folios. Return value is always >=3D 0. */ -static unsigned long collect_longterm_unpinnable_folios( +static void collect_longterm_unpinnable_folios( struct list_head *movable_folio_list, struct pages_or_folios *pofs) { - unsigned long i, collected =3D 0; + unsigned long i; struct folio *prev_folio =3D NULL; bool drain_allow =3D true; =20 @@ -2341,8 +2341,6 @@ static unsigned long collect_longterm_unpinnable_foli= os( if (folio_is_longterm_pinnable(folio)) continue; =20 - collected++; - if (folio_is_device_coherent(folio)) continue; =20 @@ -2364,8 +2362,6 @@ static unsigned long collect_longterm_unpinnable_foli= os( NR_ISOLATED_ANON + folio_is_file_lru(folio), folio_nr_pages(folio)); } - - return collected; } =20 /* @@ -2442,11 +2438,9 @@ static long check_and_migrate_movable_pages_or_folios(struct pages_or_folios *pofs) { LIST_HEAD(movable_folio_list); - unsigned long collected; =20 - collected =3D collect_longterm_unpinnable_folios(&movable_folio_list, - pofs); - if (!collected) + collect_longterm_unpinnable_folios(&movable_folio_list, pofs); + if (list_empty(&movable_folio_list)) return 0; =20 return migrate_longterm_unpinnable_folios(&movable_folio_list, pofs); --=20 2.25.1