From patchwork Tue Jan 21 02:01:59 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "zhaoyang.huang" X-Patchwork-Id: 13945586 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 33F21C02181 for ; Tue, 21 Jan 2025 02:02:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A81D66B0083; Mon, 20 Jan 2025 21:02:42 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A32536B0088; Mon, 20 Jan 2025 21:02:42 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8F9E96B0089; Mon, 20 Jan 2025 21:02:42 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 72E416B0083 for ; Mon, 20 Jan 2025 21:02:42 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 259551405C8 for ; Tue, 21 Jan 2025 02:02:42 +0000 (UTC) X-FDA: 83029810164.07.84CCA4E Received: from SHSQR01.spreadtrum.com (mx1.unisoc.com [222.66.158.135]) by imf07.hostedemail.com (Postfix) with ESMTP id 54F7140006 for ; Tue, 21 Jan 2025 02:02:38 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf07.hostedemail.com: domain of zhaoyang.huang@unisoc.com designates 222.66.158.135 as permitted sender) smtp.mailfrom=zhaoyang.huang@unisoc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737424960; a=rsa-sha256; cv=none; b=FBVF2o/KLSTzBWQgnVsvU4SwLnsZfQDTvfpl0vDexFmm2kkbB6DWTZDqr+ciyR1Iyodrw5 /nn00aohqsCpuOE6rpRxU3IRhiZc1A3ewLGriJhTQeindWgAsflXbhUMU7GQPovF5DLFZo Opuz+F1aaXTBed4XV2wEUWIWtR1nTcI= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf07.hostedemail.com: domain of zhaoyang.huang@unisoc.com designates 222.66.158.135 as permitted sender) smtp.mailfrom=zhaoyang.huang@unisoc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1737424960; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding:in-reply-to: references; bh=/0SC4EfxlrBFoCBjAXKRGnN110ykmFugr7k17vWvmuk=; b=qjn9SK82BK7Tex43eIlkhnta7M1YobB9gE8AAAtcL4olUlYqNa5ekocq4u/J9rRyosxA5B gU5TF52JFHR/JB/k0oULHX0Uyut74iHAyDN0wur7QcgkaG147E7uUu8N0B+jR/45dnqbwH 6KA43i3jgy807SEDcSOm6bpXh5T5aG8= 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 MIME-Version: 1.0 X-Originating-IP: [10.0.73.40] X-ClientProxiedBy: SHCAS03.spreadtrum.com (10.0.1.207) To BJMBX01.spreadtrum.com (10.0.64.7) X-MAIL: SHSQR01.spreadtrum.com 50L22Aue013285 X-Rspam-User: X-Rspamd-Queue-Id: 54F7140006 X-Rspamd-Server: rspam10 X-Stat-Signature: 5k4u4usb3d9i1bsscn1xbxkhynaoq5bb X-HE-Tag: 1737424958-416914 X-HE-Meta: U2FsdGVkX191Yv5ZREc9dua4A73Ctdf4BvEgsGYzbe6jnoT4Qxls6zBOSR3MJzIdXV0FrIskMKAUlhXW/t01PF/VBkAagD4lCypm7TIpfil0ku5GmRn8K6S3+CY5rAUWZw/kl0KL1afNl6qz6DXFy4CDHdCb9IeMc5YNygAyfa68OajMvABPdQ1uJFlbhQQaFQWgvSNSwfhwPKOG/Ex5pLRZfg5mDf6ZzBCCpgv3wSWPSUJqqFxsTidwYQ4rR0WzgoPH/jc7iS5ufPIg/vDTyC2SzILHRkEwLjDnzPRXmxQMzRzDMHwd97QXDD5Xd/ZwvrVb3CwzvP/V/5/s8nJUdsIJL5wW7r7MPAJTJlGkGm2wGNqbeooJI1S55LlmJJKYQwpJ4X2g2dxuW9v44V9IzV+ogKU1u9VP7Be5x1f9P3jfq2XohQjn0mpy1RJ1wpjo/yMrUzCmQJA3hb0CsxixbhCQtCNZe94pH3oRHbwXaDjgM/aJt6MLgwS8h1Lv1p2Y2TtTalqpz4PpbkneaS8m2t1HcRrWRxVgcN5aVnOjrIdAyh/i+GQfCrGCHNdW9aIHHa93/t7gzC3YQEL+KpkdswozjYkXlTYtJlz0iGEg3V7cKwzD0lWK1fO0lgt/LNmC6zFb9wS7YeftykjItMoFQWsh53NKXxDzAiNRLjP+QUfTiBAmQebb7yYx8XA7G6snSgkjycASgpX/I4utVwMTsoeR0LVScpbCKgJWc4bhED1j9jyedWShNBH8aQGDZ1FHfYW+G9Hw43rb3m/pi5KLh7AKRLECzUNKEZc5IJ6WcRI9qoCmj+8MW2DrjmLgxLGoTA3P3hDk0c62DjpOuQbyWbkgoahLOVCiLMyQdf7SMsFBpYHHMuAyPtoZbre7X9qMrsL+mRYHD+8xIhPzI5eWgXE8UcHU9bmQp768kqTd0W8xcAKWt6xUCiyMaReoYMa4/7mEZ76QcDGxK+giOr0 QgKBhYnd iM7m9aND2OXZCfn1iwmpdD0LHrsEXlBafBhEP+uhucmnoThTiTnMO49HrWJ9S4Y4rYetQDx1h2QoBXkb4/3Ts4LVRYS1/P8j0FgGPWTV8sedcqmhifSUBW/41nv3TU3yUMNgpct/LKxamiQ1bWOKYofR52tRS6fDQX+KqK90nT100k/80v1PaS+NScMlYvU7HELO/KZ6cfLMamG0Vg0+Ia2ipQDeXhqcnzEm0uPI3r7BV+0QXiXWIC4qXVWeJ1mmaLVC2GTN5O5I6OyooacwfTqhtB93T7k6JVMYC X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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 >= 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 = 0; + unsigned long i; struct folio *prev_folio = NULL; bool drain_allow = true; @@ -2341,8 +2341,6 @@ static unsigned long collect_longterm_unpinnable_folios( if (folio_is_longterm_pinnable(folio)) continue; - collected++; - if (folio_is_device_coherent(folio)) continue; @@ -2364,8 +2362,6 @@ static unsigned long collect_longterm_unpinnable_folios( NR_ISOLATED_ANON + folio_is_file_lru(folio), folio_nr_pages(folio)); } - - return collected; } /* @@ -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; - collected = 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; return migrate_longterm_unpinnable_folios(&movable_folio_list, pofs);