linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH RFC] mm: count zeromap read and set for swapout and swapin
@ 2024-10-27  1:19 Barry Song
  2024-10-27  2:45 ` Nhat Pham
  0 siblings, 1 reply; 27+ messages in thread
From: Barry Song @ 2024-10-27  1:19 UTC (permalink / raw)
  To: akpm, linux-mm
  Cc: linux-kernel, Barry Song, Usama Arif, Chengming Zhou,
	Yosry Ahmed, Nhat Pham, Johannes Weiner, David Hildenbrand,
	Hugh Dickins, Matthew Wilcox, Shakeel Butt, Andi Kleen,
	Baolin Wang, Chris Li, Huang, Ying, Kairui Song, Ryan Roberts

From: Barry Song <v-songbaohua@oppo.com>

When the proportion of folios from the zero map is small, missing their
accounting may not significantly impact profiling. However, it’s easy
to construct a scenario where this becomes an issue—for example,
allocating 1 GB of memory, writing zeros from userspace, followed by
MADV_PAGEOUT, and then swapping it back in. In this case, the swap-out
and swap-in counts seem to vanish into a black hole, potentially
causing semantic ambiguity.

We have two ways to address this:

1. Add a separate counter specifically for the zero map.
2. Continue using the current accounting, treating the zero map like
a normal backend. (This aligns with the current behavior of zRAM
when supporting same-page fills at the device level.)

This patch adopts option 2. I'm curious if others have different
opinions, so I'm marking it as RFC.

Fixes: 0ca0c24e3211 ("mm: store zero pages to be swapped out in a bitmap")
Cc: Usama Arif <usamaarif642@gmail.com>
Cc: Chengming Zhou <chengming.zhou@linux.dev>
Cc: Yosry Ahmed <yosryahmed@google.com>
Cc: Nhat Pham <nphamcs@gmail.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: David Hildenbrand <david@redhat.com>
Cc: Hugh Dickins <hughd@google.com>
Cc: Matthew Wilcox (Oracle) <willy@infradead.org>
Cc: Shakeel Butt <shakeel.butt@linux.dev>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Baolin Wang <baolin.wang@linux.alibaba.com>
Cc: Chris Li <chrisl@kernel.org>
Cc: "Huang, Ying" <ying.huang@intel.com>
Cc: Kairui Song <kasong@tencent.com>
Cc: Ryan Roberts <ryan.roberts@arm.com>
Signed-off-by: Barry Song <v-songbaohua@oppo.com>
---
 mm/page_io.c | 30 +++++++++++++++++-------------
 1 file changed, 17 insertions(+), 13 deletions(-)

diff --git a/mm/page_io.c b/mm/page_io.c
index 5d9b6e6cf96c..90c5ea870038 100644
--- a/mm/page_io.c
+++ b/mm/page_io.c
@@ -226,6 +226,19 @@ static void swap_zeromap_folio_clear(struct folio *folio)
 	}
 }
 
+static inline void count_swpout_vm_event(struct folio *folio)
+{
+#ifdef CONFIG_TRANSPARENT_HUGEPAGE
+	if (unlikely(folio_test_pmd_mappable(folio))) {
+		count_memcg_folio_events(folio, THP_SWPOUT, 1);
+		count_vm_event(THP_SWPOUT);
+	}
+#endif
+	count_mthp_stat(folio_order(folio), MTHP_STAT_SWPOUT);
+	count_memcg_folio_events(folio, PSWPOUT, folio_nr_pages(folio));
+	count_vm_events(PSWPOUT, folio_nr_pages(folio));
+}
+
 /*
  * We may have stale swap cache pages in memory: notice
  * them here and get rid of the unnecessary final write.
@@ -258,6 +271,7 @@ int swap_writepage(struct page *page, struct writeback_control *wbc)
 	 */
 	if (is_folio_zero_filled(folio)) {
 		swap_zeromap_folio_set(folio);
+		count_swpout_vm_event(folio);
 		folio_unlock(folio);
 		return 0;
 	} else {
@@ -282,19 +296,6 @@ int swap_writepage(struct page *page, struct writeback_control *wbc)
 	return 0;
 }
 
-static inline void count_swpout_vm_event(struct folio *folio)
-{
-#ifdef CONFIG_TRANSPARENT_HUGEPAGE
-	if (unlikely(folio_test_pmd_mappable(folio))) {
-		count_memcg_folio_events(folio, THP_SWPOUT, 1);
-		count_vm_event(THP_SWPOUT);
-	}
-#endif
-	count_mthp_stat(folio_order(folio), MTHP_STAT_SWPOUT);
-	count_memcg_folio_events(folio, PSWPOUT, folio_nr_pages(folio));
-	count_vm_events(PSWPOUT, folio_nr_pages(folio));
-}
-
 #if defined(CONFIG_MEMCG) && defined(CONFIG_BLK_CGROUP)
 static void bio_associate_blkg_from_page(struct bio *bio, struct folio *folio)
 {
@@ -621,6 +622,9 @@ void swap_read_folio(struct folio *folio, struct swap_iocb **plug)
 	delayacct_swapin_start();
 
 	if (swap_read_folio_zeromap(folio)) {
+		count_mthp_stat(folio_order(folio), MTHP_STAT_SWPIN);
+		count_memcg_folio_events(folio, PSWPIN, folio_nr_pages(folio));
+		count_vm_events(PSWPIN, folio_nr_pages(folio));
 		folio_unlock(folio);
 		goto finish;
 	} else if (zswap_load(folio)) {
-- 
2.39.3 (Apple Git-146)



^ permalink raw reply	[flat|nested] 27+ messages in thread

end of thread, other threads:[~2024-10-30 23:46 UTC | newest]

Thread overview: 27+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-10-27  1:19 [PATCH RFC] mm: count zeromap read and set for swapout and swapin Barry Song
2024-10-27  2:45 ` Nhat Pham
2024-10-28  2:32   ` Barry Song
2024-10-28 12:23     ` Usama Arif
2024-10-28 16:33       ` Nhat Pham
2024-10-28 17:00         ` Usama Arif
2024-10-28 17:08           ` Yosry Ahmed
2024-10-28 17:19             ` Usama Arif
2024-10-28 19:54               ` Barry Song
2024-10-28 19:58                 ` Yosry Ahmed
2024-10-28 20:00                 ` Usama Arif
2024-10-28 20:42                   ` Barry Song
2024-10-28 20:51                     ` Usama Arif
2024-10-28 21:15                       ` Barry Song
2024-10-28 21:24                         ` Usama Arif
2024-10-28 21:40                           ` Barry Song
2024-10-28 21:49                             ` Usama Arif
2024-10-28 22:11                               ` Barry Song
2024-10-28 22:32                                 ` Yosry Ahmed
2024-10-28 22:51                                   ` Barry Song
2024-10-28 22:54                                     ` Yosry Ahmed
2024-10-28 23:03                                       ` Barry Song
2024-10-29 17:46                                         ` Nhat Pham
2024-10-29 17:55                                           ` Yosry Ahmed
2024-10-30 23:46                                             ` Nhat Pham
2024-10-28 16:34     ` Nhat Pham
2024-10-28 17:17       ` David Hildenbrand

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox