From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from outbound.qs.icloud.com (qs-2001g-snip4-6.eps.apple.com [57.103.87.69]) (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 0F84E396588 for ; Mon, 20 Jul 2026 05:08:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=57.103.87.69 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784524087; cv=none; b=MsPYYxxLhI/tE36/s7IJxBEJMOKLarBr7btOPFmf/gr0dLgddpIIIj9ZHzl5lcd7atvcmMJN+/dMEoQm+Qew1LzPq6RdOICWAqZnSXj6e0FkjNWAGSq+eI6n/GKswhJFt8XJH6S//B0r7YjgFpkR52NHk8t7amyR15W/uyhjvco= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784524087; c=relaxed/simple; bh=3E2ywKByDNomV6rtuYwrWIJ7KWxsBoMRkdyyYPY6J20=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=QfxXQLy+rKmGDC4RvrZvIeqaI9l8J03bYLxNUlsbP+GNsgGyLgpi/CcAbliV/m0HW7XfUJXibZWiltHko7L5Q3BumJ+ussryeOJaAlqIq5VSIdrDNLD62WMP1v7UMih/40pr9HsqqrAVBrARW6BS/LuwhuY/N36ebLTBIsfEbYs= 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=1LuCG8cT; arc=none smtp.client-ip=57.103.87.69 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="1LuCG8cT" Received: from outbound.qs.icloud.com (unknown [127.0.0.2]) by p00-icloudmta-asmtp-us-east-2d-60-percent-7 (Postfix) with ESMTPS id 2B5771800142; Mon, 20 Jul 2026 05:08:02 +0000 (UTC) X-ICL-RepId: 019f7dec-8f5b-7282-86be-ea88f13742cf X-ICL-Out-Info: HUtFAUMEWwJACUgBTUQeDx5WFlZNRAJCTQhMHVwGXRxCCkEdXgBLVxQEAlodRw5AHVYWWAhOK1sTVRdGCRkIXR0ZHldQXgheH0wcHQ5YBhICWkUBXRcDVxxWRVwYQwldBVccHRxERVsTVRdGCRkIXR0ZCEcfCjADQg5WA0MHRQAtGRxXUF4IXh9MHB0OWAYSHVAcDlEFWwBGCU8BXRoJUwRaEB4ZWwkfFlUNQAUaHQddC1VXDw5fAREJHAMJAQlyGVoUXBhTRVEfVEYTGU4bV01QG18CQg8= Dkim-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=icloud.com; s=1a1hai; t=1784524085; x=1787116085; bh=r8aZQYm37v3YWegSE0f/522iUhXwfG0dV8XiPNn7/zA=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:To:x-icloud-hme; b=1LuCG8cTW02fWgPcouVYMSt/nzspcCwjz8cBq9KFVAXIBqTng9tCvRTn2dCAnZzYQFsu8X2ti+c3ZnV9NtPM+N7T25BeOEs2Tudu8un+4Zjos0vfjGlyzgLiYAk2rG1tCHBraDZzKpWBv8on5BivMhdm66hbpF67YFCZb8vDd8gR5eykWimuxJ/DJrI2iQ+xnuWbvzeNPJpat6UbVTHrkAX+8vviAtWQ54Y4KA6BiJTJYaiIGmENrWF0TQyFT5jvdYuHY6FhF0xDU4MAWXrHKYSKOlJyLDh06clYHN/LMDblWVjCdarKt4ARPPn5vGCti3y/e641H3iLHoukXxm8pw== Received: from [21.6.122.162] (unknown [17.57.155.37]) by p00-icloudmta-asmtp-us-east-2d-60-percent-7 (Postfix) with ESMTPSA id A8A6A180014F; Mon, 20 Jul 2026 05:07:57 +0000 (UTC) From: Zhang Peng Date: Mon, 20 Jul 2026 13:07:39 +0800 Subject: [PATCH v5 2/5] mm/vmscan: extract folio_free() 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: <20260720-batch-tlb-flush-v5-2-db943a0d0d6b@icloud.com> References: <20260720-batch-tlb-flush-v5-0-db943a0d0d6b@icloud.com> In-Reply-To: <20260720-batch-tlb-flush-v5-0-db943a0d0d6b@icloud.com> To: Andrew Morton , David Hildenbrand , Lorenzo Stoakes , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Johannes Weiner , Shakeel Butt , Axel Rasmussen , Yuanchu Xie , Wei Xu , Michal Hocko , Qi Zheng , "Liam R. Howlett" , Qi Zheng Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Barry Song , Kairui Song , Zhang Peng X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1784524064; l=6784; i=zippermonkey@icloud.com; s=20260309; h=from:subject:message-id; bh=3E2ywKByDNomV6rtuYwrWIJ7KWxsBoMRkdyyYPY6J20=; b=xwLl6qIZQKNcuhsChppd92HF64KnsIhpGCL3RSjuWjDbfuuywcAeINyOZGLuqw21yHt4lJDb3 pmdrUVoASFpB7EEMrB8fI9pnl2tna6V9T/qRQxbTy923iTqsnu9AQp5 X-Developer-Key: i=zippermonkey@icloud.com; a=ed25519; pk=tPCLpFnBfIyHsp0k7eaUTUREEa36bQNW/69X+NS8wBU= X-Proofpoint-GUID: S0J1xPK0SItHssd-vov55hLNs4bFa7L1 X-Proofpoint-ORIG-GUID: S0J1xPK0SItHssd-vov55hLNs4bFa7L1 X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwNzIwMDA1NCBTYWx0ZWRfX4w7MkmROKZPX ShHbKgcSO4k4EoMq9RR4TnRAan8uFln4NGYmXZUbrCC8edrwzkW7A+e6EjmB7r/nsllO0aMVBmt BGM85RqenWX1Ak0Ugjd8tH57hIGC/Q7bsa9ahUuRG+leIagFrP3YxLl5NZICMtjKZARYLqJqqAn NdwubE7x2XEVovL4nvUvomWRLsKyknQ1KEaddr/z2LVup4uts9L5GCl4UUg3rqd2RJuqMO/GIcP zbQDNG/J/Rb5DNO9At2fBazqvPAlQ/v96i+oES0BqJkPMvqLCjOnfcn5LMuODdxu7t+Q0weuyUO Hyozsts2ySvEPNUhM5V shrink_folio_list() contains a self-contained folio-freeing section: buffer release, lazyfree, __remove_mapping, and folio_batch drain. Extract it into folio_free() to reduce the size of shrink_folio_list() and make the freeing step independently readable. No functional change. Signed-off-by: Zhang Peng --- mm/vmscan.c | 164 +++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 89 insertions(+), 75 deletions(-) diff --git a/mm/vmscan.c b/mm/vmscan.c index 5ba880dce21e..a0807dd01c5a 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -1076,6 +1076,93 @@ static void folio_activate_locked(struct folio *folio, } } +static bool folio_try_reclaim_free(struct folio *folio, + struct folio_batch *free_folios, + struct scan_control *sc, struct reclaim_stat *stat, + unsigned int *nr_reclaimed) +{ + const unsigned int nr_pages = folio_nr_pages(folio); + struct address_space *mapping = folio_mapping(folio); + + /* + * If the folio has buffers, try to free the buffer mappings + * associated with this folio. If we succeed we try to free + * the folio as well. + * + * We do this even if the folio is dirty. + * filemap_release_folio() does not perform I/O, but it is + * possible for a folio to have the dirty flag set, but it + * is actually clean (all its buffers are clean). This + * happens if the buffers were written out directly, with + * submit_bh(). ext3 will do this, as well as the blockdev + * mapping. filemap_release_folio() will discover that + * cleanness and will drop the buffers and mark the folio + * clean - it can be freed. + * + * Rarely, folios can have buffers and no ->mapping. These + * are the folios which were not successfully invalidated in + * truncate_cleanup_folio(). We try to drop those buffers + * here and if that worked, and the folio is no longer + * mapped into process address space (refcount == 1) it can + * be freed. Otherwise, leave the folio on the LRU so it is + * swappable. + */ + if (folio_needs_release(folio)) { + if (!filemap_release_folio(folio, sc->gfp_mask)) { + folio_activate_locked(folio, stat); + return false; + } + + if (!mapping && folio_ref_count(folio) == 1) { + folio_unlock(folio); + if (folio_put_testzero(folio)) + goto free_it; + else { + /* + * rare race with speculative reference. + * the speculative reference will free + * this folio shortly, so we may + * increment nr_reclaimed here (and + * leave it off the LRU). + */ + *nr_reclaimed += nr_pages; + return true; + } + } + } + + if (folio_test_lazyfree(folio)) { + /* follow __remove_mapping for reference */ + if (!folio_ref_freeze(folio, 1)) + return false; + /* + * The folio has only one reference left, which is + * from the isolation. After the caller puts the + * folio back on the lru and drops the reference, the + * folio will be freed anyway. It doesn't matter + * which lru it goes on. So we don't bother checking + * the dirty flag here. + */ + count_vm_events(PGLAZYFREED, nr_pages); + count_memcg_folio_events(folio, PGLAZYFREED, nr_pages); + } else if (!mapping || !__remove_mapping(mapping, folio, true, + sc->target_mem_cgroup)) + return false; + + folio_unlock(folio); +free_it: + VM_WARN_ON_ONCE_FOLIO(folio_ref_count(folio), folio); + *nr_reclaimed += nr_pages; + + folio_unqueue_deferred_split(folio); + if (folio_batch_add(free_folios, folio) == 0) { + mem_cgroup_uncharge_folios(free_folios); + try_to_unmap_flush(); + free_unref_folios(free_folios); + } + return true; +} + /* * shrink_folio_list() returns the number of reclaimed pages */ @@ -1463,82 +1550,9 @@ static unsigned int shrink_folio_list(struct list_head *folio_list, } } - /* - * If the folio has buffers, try to free the buffer - * mappings associated with this folio. If we succeed - * we try to free the folio as well. - * - * We do this even if the folio is dirty. - * filemap_release_folio() does not perform I/O, but it - * is possible for a folio to have the dirty flag set, - * but it is actually clean (all its buffers are clean). - * This happens if the buffers were written out directly, - * with submit_bh(). ext3 will do this, as well as - * the blockdev mapping. filemap_release_folio() will - * discover that cleanness and will drop the buffers - * and mark the folio clean - it can be freed. - * - * Rarely, folios can have buffers and no ->mapping. - * These are the folios which were not successfully - * invalidated in truncate_cleanup_folio(). We try to - * drop those buffers here and if that worked, and the - * folio is no longer mapped into process address space - * (refcount == 1) it can be freed. Otherwise, leave - * the folio on the LRU so it is swappable. - */ - if (folio_needs_release(folio)) { - if (!filemap_release_folio(folio, sc->gfp_mask)) - goto activate_locked; - if (!mapping && folio_ref_count(folio) == 1) { - folio_unlock(folio); - if (folio_put_testzero(folio)) - goto free_it; - else { - /* - * rare race with speculative reference. - * the speculative reference will free - * this folio shortly, so we may - * increment nr_reclaimed here (and - * leave it off the LRU). - */ - nr_reclaimed += nr_pages; - continue; - } - } - } - - if (folio_test_lazyfree(folio)) { - /* follow __remove_mapping for reference */ - if (!folio_ref_freeze(folio, 1)) - goto keep_locked; - /* - * The folio has only one reference left, which is - * from the isolation. After the caller puts the - * folio back on the lru and drops the reference, the - * folio will be freed anyway. It doesn't matter - * which lru it goes on. So we don't bother checking - * the dirty flag here. - */ - count_vm_events(PGLAZYFREED, nr_pages); - count_memcg_folio_events(folio, PGLAZYFREED, nr_pages); - } else if (!mapping || !__remove_mapping(mapping, folio, true, - sc->target_mem_cgroup)) + if (!folio_try_reclaim_free(folio, &free_folios, sc, stat, + &nr_reclaimed)) goto keep_locked; - - folio_unlock(folio); -free_it: - /* - * Folio may get swapped out as a whole, need to account - * all pages in it. - */ - nr_reclaimed += nr_pages; - - folio_unqueue_deferred_split(folio); - if (folio_batch_add(&free_folios, folio) == 0) { - mem_cgroup_uncharge_folios(&free_folios); - try_to_unmap_flush(); - free_unref_folios(&free_folios); - } continue; activate_locked_split: -- 2.43.7