From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from outbound.st.icloud.com (st-2005j-snip4-11.eps.apple.com [57.103.79.123]) (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 4E9E6379C4F for ; Sun, 20 Sep 2026 14:25:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=57.103.79.123 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789914346; cv=none; b=VqaqYM5emWz5U/q4XPEjNMYI/wS+6fqOkS+eamK9/NOouuM9u2g9Y1zhFTEvu8LNfoRtVIi/etjkYfr+0ke5l9Zjp6RX3x04RmtIGQXbMK2ECa1AcuoAGl5o0FX65VHHZHDqjDrlwgwsanuVdL+rpzwiFK5KzAvn+Repb2D7Bvw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789914346; c=relaxed/simple; bh=f342N/c8+dEDJDWraNU2OVkQeSo31UDlhHfw7nORPhE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=HNYHaZAjh2mZtxfGuN5DLtT4MCsNWECFJaYxWYVtyY8hN+9kAHXGw/O/1eS4ZKzJUIttbs4PE5/AFNsj+B5gY7R7HJInLEdBjGXkfGYbHA35+y/wLGmDo1CJKENgnaXT2G3tOVKBmXQxXjQptpC7Z6Bqh9B2fym4F8NsGxqsL8I= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=icloud.com; spf=pass smtp.mailfrom=icloud.com; dkim=pass (2048-bit key) header.d=icloud.com header.i=@icloud.com header.b=WQgQc6Sf; arc=none smtp.client-ip=57.103.79.123 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=icloud.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=icloud.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=icloud.com header.i=@icloud.com header.b="WQgQc6Sf" Received: from outbound.st.icloud.com (unknown [127.0.0.2]) by p00-icloudmta-asmtp-us-east-1a-60-percent-7 (Postfix) with ESMTPS id 99F261800297; Sun, 20 Sep 2026 14:25:41 +0000 (UTC) X-ICL-RepId: 01a0bf35-60c5-75c9-8b47-8dffd66b0e93 X-ICL-Out-Info: HUtFAUMEWwJACUgATUQeDx5WFlZNRAJCTQhKB0MGWQReCEsEQwFbEhVdRUkERxtXAlQXXQZSEnIZWhRcGFNFUR9UWFUJCgJRHFYNV0NUBF9QSxsOXABLWhVVFw4CQh9QH0wWV0NEHxwZWhRcGFNFUR9UWEMZRVZpQQtPHV0ZWxxCZFhXCQoCURxWDVdDVARfUFQRV1ALWQJCD0gKXwdGRB1KG1IDGhlXFlgbRwJFRkRBFEseCFRbBhQOSVAPAF0DME0dXQ5SBUZeWhdeUxcfSwBcRVoOWwRHFA== Dkim-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=icloud.com; s=1a1hai; t=1789914344; x=1792506344; bh=5pw+tpdHx2j/84yt3i8Fgf/shxRk6ErVktUyAKNdLp4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:To:x-icloud-hme; b=WQgQc6SfBU1UTStyIAAHOgOXwoYXMfQzpHOvzXm9IHBmMIbz2YcQnssNwdomRlhZcSVIYKLlZxSnrXYTLFwwYetyubsGxM+RHTByhTnUhkwhvNMSc9dHGXPQnoxFzytkIhJCNTEHt6SRtzMrEJu+g9Z3+hIs/YbXtvLo4SnvtbTr4Du6Z1Qmhcs7KXoYPNRoHWg1imQrC2O8pIerTGswULO1lREstEw8yKLP+Bk+4OqKiuoVDp3AStlipTBS088F5LAvqiCV58ft1B4K1977IwJWs2e5nlNveYsmljdUpX/9vrSrVZHRgcI7htP3LMT3SByMwd/NkjNYTOdfGTQ92g== Received: from [127.0.0.1] (unknown [17.156.216.30]) by p00-icloudmta-asmtp-us-east-1a-60-percent-7 (Postfix) with ESMTPSA id C7131180028A; Sun, 20 Sep 2026 14:25:33 +0000 (UTC) From: Zhang Peng Date: Sun, 20 Sep 2026 22:24:16 +0800 Subject: [PATCH 3/4] mm/vmscan: extract folio pageout from shrink_folio_list() Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20260920-vmscan-refactor-v1-3-ec04d71cb761@tencent.com> References: <20260920-vmscan-refactor-v1-0-ec04d71cb761@tencent.com> In-Reply-To: <20260920-vmscan-refactor-v1-0-ec04d71cb761@tencent.com> To: Andrew Morton , Kairui Song , Qi Zheng , Shakeel Butt , Barry Song , Axel Rasmussen , Yuanchu Xie , Wei Xu , Baoquan He , Baolin Wang , Johannes Weiner , David Hildenbrand , Michal Hocko , Lorenzo Stoakes Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Zhang Peng X-Mailer: b4 0.16.0 X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwOTIwMDIxMCBTYWx0ZWRfXwXrX+1CQ9OqP qilit9DWHNxX4siHZVf6yDfELsJncHnl1Wz2qrUCrtD5BkDyowPT93npjQEtO59kiJTf456CAAr ZZIRJwAnM2UT0chAQ5i0V+F+hmeKfJl+3SHm7h86FYoZpU/mKff54IZZuFTGl/jHVHPPg8JULDO CZYJRkqZGE/TrJhrTaFgxKa8GiVzgt0SVbhzvYwB6bKOHELfdZAVC/86BfaOpZtHlHqGIGbWhnO hR9crLe0LUsT0rjYdR5TaXASz9rhqdM4lhbCTEHPUTPK3oy5E4+e4FFyIRw85fWHJ/sidiVtBMQ NgQVIsiSuVTbqIAQkIu9xLQxkcwBU3aE81FQQf7HdKKhxLILE2vENci9/Ifsbg= X-Proofpoint-GUID: XofwOTYfrX-FMP0KpQDBM3g2zGkwMuyn X-Authority-Info-Out: v=2.4 cv=R/8O2N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H0wSoAVNgGK5IVqMpHxUrBJl5VEU+D0anjidG6msxHyw+jPNB1/gNM1fprrUICmP1gSqoYmQV24ml04CWcvJR10pzEAGtBCqnNqXYQe/Pbfois3PPbCHzU6CQQ0No++pDsArC6qjMIoA7+3cjglmIBUi1kkZGFBynmgEQTsd6QcfPbdfXceSicU3zy94Zeb7cxZzzapYhMku+Q5/rOeVQMob3DmSsknmsrhGtJU/zGw== From: Zhang Peng shrink_folio_list() contains a self-contained pageout() dispatch state machine. Extract it into folio_try_pageout() so it can be reused by the batched pageout path. Return an explicit pageout result instead of a boolean. This keeps the important distinction between activating and merely keeping a folio visible to the caller, and also records whether a kept folio remains locked after pageout(). The clean and synchronous-write cases return FOLIO_PAGEOUT_FREE: the folio is still locked there, and shrink_folio_list() then runs its single folio_try_reclaim_free() call, so the reclaim-free step keeps exactly one caller. The PAGE_ACTIVATE arm no longer normalises nr_pages to 1 after a split: folio_activate_locked() derives the count from folio_nr_pages() itself, which is equal by construction once the folio is order-0. Note this makes the patch depend on patch 1; applying it alone would over-account nr_activate for a split shmem folio. No functional change. --- mm/vmscan.c | 100 ++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 61 insertions(+), 39 deletions(-) diff --git a/mm/vmscan.c b/mm/vmscan.c index 120085dfa2fe..3bce7ff89293 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -1266,8 +1266,60 @@ static enum folio_reclaim_result folio_try_reclaim_free(struct folio *folio, return FOLIO_RECLAIM_SUCCESS; } +enum folio_pageout_result { + FOLIO_PAGEOUT_KEEP_LOCKED, + FOLIO_PAGEOUT_KEEP_UNLOCKED, + FOLIO_PAGEOUT_ACTIVATE, + FOLIO_PAGEOUT_FREE, /* folio is locked, hand it to folio_try_reclaim_free() */ +}; + +static enum folio_pageout_result folio_try_pageout(struct folio *folio, + struct scan_control *sc, struct swap_io_ctx *ctx, + struct list_head *folio_list) +{ + struct address_space *mapping = folio_mapping(folio); + unsigned int nr_pages = folio_nr_pages(folio); + + switch (pageout(ctx, mapping, folio, folio_list)) { + case PAGE_ACTIVATE: + /* + * If shmem folio is split when writeback to swap, the + * tail pages will make their own pass through this + * function and be accounted then. There is no need to + * clamp nr_pages here: folio_activate_locked() derives + * the count from folio_nr_pages() itself. + */ + if (nr_pages > 1 && !folio_test_large(folio)) + sc->nr_scanned -= (nr_pages - 1); + return FOLIO_PAGEOUT_ACTIVATE; + case PAGE_KEEP: + return FOLIO_PAGEOUT_KEEP_LOCKED; + case PAGE_SUCCESS: + if (nr_pages > 1 && !folio_test_large(folio)) + sc->nr_scanned -= (nr_pages - 1); + + if (folio_test_writeback(folio)) + return FOLIO_PAGEOUT_KEEP_UNLOCKED; + if (folio_test_dirty(folio)) + return FOLIO_PAGEOUT_KEEP_UNLOCKED; + + /* + * A synchronous write - probably a ramdisk. Go ahead + * and try to reclaim the folio. + */ + if (!folio_trylock(folio)) + return FOLIO_PAGEOUT_KEEP_UNLOCKED; + if (folio_test_dirty(folio) || folio_test_writeback(folio)) + return FOLIO_PAGEOUT_KEEP_LOCKED; + fallthrough; + case PAGE_CLEAN: + return FOLIO_PAGEOUT_FREE; + } + unreachable(); +} + /* - * shrink_folio_list() returns the number of reclaimed pages + * Reclaimed folios are counted in the return value. */ static unsigned int shrink_folio_list(struct list_head *folio_list, struct pglist_data *pgdat, struct scan_control *sc, @@ -1585,7 +1637,6 @@ static unsigned int shrink_folio_list(struct list_head *folio_list, if (folio_maybe_dma_pinned(folio)) goto activate_locked; - mapping = folio_mapping(folio); if (folio_test_dirty(folio)) { if (folio_is_file_lru(folio)) { /* @@ -1606,50 +1657,21 @@ static unsigned int shrink_folio_list(struct list_head *folio_list, goto keep_locked; if (!sc->may_writepage) goto keep_locked; - /* * Folio is dirty. Flush the TLB if a writable entry * potentially exists to avoid CPU writes after I/O * starts and then write it out here. */ try_to_unmap_flush_dirty(); - switch (pageout(&ctx, mapping, folio, folio_list)) { - case PAGE_KEEP: - goto keep_locked; - case PAGE_ACTIVATE: - /* - * If shmem folio is split when writeback to swap, - * the tail pages will make their own pass through - * this function and be accounted then. - */ - if (nr_pages > 1 && !folio_test_large(folio)) { - sc->nr_scanned -= (nr_pages - 1); - nr_pages = 1; - } + switch (folio_try_pageout(folio, sc, &ctx, folio_list)) { + case FOLIO_PAGEOUT_ACTIVATE: goto activate_locked; - case PAGE_SUCCESS: - if (nr_pages > 1 && !folio_test_large(folio)) { - sc->nr_scanned -= (nr_pages - 1); - nr_pages = 1; - } - if (folio_test_writeback(folio)) - goto keep; - if (folio_test_dirty(folio)) - goto keep; - - /* - * A synchronous write - probably a ramdisk. Go - * ahead and try to reclaim the folio. - */ - if (!folio_trylock(folio)) - goto keep; - if (folio_test_dirty(folio) || - folio_test_writeback(folio)) - goto keep_locked; - mapping = folio_mapping(folio); - fallthrough; - case PAGE_CLEAN: - ; /* try to free the folio below */ + case FOLIO_PAGEOUT_KEEP_LOCKED: + goto keep_locked; + case FOLIO_PAGEOUT_KEEP_UNLOCKED: + goto keep; + case FOLIO_PAGEOUT_FREE: + break; /* folio is locked; try to free it below */ } } -- 2.55.0