* [PATCH v7 0/2] mm: store zero pages to be swapped out in a bitmap @ 2024-06-27 10:55 Usama Arif 2024-06-27 10:55 ` [PATCH v7 1/2] " Usama Arif 2024-06-27 10:55 ` [PATCH v7 2/2] mm: remove code to handle same filled pages Usama Arif 0 siblings, 2 replies; 11+ messages in thread From: Usama Arif @ 2024-06-27 10:55 UTC (permalink / raw) To: akpm Cc: hannes, shakeel.butt, david, ying.huang, hughd, willy, yosryahmed, nphamcs, chengming.zhou, linux-mm, linux-kernel, kernel-team, Usama Arif As shown in the patchseries that introduced the zswap same-filled optimization [1], 10-20% of the pages stored in zswap are same-filled. This is also observed across Meta's server fleet. By using VM counters in swap_writepage (not included in this patchseries) it was found that less than 1% of the same-filled pages to be swapped out are non-zero pages. For conventional swap setup (without zswap), rather than reading/writing these pages to flash resulting in increased I/O and flash wear, a bitmap can be used to mark these pages as zero at write time, and the pages can be filled at read time if the bit corresponding to the page is set. When using zswap with swap, this also means that a zswap_entry does not need to be allocated for zero filled pages resulting in memory savings which would offset the memory used for the bitmap. A similar attempt was made earlier in [2] where zswap would only track zero-filled pages instead of same-filled. This patchseries adds zero-filled pages optimization to swap (hence it can be used even if zswap is disabled) and removes the same-filled code from zswap (as only 1% of the same-filled pages are non-zero), simplifying code. This patchseries is based on mm-unstable. [1] https://lore.kernel.org/all/20171018104832epcms5p1b2232e2236258de3d03d1344dde9fce0@epcms5p1/ [2] https://lore.kernel.org/lkml/20240325235018.2028408-1-yosryahmed@google.com/ --- v6 -> v7: (Yosry and David): - Change to kvmalloc_array for zeromap allocation instead of kvzalloc as it does an additional overflow check, and use sizeof(unsigned long) for allocation size calculation to take into account 32 bit kernels. v5 -> v6 (kernel test robot <oliver.sang@intel.com>): - change bitmap_zalloc/free to kvzalloc/free as a very large swap file will result in the allocation order to exceed MAX_PAGE_ORDER retulting in bitmap_zalloc to fail. v4 -> v5 (Yosry): - Correct comment about using clear_bit instead of bitmp_clear. - Remove clearing the zeromap from swap_cluster_schedule_discard and swap_do_scheduled_discard. v3 -> v4: - remove folio_start/end_writeback when folio is zero filled at swap_writepage (Matthew) - check if a large folio is partially in zeromap and return without folio_mark_uptodate so that an IO error is emitted, rather than checking zswap/disk (Yosry) - clear zeromap in swap_free_cluster (Nhat) v2 -> v3: - Going back to the v1 version of the implementation (David and Shakeel) - convert unatomic bitmap_set/clear to atomic set/clear_bit (Johannes) - use clear_highpage instead of folio_page_zero_fill (Yosry) v1 -> v2: - instead of using a bitmap in swap, clear pte for zero pages and let do_pte_missing handle this page at page fault. (Yosry and Matthew) - Check end of page first when checking if folio is zero filled as it could lead to better performance. (Yosry) Usama Arif (2): mm: store zero pages to be swapped out in a bitmap mm: remove code to handle same filled pages include/linux/swap.h | 1 + mm/page_io.c | 113 ++++++++++++++++++++++++++++++++++++++++++- mm/swapfile.c | 20 ++++++++ mm/zswap.c | 86 +++----------------------------- 4 files changed, 141 insertions(+), 79 deletions(-) -- 2.43.0 ^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH v7 1/2] mm: store zero pages to be swapped out in a bitmap 2024-06-27 10:55 [PATCH v7 0/2] mm: store zero pages to be swapped out in a bitmap Usama Arif @ 2024-06-27 10:55 ` Usama Arif 2024-06-27 16:18 ` Johannes Weiner 2024-06-27 10:55 ` [PATCH v7 2/2] mm: remove code to handle same filled pages Usama Arif 1 sibling, 1 reply; 11+ messages in thread From: Usama Arif @ 2024-06-27 10:55 UTC (permalink / raw) To: akpm Cc: hannes, shakeel.butt, david, ying.huang, hughd, willy, yosryahmed, nphamcs, chengming.zhou, linux-mm, linux-kernel, kernel-team, Usama Arif, Andi Kleen Approximately 10-20% of pages to be swapped out are zero pages [1]. Rather than reading/writing these pages to flash resulting in increased I/O and flash wear, a bitmap can be used to mark these pages as zero at write time, and the pages can be filled at read time if the bit corresponding to the page is set. With this patch, NVMe writes in Meta server fleet decreased by almost 10% with conventional swap setup (zswap disabled). [1] https://lore.kernel.org/all/20171018104832epcms5p1b2232e2236258de3d03d1344dde9fce0@epcms5p1/ Signed-off-by: Usama Arif <usamaarif642@gmail.com> Reviewed-by: Chengming Zhou <chengming.zhou@linux.dev> Reviewed-by: Yosry Ahmed <yosryahmed@google.com> Reviewed-by: Nhat Pham <nphamcs@gmail.com> Cc: David Hildenbrand <david@redhat.com> Cc: "Huang, Ying" <ying.huang@intel.com> Cc: Hugh Dickins <hughd@google.com> Cc: Johannes Weiner <hannes@cmpxchg.org> Cc: Matthew Wilcox (Oracle) <willy@infradead.org> Cc: Shakeel Butt <shakeel.butt@linux.dev> Cc: Usama Arif <usamaarif642@gmail.com> Cc: Andi Kleen <ak@linux.intel.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> --- include/linux/swap.h | 1 + mm/page_io.c | 113 ++++++++++++++++++++++++++++++++++++++++++- mm/swapfile.c | 20 ++++++++ 3 files changed, 133 insertions(+), 1 deletion(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index 3df75d62a835..ed03d421febd 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -299,6 +299,7 @@ struct swap_info_struct { signed char type; /* strange name for an index */ unsigned int max; /* extent of the swap_map */ unsigned char *swap_map; /* vmalloc'ed array of usage counts */ + unsigned long *zeromap; /* vmalloc'ed bitmap to track zero pages */ struct swap_cluster_info *cluster_info; /* cluster info. Only for SSD */ struct swap_cluster_list free_clusters; /* free clusters list */ unsigned int lowest_bit; /* index of first free in swap_map */ diff --git a/mm/page_io.c b/mm/page_io.c index 6c1c1828bb88..480b8f221d90 100644 --- a/mm/page_io.c +++ b/mm/page_io.c @@ -172,6 +172,88 @@ int generic_swapfile_activate(struct swap_info_struct *sis, goto out; } +static bool is_folio_page_zero_filled(struct folio *folio, int i) +{ + unsigned long *data; + unsigned int pos, last_pos = PAGE_SIZE / sizeof(*data) - 1; + bool ret = false; + + data = kmap_local_folio(folio, i * PAGE_SIZE); + if (data[last_pos]) + goto out; + for (pos = 0; pos < PAGE_SIZE / sizeof(*data); pos++) { + if (data[pos]) + goto out; + } + ret = true; +out: + kunmap_local(data); + return ret; +} + +static bool is_folio_zero_filled(struct folio *folio) +{ + unsigned int i; + + for (i = 0; i < folio_nr_pages(folio); i++) { + if (!is_folio_page_zero_filled(folio, i)) + return false; + } + return true; +} + +static void folio_zero_fill(struct folio *folio) +{ + unsigned int i; + + for (i = 0; i < folio_nr_pages(folio); i++) + clear_highpage(folio_page(folio, i)); +} + +static void swap_zeromap_folio_set(struct folio *folio) +{ + struct swap_info_struct *sis = swp_swap_info(folio->swap); + swp_entry_t entry; + unsigned int i; + + for (i = 0; i < folio_nr_pages(folio); i++) { + entry = page_swap_entry(folio_page(folio, i)); + set_bit(swp_offset(entry), sis->zeromap); + } +} + +static void swap_zeromap_folio_clear(struct folio *folio) +{ + struct swap_info_struct *sis = swp_swap_info(folio->swap); + swp_entry_t entry; + unsigned int i; + + for (i = 0; i < folio_nr_pages(folio); i++) { + entry = page_swap_entry(folio_page(folio, i)); + clear_bit(swp_offset(entry), sis->zeromap); + } +} + +/* + * Return the index of the first subpage which is not zero-filled + * according to swap_info_struct->zeromap. + * If all pages are zero-filled according to zeromap, it will return + * folio_nr_pages(folio). + */ +static unsigned int swap_zeromap_folio_test(struct folio *folio) +{ + struct swap_info_struct *sis = swp_swap_info(folio->swap); + swp_entry_t entry; + unsigned int i; + + for (i = 0; i < folio_nr_pages(folio); i++) { + entry = page_swap_entry(folio_page(folio, i)); + if (!test_bit(swp_offset(entry), sis->zeromap)) + return i; + } + return i; +} + /* * We may have stale swap cache pages in memory: notice * them here and get rid of the unnecessary final write. @@ -195,6 +277,13 @@ int swap_writepage(struct page *page, struct writeback_control *wbc) folio_unlock(folio); return ret; } + + if (is_folio_zero_filled(folio)) { + swap_zeromap_folio_set(folio); + folio_unlock(folio); + return 0; + } + swap_zeromap_folio_clear(folio); if (zswap_store(folio)) { folio_unlock(folio); return 0; @@ -424,6 +513,26 @@ static void sio_read_complete(struct kiocb *iocb, long ret) mempool_free(sio, sio_pool); } +static bool swap_read_folio_zeromap(struct folio *folio) +{ + unsigned int idx = swap_zeromap_folio_test(folio); + + if (idx == 0) + return false; + + /* + * Swapping in a large folio that is partially in the zeromap is not + * currently handled. Return true without marking the folio uptodate so + * that an IO error is emitted (e.g. do_swap_page() will sigbus). + */ + if (WARN_ON_ONCE(idx < folio_nr_pages(folio))) + return true; + + folio_zero_fill(folio); + folio_mark_uptodate(folio); + return true; +} + static void swap_read_folio_fs(struct folio *folio, struct swap_iocb **plug) { struct swap_info_struct *sis = swp_swap_info(folio->swap); @@ -514,7 +623,9 @@ void swap_read_folio(struct folio *folio, struct swap_iocb **plug) } delayacct_swapin_start(); - if (zswap_load(folio)) { + if (swap_read_folio_zeromap(folio)) { + folio_unlock(folio); + } else if (zswap_load(folio)) { folio_unlock(folio); } else if (data_race(sis->flags & SWP_FS_OPS)) { swap_read_folio_fs(folio, plug); diff --git a/mm/swapfile.c b/mm/swapfile.c index 9c6d8e557c0f..028d33f3a76d 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -747,6 +747,14 @@ static void swap_range_free(struct swap_info_struct *si, unsigned long offset, unsigned long begin = offset; unsigned long end = offset + nr_entries - 1; void (*swap_slot_free_notify)(struct block_device *, unsigned long); + unsigned int i; + + /* + * Use atomic clear_bit operations only on zeromap instead of non-atomic + * bitmap_clear to prevent adjacent bits corruption due to simultaneous writes. + */ + for (i = 0; i < nr_entries; i++) + clear_bit(offset + i, si->zeromap); if (offset < si->lowest_bit) si->lowest_bit = offset; @@ -2635,6 +2643,7 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) free_percpu(p->cluster_next_cpu); p->cluster_next_cpu = NULL; vfree(swap_map); + kvfree(p->zeromap); kvfree(cluster_info); /* Destroy swap account information */ swap_cgroup_swapoff(p->type); @@ -3161,6 +3170,17 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) goto bad_swap_unlock_inode; } + /* + * Use kvmalloc_array instead of bitmap_zalloc as the allocation order might + * be above MAX_PAGE_ORDER incase of a large swap file. + */ + p->zeromap = kvmalloc_array(BITS_TO_LONGS(maxpages), sizeof(unsigned long), + GFP_KERNEL | __GFP_ZERO); + if (!p->zeromap) { + error = -ENOMEM; + goto bad_swap_unlock_inode; + } + if (p->bdev && bdev_stable_writes(p->bdev)) p->flags |= SWP_STABLE_WRITES; -- 2.43.0 ^ permalink raw reply related [flat|nested] 11+ messages in thread
* Re: [PATCH v7 1/2] mm: store zero pages to be swapped out in a bitmap 2024-06-27 10:55 ` [PATCH v7 1/2] " Usama Arif @ 2024-06-27 16:18 ` Johannes Weiner 2024-06-28 15:30 ` Usama Arif 0 siblings, 1 reply; 11+ messages in thread From: Johannes Weiner @ 2024-06-27 16:18 UTC (permalink / raw) To: Usama Arif Cc: akpm, shakeel.butt, david, ying.huang, hughd, willy, yosryahmed, nphamcs, chengming.zhou, linux-mm, linux-kernel, kernel-team, Andi Kleen Hi Usama, On Thu, Jun 27, 2024 at 11:55:29AM +0100, Usama Arif wrote: > Approximately 10-20% of pages to be swapped out are zero pages [1]. > Rather than reading/writing these pages to flash resulting > in increased I/O and flash wear, a bitmap can be used to mark these > pages as zero at write time, and the pages can be filled at > read time if the bit corresponding to the page is set. > With this patch, NVMe writes in Meta server fleet decreased > by almost 10% with conventional swap setup (zswap disabled). > > [1] https://lore.kernel.org/all/20171018104832epcms5p1b2232e2236258de3d03d1344dde9fce0@epcms5p1/ > > Signed-off-by: Usama Arif <usamaarif642@gmail.com> > Reviewed-by: Chengming Zhou <chengming.zhou@linux.dev> > Reviewed-by: Yosry Ahmed <yosryahmed@google.com> > Reviewed-by: Nhat Pham <nphamcs@gmail.com> > Cc: David Hildenbrand <david@redhat.com> > Cc: "Huang, Ying" <ying.huang@intel.com> > Cc: Hugh Dickins <hughd@google.com> > Cc: Johannes Weiner <hannes@cmpxchg.org> > Cc: Matthew Wilcox (Oracle) <willy@infradead.org> > Cc: Shakeel Butt <shakeel.butt@linux.dev> > Cc: Usama Arif <usamaarif642@gmail.com> > Cc: Andi Kleen <ak@linux.intel.com> > Signed-off-by: Andrew Morton <akpm@linux-foundation.org> This looks great to me, and the numbers speak for themselves. A few minor comments below: > --- > include/linux/swap.h | 1 + > mm/page_io.c | 113 ++++++++++++++++++++++++++++++++++++++++++- > mm/swapfile.c | 20 ++++++++ > 3 files changed, 133 insertions(+), 1 deletion(-) > > diff --git a/include/linux/swap.h b/include/linux/swap.h > index 3df75d62a835..ed03d421febd 100644 > --- a/include/linux/swap.h > +++ b/include/linux/swap.h > @@ -299,6 +299,7 @@ struct swap_info_struct { > signed char type; /* strange name for an index */ > unsigned int max; /* extent of the swap_map */ > unsigned char *swap_map; /* vmalloc'ed array of usage counts */ > + unsigned long *zeromap; /* vmalloc'ed bitmap to track zero pages */ > struct swap_cluster_info *cluster_info; /* cluster info. Only for SSD */ > struct swap_cluster_list free_clusters; /* free clusters list */ > unsigned int lowest_bit; /* index of first free in swap_map */ > diff --git a/mm/page_io.c b/mm/page_io.c > index 6c1c1828bb88..480b8f221d90 100644 > --- a/mm/page_io.c > +++ b/mm/page_io.c > @@ -172,6 +172,88 @@ int generic_swapfile_activate(struct swap_info_struct *sis, > goto out; > } > It might be good to have a short comment that gives 1) an overview, that we're using a bitmap to avoid doing IO for zero-filled pages and 2) the locking, that the bits are protected by the locked swapcache folio and atomic updates are used to protect against RMW corruption due to other zero swap entries seeing concurrent updates. > +static bool is_folio_page_zero_filled(struct folio *folio, int i) > +{ > + unsigned long *data; > + unsigned int pos, last_pos = PAGE_SIZE / sizeof(*data) - 1; > + bool ret = false; > + > + data = kmap_local_folio(folio, i * PAGE_SIZE); > + if (data[last_pos]) > + goto out; This shortcut is interesting. It's what zswap does, for which git blame finds a commit with test results: commit 62bf1258ec90554c3c0925951149cfe4b67a3e98 Author: Taejoon Song <taejoon.song@lge.com> Date: Mon Feb 6 04:00:36 2023 +0900 mm/zswap: try to avoid worst-case scenario on same element pages The worst-case scenario on finding same element pages is that almost all elements are same at the first glance but only last few elements are different. Since the same element tends to be grouped from the beginning of the pages, if we check the first element with the last element before looping through all elements, we might have some chances to quickly detect non-same element pages. 1. Test is done under LG webOS TV (64-bit arch) 2. Dump the swap-out pages (~819200 pages) 3. Analyze the pages with simple test script which counts the iteration number and measures the speed at off-line Under 64-bit arch, the worst iteration count is PAGE_SIZE / 8 bytes = 512. The speed is based on the time to consume page_same_filled() function only. The result, on average, is listed as below: Num of Iter Speed(MB/s) Looping-Forward (Orig) 38 99265 Looping-Backward 36 102725 Last-element-check (This Patch) 33 125072 The result shows that the average iteration count decreases by 13% and the speed increases by 25% with this patch. This patch does not increase the overall time complexity, though. I also ran simpler version which uses backward loop. Just looping backward also makes some improvement, but less than this patch. A similar change has already been made to zram in 90f82cbfe502 ("zram: try to avoid worst-case scenario on same element pages"). Since you add it here, and then delete the zswap code in the next patch, this link to the justification will be lost. Can you please add a short comment that explains that this is a measure to avoid worst-case behavior for tail-filled pages, which are common in real life workloads? > + for (pos = 0; pos < PAGE_SIZE / sizeof(*data); pos++) { > + if (data[pos]) > + goto out; > + } > + ret = true; > +out: > + kunmap_local(data); > + return ret; > +} > + > +static bool is_folio_zero_filled(struct folio *folio) > +{ > + unsigned int i; > + > + for (i = 0; i < folio_nr_pages(folio); i++) { > + if (!is_folio_page_zero_filled(folio, i)) > + return false; > + } > + return true; > +} I think these two functions can be merged. It's less code and fewer similar-sounding names in the namespace. > +static void folio_zero_fill(struct folio *folio) > +{ > + unsigned int i; > + > + for (i = 0; i < folio_nr_pages(folio); i++) > + clear_highpage(folio_page(folio, i)); > +} Should this be in highmem.h next to the other folio_zero_* functions? > +static void swap_zeromap_folio_set(struct folio *folio) > +{ > + struct swap_info_struct *sis = swp_swap_info(folio->swap); > + swp_entry_t entry; > + unsigned int i; > + > + for (i = 0; i < folio_nr_pages(folio); i++) { > + entry = page_swap_entry(folio_page(folio, i)); > + set_bit(swp_offset(entry), sis->zeromap); > + } > +} > + > +static void swap_zeromap_folio_clear(struct folio *folio) > +{ > + struct swap_info_struct *sis = swp_swap_info(folio->swap); > + swp_entry_t entry; > + unsigned int i; > + > + for (i = 0; i < folio_nr_pages(folio); i++) { > + entry = page_swap_entry(folio_page(folio, i)); > + clear_bit(swp_offset(entry), sis->zeromap); > + } > +} > + > +/* > + * Return the index of the first subpage which is not zero-filled > + * according to swap_info_struct->zeromap. > + * If all pages are zero-filled according to zeromap, it will return > + * folio_nr_pages(folio). > + */ > +static unsigned int swap_zeromap_folio_test(struct folio *folio) > +{ > + struct swap_info_struct *sis = swp_swap_info(folio->swap); > + swp_entry_t entry; > + unsigned int i; > + > + for (i = 0; i < folio_nr_pages(folio); i++) { > + entry = page_swap_entry(folio_page(folio, i)); > + if (!test_bit(swp_offset(entry), sis->zeromap)) > + return i; > + } > + return i; > +} > + > /* > * We may have stale swap cache pages in memory: notice > * them here and get rid of the unnecessary final write. > @@ -195,6 +277,13 @@ int swap_writepage(struct page *page, struct writeback_control *wbc) > folio_unlock(folio); > return ret; > } > + > + if (is_folio_zero_filled(folio)) { > + swap_zeromap_folio_set(folio); > + folio_unlock(folio); > + return 0; > + } > + swap_zeromap_folio_clear(folio); Just for clarity, it would be good to put this into an else branch and add a comment about overwrites due to changes in the in-memory data. Losing this would cause nasty data corruption, so let's make sure the reason for why it's there is spelled out, and stands out. > if (zswap_store(folio)) { > folio_unlock(folio); > return 0; > @@ -3161,6 +3170,17 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) > goto bad_swap_unlock_inode; > } > > + /* > + * Use kvmalloc_array instead of bitmap_zalloc as the allocation order might > + * be above MAX_PAGE_ORDER incase of a large swap file. > + */ > + p->zeromap = kvmalloc_array(BITS_TO_LONGS(maxpages), sizeof(unsigned long), > + GFP_KERNEL | __GFP_ZERO); sizeof(long) would get this below 80 characters ;) > + if (!p->zeromap) { > + error = -ENOMEM; > + goto bad_swap_unlock_inode; > + } > + With that, Acked-by: Johannes Weiner <hannes@cmpxchg.org> ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v7 1/2] mm: store zero pages to be swapped out in a bitmap 2024-06-27 16:18 ` Johannes Weiner @ 2024-06-28 15:30 ` Usama Arif 2024-07-01 15:01 ` Usama Arif 0 siblings, 1 reply; 11+ messages in thread From: Usama Arif @ 2024-06-28 15:30 UTC (permalink / raw) To: Johannes Weiner Cc: akpm, shakeel.butt, david, ying.huang, hughd, willy, yosryahmed, nphamcs, chengming.zhou, linux-mm, linux-kernel, kernel-team, Andi Kleen On 27/06/2024 19:18, Johannes Weiner wrote: > Hi Usama, > > On Thu, Jun 27, 2024 at 11:55:29AM +0100, Usama Arif wrote: >> Approximately 10-20% of pages to be swapped out are zero pages [1]. >> Rather than reading/writing these pages to flash resulting >> in increased I/O and flash wear, a bitmap can be used to mark these >> pages as zero at write time, and the pages can be filled at >> read time if the bit corresponding to the page is set. >> With this patch, NVMe writes in Meta server fleet decreased >> by almost 10% with conventional swap setup (zswap disabled). >> >> [1] https://lore.kernel.org/all/20171018104832epcms5p1b2232e2236258de3d03d1344dde9fce0@epcms5p1/ >> >> Signed-off-by: Usama Arif <usamaarif642@gmail.com> >> Reviewed-by: Chengming Zhou <chengming.zhou@linux.dev> >> Reviewed-by: Yosry Ahmed <yosryahmed@google.com> >> Reviewed-by: Nhat Pham <nphamcs@gmail.com> >> Cc: David Hildenbrand <david@redhat.com> >> Cc: "Huang, Ying" <ying.huang@intel.com> >> Cc: Hugh Dickins <hughd@google.com> >> Cc: Johannes Weiner <hannes@cmpxchg.org> >> Cc: Matthew Wilcox (Oracle) <willy@infradead.org> >> Cc: Shakeel Butt <shakeel.butt@linux.dev> >> Cc: Usama Arif <usamaarif642@gmail.com> >> Cc: Andi Kleen <ak@linux.intel.com> >> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> > This looks great to me, and the numbers speak for themselves. A few > minor comments below: > >> --- >> include/linux/swap.h | 1 + >> mm/page_io.c | 113 ++++++++++++++++++++++++++++++++++++++++++- >> mm/swapfile.c | 20 ++++++++ >> 3 files changed, 133 insertions(+), 1 deletion(-) >> >> diff --git a/include/linux/swap.h b/include/linux/swap.h >> index 3df75d62a835..ed03d421febd 100644 >> --- a/include/linux/swap.h >> +++ b/include/linux/swap.h >> @@ -299,6 +299,7 @@ struct swap_info_struct { >> signed char type; /* strange name for an index */ >> unsigned int max; /* extent of the swap_map */ >> unsigned char *swap_map; /* vmalloc'ed array of usage counts */ >> + unsigned long *zeromap; /* vmalloc'ed bitmap to track zero pages */ >> struct swap_cluster_info *cluster_info; /* cluster info. Only for SSD */ >> struct swap_cluster_list free_clusters; /* free clusters list */ >> unsigned int lowest_bit; /* index of first free in swap_map */ >> diff --git a/mm/page_io.c b/mm/page_io.c >> index 6c1c1828bb88..480b8f221d90 100644 >> --- a/mm/page_io.c >> +++ b/mm/page_io.c >> @@ -172,6 +172,88 @@ int generic_swapfile_activate(struct swap_info_struct *sis, >> goto out; >> } >> > It might be good to have a short comment that gives 1) an overview, > that we're using a bitmap to avoid doing IO for zero-filled pages and > 2) the locking, that the bits are protected by the locked swapcache > folio and atomic updates are used to protect against RMW corruption > due to other zero swap entries seeing concurrent updates. Thanks! have addressed the comments and will include them in next revision. Just a couple of things. Will add the overview in swap_writepage when the check is made if the folio is zero filled and zeromap bits are set, instead of at this point. >> +static bool is_folio_page_zero_filled(struct folio *folio, int i) >> +{ >> + unsigned long *data; >> + unsigned int pos, last_pos = PAGE_SIZE / sizeof(*data) - 1; >> + bool ret = false; >> + >> + data = kmap_local_folio(folio, i * PAGE_SIZE); >> + if (data[last_pos]) >> + goto out; >> +static void folio_zero_fill(struct folio *folio) >> +{ >> + unsigned int i; >> + >> + for (i = 0; i < folio_nr_pages(folio); i++) >> + clear_highpage(folio_page(folio, i)); >> +} > Should this be in highmem.h next to the other folio_zero_* functions? Thanks for pointing to highmem.h. It already has folio_zero_range, which should do the same thing, so I think I can just do folio_zero_range(folio, 0, folio_size(folio)) and this function shouldnt be needed. ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v7 1/2] mm: store zero pages to be swapped out in a bitmap 2024-06-28 15:30 ` Usama Arif @ 2024-07-01 15:01 ` Usama Arif 2024-07-01 15:37 ` Usama Arif 0 siblings, 1 reply; 11+ messages in thread From: Usama Arif @ 2024-07-01 15:01 UTC (permalink / raw) To: Johannes Weiner, akpm Cc: shakeel.butt, david, ying.huang, hughd, willy, yosryahmed, nphamcs, chengming.zhou, linux-mm, linux-kernel, kernel-team, Andi Kleen On 28/06/2024 18:30, Usama Arif wrote: > > On 27/06/2024 19:18, Johannes Weiner wrote: >> Hi Usama, >> >> On Thu, Jun 27, 2024 at 11:55:29AM +0100, Usama Arif wrote: >>> Approximately 10-20% of pages to be swapped out are zero pages [1]. >>> Rather than reading/writing these pages to flash resulting >>> in increased I/O and flash wear, a bitmap can be used to mark these >>> pages as zero at write time, and the pages can be filled at >>> read time if the bit corresponding to the page is set. >>> With this patch, NVMe writes in Meta server fleet decreased >>> by almost 10% with conventional swap setup (zswap disabled). >>> >>> [1] >>> https://lore.kernel.org/all/20171018104832epcms5p1b2232e2236258de3d03d1344dde9fce0@epcms5p1/ >>> >>> Signed-off-by: Usama Arif <usamaarif642@gmail.com> >>> Reviewed-by: Chengming Zhou <chengming.zhou@linux.dev> >>> Reviewed-by: Yosry Ahmed <yosryahmed@google.com> >>> Reviewed-by: Nhat Pham <nphamcs@gmail.com> >>> Cc: David Hildenbrand <david@redhat.com> >>> Cc: "Huang, Ying" <ying.huang@intel.com> >>> Cc: Hugh Dickins <hughd@google.com> >>> Cc: Johannes Weiner <hannes@cmpxchg.org> >>> Cc: Matthew Wilcox (Oracle) <willy@infradead.org> >>> Cc: Shakeel Butt <shakeel.butt@linux.dev> >>> Cc: Usama Arif <usamaarif642@gmail.com> >>> Cc: Andi Kleen <ak@linux.intel.com> >>> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> >> This looks great to me, and the numbers speak for themselves. A few >> minor comments below: >> >>> --- >>> include/linux/swap.h | 1 + >>> mm/page_io.c | 113 >>> ++++++++++++++++++++++++++++++++++++++++++- >>> mm/swapfile.c | 20 ++++++++ >>> 3 files changed, 133 insertions(+), 1 deletion(-) >>> >>> diff --git a/include/linux/swap.h b/include/linux/swap.h >>> index 3df75d62a835..ed03d421febd 100644 >>> --- a/include/linux/swap.h >>> +++ b/include/linux/swap.h >>> @@ -299,6 +299,7 @@ struct swap_info_struct { >>> signed char type; /* strange name for an index */ >>> unsigned int max; /* extent of the swap_map */ >>> unsigned char *swap_map; /* vmalloc'ed array of usage >>> counts */ >>> + unsigned long *zeromap; /* vmalloc'ed bitmap to track >>> zero pages */ >>> struct swap_cluster_info *cluster_info; /* cluster info. Only >>> for SSD */ >>> struct swap_cluster_list free_clusters; /* free clusters list */ >>> unsigned int lowest_bit; /* index of first free in swap_map */ >>> diff --git a/mm/page_io.c b/mm/page_io.c >>> index 6c1c1828bb88..480b8f221d90 100644 >>> --- a/mm/page_io.c >>> +++ b/mm/page_io.c >>> @@ -172,6 +172,88 @@ int generic_swapfile_activate(struct >>> swap_info_struct *sis, >>> goto out; >>> } >> It might be good to have a short comment that gives 1) an overview, >> that we're using a bitmap to avoid doing IO for zero-filled pages and >> 2) the locking, that the bits are protected by the locked swapcache >> folio and atomic updates are used to protect against RMW corruption >> due to other zero swap entries seeing concurrent updates. > > Thanks! have addressed the comments and will include them in next > revision. Just a couple of things. > > Will add the overview in swap_writepage when the check is made if the > folio is zero filled and zeromap bits are set, instead of at this point. > >>> +static bool is_folio_page_zero_filled(struct folio *folio, int i) >>> +{ >>> + unsigned long *data; >>> + unsigned int pos, last_pos = PAGE_SIZE / sizeof(*data) - 1; >>> + bool ret = false; >>> + >>> + data = kmap_local_folio(folio, i * PAGE_SIZE); >>> + if (data[last_pos]) >>> + goto out; >>> +static void folio_zero_fill(struct folio *folio) >>> +{ >>> + unsigned int i; >>> + >>> + for (i = 0; i < folio_nr_pages(folio); i++) >>> + clear_highpage(folio_page(folio, i)); >>> +} >> Should this be in highmem.h next to the other folio_zero_* functions? > > Thanks for pointing to highmem.h. It already has folio_zero_range, > which should do the same thing, so I think I can just do > folio_zero_range(folio, 0, folio_size(folio)) and this function > shouldnt be needed. How about this? Patch 2 to remove zswap code doesn't change. From b4aa285289b0d5c87b998cc48669cf3fc41fcbb6 Mon Sep 17 00:00:00 2001 From: Usama Arif <usamaarif642@gmail.com> Date: Fri, 24 May 2024 14:52:55 +0100 Subject: [PATCH 1/2] mm: store zero pages to be swapped out in a bitmap Approximately 10-20% of pages to be swapped out are zero pages [1]. Rather than reading/writing these pages to flash resulting in increased I/O and flash wear, a bitmap can be used to mark these pages as zero at write time, and the pages can be filled at read time if the bit corresponding to the page is set. With this patch, NVMe writes in Meta server fleet decreased by almost 10% with conventional swap setup (zswap disabled). [1] https://lore.kernel.org/all/20171018104832epcms5p1b2232e2236258de3d03d1344dde9fce0@epcms5p1/ Signed-off-by: Usama Arif <usamaarif642@gmail.com> Reviewed-by: Chengming Zhou <chengming.zhou@linux.dev> Reviewed-by: Yosry Ahmed <yosryahmed@google.com> Reviewed-by: Nhat Pham <nphamcs@gmail.com> Cc: David Hildenbrand <david@redhat.com> Cc: "Huang, Ying" <ying.huang@intel.com> Cc: Hugh Dickins <hughd@google.com> Cc: Johannes Weiner <hannes@cmpxchg.org> Cc: Matthew Wilcox (Oracle) <willy@infradead.org> Cc: Shakeel Butt <shakeel.butt@linux.dev> Cc: Usama Arif <usamaarif642@gmail.com> Cc: Andi Kleen <ak@linux.intel.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Acked-by: Johannes Weiner <hannes@cmpxchg.org> --- include/linux/swap.h | 1 + mm/page_io.c | 117 ++++++++++++++++++++++++++++++++++++++++++- mm/swapfile.c | 20 ++++++++ 3 files changed, 137 insertions(+), 1 deletion(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index 3df75d62a835..8c38a18320b8 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -299,6 +299,7 @@ struct swap_info_struct { signed char type; /* strange name for an index */ unsigned int max; /* extent of the swap_map */ unsigned char *swap_map; /* vmalloc'ed array of usage counts */ + unsigned long *zeromap; /* kvmalloc'ed bitmap to track zero pages */ struct swap_cluster_info *cluster_info; /* cluster info. Only for SSD */ struct swap_cluster_list free_clusters; /* free clusters list */ unsigned int lowest_bit; /* index of first free in swap_map */ diff --git a/mm/page_io.c b/mm/page_io.c index 6c1c1828bb88..6cbfb277d020 100644 --- a/mm/page_io.c +++ b/mm/page_io.c @@ -172,6 +172,80 @@ int generic_swapfile_activate(struct swap_info_struct *sis, goto out; } +static bool is_folio_zero_filled(struct folio *folio) +{ + unsigned int pos, last_pos; + unsigned long *data; + unsigned int i; + + last_pos = PAGE_SIZE / sizeof(*data) - 1; + for (i = 0; i < folio_nr_pages(folio); i++) { + data = kmap_local_folio(folio, i * PAGE_SIZE); + /* + * Check last word first, incase the page is zero-filled at + * the start and has non-zero data at the end, which is common + * in real-world workloads. + */ + if (data[last_pos]) { + kunmap_local(data); + return false; + } + for (pos = 0; pos < last_pos; pos++) { + if (data[pos]) { + kunmap_local(data); + return false; + } + } + kunmap_local(data); + } + + return true; +} + +static void swap_zeromap_folio_set(struct folio *folio) +{ + struct swap_info_struct *sis = swp_swap_info(folio->swap); + swp_entry_t entry; + unsigned int i; + + for (i = 0; i < folio_nr_pages(folio); i++) { + entry = page_swap_entry(folio_page(folio, i)); + set_bit(swp_offset(entry), sis->zeromap); + } +} + +static void swap_zeromap_folio_clear(struct folio *folio) +{ + struct swap_info_struct *sis = swp_swap_info(folio->swap); + swp_entry_t entry; + unsigned int i; + + for (i = 0; i < folio_nr_pages(folio); i++) { + entry = page_swap_entry(folio_page(folio, i)); + clear_bit(swp_offset(entry), sis->zeromap); + } +} + +/* + * Return the index of the first subpage which is not zero-filled + * according to swap_info_struct->zeromap. + * If all pages are zero-filled according to zeromap, it will return + * folio_nr_pages(folio). + */ +static unsigned int swap_zeromap_folio_test(struct folio *folio) +{ + struct swap_info_struct *sis = swp_swap_info(folio->swap); + swp_entry_t entry; + unsigned int i; + + for (i = 0; i < folio_nr_pages(folio); i++) { + entry = page_swap_entry(folio_page(folio, i)); + if (!test_bit(swp_offset(entry), sis->zeromap)) + return i; + } + return i; +} + /* * We may have stale swap cache pages in memory: notice * them here and get rid of the unnecessary final write. @@ -195,6 +269,25 @@ int swap_writepage(struct page *page, struct writeback_control *wbc) folio_unlock(folio); return ret; } + + /* + * Use a bitmap (zeromap) to avoid doing IO for zero-filled pages. + * The bits in zeromap are protected by the locked swapcache folio + * and atomic updates are used to protect against read-modify-write + * corruption due to other zero swap entries seeing concurrent updates. + */ + if (is_folio_zero_filled(folio)) { + swap_zeromap_folio_set(folio); + folio_unlock(folio); + return 0; + } else { + /* + * Clear bits this folio occupies in the zeromap to prevent + * zero data being read in from any previous zero writes that + * occupied the same swap entries. + */ + swap_zeromap_folio_clear(folio); + } if (zswap_store(folio)) { folio_unlock(folio); return 0; @@ -424,6 +517,26 @@ static void sio_read_complete(struct kiocb *iocb, long ret) mempool_free(sio, sio_pool); } +static bool swap_read_folio_zeromap(struct folio *folio) +{ + unsigned int idx = swap_zeromap_folio_test(folio); + + if (idx == 0) + return false; + + /* + * Swapping in a large folio that is partially in the zeromap is not + * currently handled. Return true without marking the folio uptodate so + * that an IO error is emitted (e.g. do_swap_page() will sigbus). + */ + if (WARN_ON_ONCE(idx < folio_nr_pages(folio))) + return true; + + folio_zero_range(folio, 0, folio_size(folio)); + folio_mark_uptodate(folio); + return true; +} + static void swap_read_folio_fs(struct folio *folio, struct swap_iocb **plug) { struct swap_info_struct *sis = swp_swap_info(folio->swap); @@ -514,7 +627,9 @@ void swap_read_folio(struct folio *folio, struct swap_iocb **plug) } delayacct_swapin_start(); - if (zswap_load(folio)) { + if (swap_read_folio_zeromap(folio)) { + folio_unlock(folio); + } else if (zswap_load(folio)) { folio_unlock(folio); } else if (data_race(sis->flags & SWP_FS_OPS)) { swap_read_folio_fs(folio, plug); diff --git a/mm/swapfile.c b/mm/swapfile.c index 9c6d8e557c0f..4f8e67d18cb5 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -747,6 +747,14 @@ static void swap_range_free(struct swap_info_struct *si, unsigned long offset, unsigned long begin = offset; unsigned long end = offset + nr_entries - 1; void (*swap_slot_free_notify)(struct block_device *, unsigned long); + unsigned int i; + + /* + * Use atomic clear_bit operations only on zeromap instead of non-atomic + * bitmap_clear to prevent adjacent bits corruption due to simultaneous writes. + */ + for (i = 0; i < nr_entries; i++) + clear_bit(offset + i, si->zeromap); if (offset < si->lowest_bit) si->lowest_bit = offset; @@ -2635,6 +2643,7 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) free_percpu(p->cluster_next_cpu); p->cluster_next_cpu = NULL; vfree(swap_map); + kvfree(p->zeromap); kvfree(cluster_info); /* Destroy swap account information */ swap_cgroup_swapoff(p->type); @@ -3161,6 +3170,17 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) goto bad_swap_unlock_inode; } + /* + * Use kvmalloc_array instead of bitmap_zalloc as the allocation order might + * be above MAX_PAGE_ORDER incase of a large swap file. + */ + p->zeromap = kvmalloc_array(BITS_TO_LONGS(maxpages), sizeof(long), + GFP_KERNEL | __GFP_ZERO); + if (!p->zeromap) { + error = -ENOMEM; + goto bad_swap_unlock_inode; + } + if (p->bdev && bdev_stable_writes(p->bdev)) p->flags |= SWP_STABLE_WRITES; -- 2.43.0 ^ permalink raw reply related [flat|nested] 11+ messages in thread
* Re: [PATCH v7 1/2] mm: store zero pages to be swapped out in a bitmap 2024-07-01 15:01 ` Usama Arif @ 2024-07-01 15:37 ` Usama Arif 2024-07-01 17:15 ` Usama Arif 0 siblings, 1 reply; 11+ messages in thread From: Usama Arif @ 2024-07-01 15:37 UTC (permalink / raw) To: Johannes Weiner, akpm Cc: shakeel.butt, david, ying.huang, hughd, willy, yosryahmed, nphamcs, chengming.zhou, linux-mm, linux-kernel, kernel-team, Andi Kleen On 01/07/2024 18:01, Usama Arif wrote: > > On 28/06/2024 18:30, Usama Arif wrote: >> >> On 27/06/2024 19:18, Johannes Weiner wrote: >>> Hi Usama, >>> >>> On Thu, Jun 27, 2024 at 11:55:29AM +0100, Usama Arif wrote: >>>> Approximately 10-20% of pages to be swapped out are zero pages [1]. >>>> Rather than reading/writing these pages to flash resulting >>>> in increased I/O and flash wear, a bitmap can be used to mark these >>>> pages as zero at write time, and the pages can be filled at >>>> read time if the bit corresponding to the page is set. >>>> With this patch, NVMe writes in Meta server fleet decreased >>>> by almost 10% with conventional swap setup (zswap disabled). >>>> >>>> [1] >>>> https://lore.kernel.org/all/20171018104832epcms5p1b2232e2236258de3d03d1344dde9fce0@epcms5p1/ >>>> >>>> Signed-off-by: Usama Arif <usamaarif642@gmail.com> >>>> Reviewed-by: Chengming Zhou <chengming.zhou@linux.dev> >>>> Reviewed-by: Yosry Ahmed <yosryahmed@google.com> >>>> Reviewed-by: Nhat Pham <nphamcs@gmail.com> >>>> Cc: David Hildenbrand <david@redhat.com> >>>> Cc: "Huang, Ying" <ying.huang@intel.com> >>>> Cc: Hugh Dickins <hughd@google.com> >>>> Cc: Johannes Weiner <hannes@cmpxchg.org> >>>> Cc: Matthew Wilcox (Oracle) <willy@infradead.org> >>>> Cc: Shakeel Butt <shakeel.butt@linux.dev> >>>> Cc: Usama Arif <usamaarif642@gmail.com> >>>> Cc: Andi Kleen <ak@linux.intel.com> >>>> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> >>> This looks great to me, and the numbers speak for themselves. A few >>> minor comments below: >>> >>>> --- >>>> include/linux/swap.h | 1 + >>>> mm/page_io.c | 113 >>>> ++++++++++++++++++++++++++++++++++++++++++- >>>> mm/swapfile.c | 20 ++++++++ >>>> 3 files changed, 133 insertions(+), 1 deletion(-) >>>> >>>> diff --git a/include/linux/swap.h b/include/linux/swap.h >>>> index 3df75d62a835..ed03d421febd 100644 >>>> --- a/include/linux/swap.h >>>> +++ b/include/linux/swap.h >>>> @@ -299,6 +299,7 @@ struct swap_info_struct { >>>> signed char type; /* strange name for an index */ >>>> unsigned int max; /* extent of the swap_map */ >>>> unsigned char *swap_map; /* vmalloc'ed array of usage >>>> counts */ >>>> + unsigned long *zeromap; /* vmalloc'ed bitmap to track >>>> zero pages */ >>>> struct swap_cluster_info *cluster_info; /* cluster info. Only >>>> for SSD */ >>>> struct swap_cluster_list free_clusters; /* free clusters list */ >>>> unsigned int lowest_bit; /* index of first free in >>>> swap_map */ >>>> diff --git a/mm/page_io.c b/mm/page_io.c >>>> index 6c1c1828bb88..480b8f221d90 100644 >>>> --- a/mm/page_io.c >>>> +++ b/mm/page_io.c >>>> @@ -172,6 +172,88 @@ int generic_swapfile_activate(struct >>>> swap_info_struct *sis, >>>> goto out; >>>> } >>> It might be good to have a short comment that gives 1) an overview, >>> that we're using a bitmap to avoid doing IO for zero-filled pages and >>> 2) the locking, that the bits are protected by the locked swapcache >>> folio and atomic updates are used to protect against RMW corruption >>> due to other zero swap entries seeing concurrent updates. >> >> Thanks! have addressed the comments and will include them in next >> revision. Just a couple of things. >> >> Will add the overview in swap_writepage when the check is made if the >> folio is zero filled and zeromap bits are set, instead of at this point. >> >>>> +static bool is_folio_page_zero_filled(struct folio *folio, int i) >>>> +{ >>>> + unsigned long *data; >>>> + unsigned int pos, last_pos = PAGE_SIZE / sizeof(*data) - 1; >>>> + bool ret = false; >>>> + >>>> + data = kmap_local_folio(folio, i * PAGE_SIZE); >>>> + if (data[last_pos]) >>>> + goto out; >>>> +static void folio_zero_fill(struct folio *folio) >>>> +{ >>>> + unsigned int i; >>>> + >>>> + for (i = 0; i < folio_nr_pages(folio); i++) >>>> + clear_highpage(folio_page(folio, i)); >>>> +} >>> Should this be in highmem.h next to the other folio_zero_* functions? >> >> Thanks for pointing to highmem.h. It already has folio_zero_range, >> which should do the same thing, so I think I can just do >> folio_zero_range(folio, 0, folio_size(folio)) and this function >> shouldnt be needed. > > > How about this? Patch 2 to remove zswap code doesn't change. > > My mail client had messed up the spacing in my previous reply, so it might not apply cleanly. Hoping it comes out correct this time From b4aa285289b0d5c87b998cc48669cf3fc41fcbb6 Mon Sep 17 00:00:00 2001 From: Usama Arif <usamaarif642@gmail.com> Date: Fri, 24 May 2024 14:52:55 +0100 Subject: [PATCH 1/2] mm: store zero pages to be swapped out in a bitmap Approximately 10-20% of pages to be swapped out are zero pages [1]. Rather than reading/writing these pages to flash resulting in increased I/O and flash wear, a bitmap can be used to mark these pages as zero at write time, and the pages can be filled at read time if the bit corresponding to the page is set. With this patch, NVMe writes in Meta server fleet decreased by almost 10% with conventional swap setup (zswap disabled). [1] https://lore.kernel.org/all/20171018104832epcms5p1b2232e2236258de3d03d1344dde9fce0@epcms5p1/ Signed-off-by: Usama Arif <usamaarif642@gmail.com> Reviewed-by: Chengming Zhou <chengming.zhou@linux.dev> Reviewed-by: Yosry Ahmed <yosryahmed@google.com> Reviewed-by: Nhat Pham <nphamcs@gmail.com> Cc: David Hildenbrand <david@redhat.com> Cc: "Huang, Ying" <ying.huang@intel.com> Cc: Hugh Dickins <hughd@google.com> Cc: Johannes Weiner <hannes@cmpxchg.org> Cc: Matthew Wilcox (Oracle) <willy@infradead.org> Cc: Shakeel Butt <shakeel.butt@linux.dev> Cc: Usama Arif <usamaarif642@gmail.com> Cc: Andi Kleen <ak@linux.intel.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Acked-by: Johannes Weiner <hannes@cmpxchg.org> --- include/linux/swap.h | 1 + mm/page_io.c | 117 ++++++++++++++++++++++++++++++++++++++++++- mm/swapfile.c | 20 ++++++++ 3 files changed, 137 insertions(+), 1 deletion(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index 3df75d62a835..8c38a18320b8 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -299,6 +299,7 @@ struct swap_info_struct { signed char type; /* strange name for an index */ unsigned int max; /* extent of the swap_map */ unsigned char *swap_map; /* vmalloc'ed array of usage counts */ + unsigned long *zeromap; /* kvmalloc'ed bitmap to track zero pages */ struct swap_cluster_info *cluster_info; /* cluster info. Only for SSD */ struct swap_cluster_list free_clusters; /* free clusters list */ unsigned int lowest_bit; /* index of first free in swap_map */ diff --git a/mm/page_io.c b/mm/page_io.c index 6c1c1828bb88..6cbfb277d020 100644 --- a/mm/page_io.c +++ b/mm/page_io.c @@ -172,6 +172,80 @@ int generic_swapfile_activate(struct swap_info_struct *sis, goto out; } +static bool is_folio_zero_filled(struct folio *folio) +{ + unsigned int pos, last_pos; + unsigned long *data; + unsigned int i; + + last_pos = PAGE_SIZE / sizeof(*data) - 1; + for (i = 0; i < folio_nr_pages(folio); i++) { + data = kmap_local_folio(folio, i * PAGE_SIZE); + /* + * Check last word first, incase the page is zero-filled at + * the start and has non-zero data at the end, which is common + * in real-world workloads. + */ + if (data[last_pos]) { + kunmap_local(data); + return false; + } + for (pos = 0; pos < last_pos; pos++) { + if (data[pos]) { + kunmap_local(data); + return false; + } + } + kunmap_local(data); + } + + return true; +} + +static void swap_zeromap_folio_set(struct folio *folio) +{ + struct swap_info_struct *sis = swp_swap_info(folio->swap); + swp_entry_t entry; + unsigned int i; + + for (i = 0; i < folio_nr_pages(folio); i++) { + entry = page_swap_entry(folio_page(folio, i)); + set_bit(swp_offset(entry), sis->zeromap); + } +} + +static void swap_zeromap_folio_clear(struct folio *folio) +{ + struct swap_info_struct *sis = swp_swap_info(folio->swap); + swp_entry_t entry; + unsigned int i; + + for (i = 0; i < folio_nr_pages(folio); i++) { + entry = page_swap_entry(folio_page(folio, i)); + clear_bit(swp_offset(entry), sis->zeromap); + } +} + +/* + * Return the index of the first subpage which is not zero-filled + * according to swap_info_struct->zeromap. + * If all pages are zero-filled according to zeromap, it will return + * folio_nr_pages(folio). + */ +static unsigned int swap_zeromap_folio_test(struct folio *folio) +{ + struct swap_info_struct *sis = swp_swap_info(folio->swap); + swp_entry_t entry; + unsigned int i; + + for (i = 0; i < folio_nr_pages(folio); i++) { + entry = page_swap_entry(folio_page(folio, i)); + if (!test_bit(swp_offset(entry), sis->zeromap)) + return i; + } + return i; +} + /* * We may have stale swap cache pages in memory: notice * them here and get rid of the unnecessary final write. @@ -195,6 +269,25 @@ int swap_writepage(struct page *page, struct writeback_control *wbc) folio_unlock(folio); return ret; } + + /* + * Use a bitmap (zeromap) to avoid doing IO for zero-filled pages. + * The bits in zeromap are protected by the locked swapcache folio + * and atomic updates are used to protect against read-modify-write + * corruption due to other zero swap entries seeing concurrent updates. + */ + if (is_folio_zero_filled(folio)) { + swap_zeromap_folio_set(folio); + folio_unlock(folio); + return 0; + } else { + /* + * Clear bits this folio occupies in the zeromap to prevent + * zero data being read in from any previous zero writes that + * occupied the same swap entries. + */ + swap_zeromap_folio_clear(folio); + } if (zswap_store(folio)) { folio_unlock(folio); return 0; @@ -424,6 +517,26 @@ static void sio_read_complete(struct kiocb *iocb, long ret) mempool_free(sio, sio_pool); } +static bool swap_read_folio_zeromap(struct folio *folio) +{ + unsigned int idx = swap_zeromap_folio_test(folio); + + if (idx == 0) + return false; + + /* + * Swapping in a large folio that is partially in the zeromap is not + * currently handled. Return true without marking the folio uptodate so + * that an IO error is emitted (e.g. do_swap_page() will sigbus). + */ + if (WARN_ON_ONCE(idx < folio_nr_pages(folio))) + return true; + + folio_zero_range(folio, 0, folio_size(folio)); + folio_mark_uptodate(folio); + return true; +} + static void swap_read_folio_fs(struct folio *folio, struct swap_iocb **plug) { struct swap_info_struct *sis = swp_swap_info(folio->swap); @@ -514,7 +627,9 @@ void swap_read_folio(struct folio *folio, struct swap_iocb **plug) } delayacct_swapin_start(); - if (zswap_load(folio)) { + if (swap_read_folio_zeromap(folio)) { + folio_unlock(folio); + } else if (zswap_load(folio)) { folio_unlock(folio); } else if (data_race(sis->flags & SWP_FS_OPS)) { swap_read_folio_fs(folio, plug); diff --git a/mm/swapfile.c b/mm/swapfile.c index 9c6d8e557c0f..4f8e67d18cb5 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -747,6 +747,14 @@ static void swap_range_free(struct swap_info_struct *si, unsigned long offset, unsigned long begin = offset; unsigned long end = offset + nr_entries - 1; void (*swap_slot_free_notify)(struct block_device *, unsigned long); + unsigned int i; + + /* + * Use atomic clear_bit operations only on zeromap instead of non-atomic + * bitmap_clear to prevent adjacent bits corruption due to simultaneous writes. + */ + for (i = 0; i < nr_entries; i++) + clear_bit(offset + i, si->zeromap); if (offset < si->lowest_bit) si->lowest_bit = offset; @@ -2635,6 +2643,7 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) free_percpu(p->cluster_next_cpu); p->cluster_next_cpu = NULL; vfree(swap_map); + kvfree(p->zeromap); kvfree(cluster_info); /* Destroy swap account information */ swap_cgroup_swapoff(p->type); @@ -3161,6 +3170,17 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) goto bad_swap_unlock_inode; } + /* + * Use kvmalloc_array instead of bitmap_zalloc as the allocation order might + * be above MAX_PAGE_ORDER incase of a large swap file. + */ + p->zeromap = kvmalloc_array(BITS_TO_LONGS(maxpages), sizeof(long), + GFP_KERNEL | __GFP_ZERO); + if (!p->zeromap) { + error = -ENOMEM; + goto bad_swap_unlock_inode; + } + if (p->bdev && bdev_stable_writes(p->bdev)) p->flags |= SWP_STABLE_WRITES; -- 2.43.0 ^ permalink raw reply related [flat|nested] 11+ messages in thread
* Re: [PATCH v7 1/2] mm: store zero pages to be swapped out in a bitmap 2024-07-01 15:37 ` Usama Arif @ 2024-07-01 17:15 ` Usama Arif 2024-07-04 23:22 ` Andrew Morton 0 siblings, 1 reply; 11+ messages in thread From: Usama Arif @ 2024-07-01 17:15 UTC (permalink / raw) To: Johannes Weiner, akpm Cc: shakeel.butt, david, ying.huang, hughd, willy, yosryahmed, nphamcs, chengming.zhou, linux-mm, linux-kernel, kernel-team, Andi Kleen On 01/07/2024 18:37, Usama Arif wrote: > > On 01/07/2024 18:01, Usama Arif wrote: >> >> On 28/06/2024 18:30, Usama Arif wrote: >>> >>> On 27/06/2024 19:18, Johannes Weiner wrote: >>>> Hi Usama, >>>> >>>> On Thu, Jun 27, 2024 at 11:55:29AM +0100, Usama Arif wrote: >>>>> Approximately 10-20% of pages to be swapped out are zero pages [1]. >>>>> Rather than reading/writing these pages to flash resulting >>>>> in increased I/O and flash wear, a bitmap can be used to mark these >>>>> pages as zero at write time, and the pages can be filled at >>>>> read time if the bit corresponding to the page is set. >>>>> With this patch, NVMe writes in Meta server fleet decreased >>>>> by almost 10% with conventional swap setup (zswap disabled). >>>>> >>>>> [1] https://lore.kernel.org/all/20171018104832epcms5p1b2232e2236258de3d03d1344dde9fce0@epcms5p1/ >>>>> >>>>> Signed-off-by: Usama Arif <usamaarif642@gmail.com> >>>>> Reviewed-by: Chengming Zhou <chengming.zhou@linux.dev> >>>>> Reviewed-by: Yosry Ahmed <yosryahmed@google.com> >>>>> Reviewed-by: Nhat Pham <nphamcs@gmail.com> >>>>> Cc: David Hildenbrand <david@redhat.com> >>>>> Cc: "Huang, Ying" <ying.huang@intel.com> >>>>> Cc: Hugh Dickins <hughd@google.com> >>>>> Cc: Johannes Weiner <hannes@cmpxchg.org> >>>>> Cc: Matthew Wilcox (Oracle) <willy@infradead.org> >>>>> Cc: Shakeel Butt <shakeel.butt@linux.dev> >>>>> Cc: Usama Arif <usamaarif642@gmail.com> >>>>> Cc: Andi Kleen <ak@linux.intel.com> >>>>> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> >>>> This looks great to me, and the numbers speak for themselves. A few >>>> minor comments below: >>>> >>>>> --- >>>>> include/linux/swap.h | 1 + >>>>> mm/page_io.c | 113 ++++++++++++++++++++++++++++++++++++++++++- >>>>> mm/swapfile.c | 20 ++++++++ >>>>> 3 files changed, 133 insertions(+), 1 deletion(-) >>>>> >>>>> diff --git a/include/linux/swap.h b/include/linux/swap.h >>>>> index 3df75d62a835..ed03d421febd 100644 >>>>> --- a/include/linux/swap.h >>>>> +++ b/include/linux/swap.h >>>>> @@ -299,6 +299,7 @@ struct swap_info_struct { >>>>> signed char type; /* strange name for an index */ >>>>> unsigned int max; /* extent of the swap_map */ >>>>> unsigned char *swap_map; /* vmalloc'ed array of usage counts */ >>>>> + unsigned long *zeromap; /* vmalloc'ed bitmap to track zero pages */ >>>>> struct swap_cluster_info *cluster_info; /* cluster info. Only for SSD */ >>>>> struct swap_cluster_list free_clusters; /* free clusters list */ >>>>> unsigned int lowest_bit; /* index of first free in swap_map */ >>>>> diff --git a/mm/page_io.c b/mm/page_io.c >>>>> index 6c1c1828bb88..480b8f221d90 100644 >>>>> --- a/mm/page_io.c >>>>> +++ b/mm/page_io.c >>>>> @@ -172,6 +172,88 @@ int generic_swapfile_activate(struct swap_info_struct *sis, >>>>> goto out; >>>>> } >>>> It might be good to have a short comment that gives 1) an overview, >>>> that we're using a bitmap to avoid doing IO for zero-filled pages and >>>> 2) the locking, that the bits are protected by the locked swapcache >>>> folio and atomic updates are used to protect against RMW corruption >>>> due to other zero swap entries seeing concurrent updates. >>> >>> Thanks! have addressed the comments and will include them in next revision. Just a couple of things. >>> >>> Will add the overview in swap_writepage when the check is made if the folio is zero filled and zeromap bits are set, instead of at this point. >>> >>>>> +static bool is_folio_page_zero_filled(struct folio *folio, int i) >>>>> +{ >>>>> + unsigned long *data; >>>>> + unsigned int pos, last_pos = PAGE_SIZE / sizeof(*data) - 1; >>>>> + bool ret = false; >>>>> + >>>>> + data = kmap_local_folio(folio, i * PAGE_SIZE); >>>>> + if (data[last_pos]) >>>>> + goto out; >>>>> +static void folio_zero_fill(struct folio *folio) >>>>> +{ >>>>> + unsigned int i; >>>>> + >>>>> + for (i = 0; i < folio_nr_pages(folio); i++) >>>>> + clear_highpage(folio_page(folio, i)); >>>>> +} >>>> Should this be in highmem.h next to the other folio_zero_* functions? >>> >>> Thanks for pointing to highmem.h. It already has folio_zero_range, which should do the same thing, so I think I can just do folio_zero_range(folio, 0, folio_size(folio)) and this function shouldnt be needed. >> >> >> How about this? Patch 2 to remove zswap code doesn't change. >> >> > My mail client had messed up the spacing in my previous reply, so it might not apply cleanly. Hoping it comes out correct this time > Sorry for the repeated emails, this one should definitely work. Changes from v7 (Johannes): - Give overview and explain how locking works in zeromap in comments - Add comment for why last word is checked first when checking if folio is zero-filled - Merge is_folio_zero_filled and is_folio_page_zero_filled into 1 function. - Use folio_zero_range to fill a folio with zero at readtime. - Put swap_zeromap_folio_clear in an else branch (even if checkpatch gives warning) and add comment to make it explicitly clear that it needs to happen if folio is not zero filled. - add missing kvfree for zeromap incase swapon fails. From 100983ea94cedf6a342352f91d60a83cf4fa27a5 Mon Sep 17 00:00:00 2001 From: Usama Arif <usamaarif642@gmail.com> Date: Fri, 24 May 2024 14:52:55 +0100 Subject: [PATCH 1/2] mm: store zero pages to be swapped out in a bitmap Approximately 10-20% of pages to be swapped out are zero pages [1]. Rather than reading/writing these pages to flash resulting in increased I/O and flash wear, a bitmap can be used to mark these pages as zero at write time, and the pages can be filled at read time if the bit corresponding to the page is set. With this patch, NVMe writes in Meta server fleet decreased by almost 10% with conventional swap setup (zswap disabled). [1] https://lore.kernel.org/all/20171018104832epcms5p1b2232e2236258de3d03d1344dde9fce0@epcms5p1/ Signed-off-by: Usama Arif <usamaarif642@gmail.com> Reviewed-by: Chengming Zhou <chengming.zhou@linux.dev> Reviewed-by: Yosry Ahmed <yosryahmed@google.com> Reviewed-by: Nhat Pham <nphamcs@gmail.com> Cc: David Hildenbrand <david@redhat.com> Cc: "Huang, Ying" <ying.huang@intel.com> Cc: Hugh Dickins <hughd@google.com> Cc: Johannes Weiner <hannes@cmpxchg.org> Cc: Matthew Wilcox (Oracle) <willy@infradead.org> Cc: Shakeel Butt <shakeel.butt@linux.dev> Cc: Usama Arif <usamaarif642@gmail.com> Cc: Andi Kleen <ak@linux.intel.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Acked-by: Johannes Weiner <hannes@cmpxchg.org> --- include/linux/swap.h | 1 + mm/page_io.c | 117 ++++++++++++++++++++++++++++++++++++++++++- mm/swapfile.c | 21 ++++++++ 3 files changed, 138 insertions(+), 1 deletion(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index 3df75d62a835..8c38a18320b8 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -299,6 +299,7 @@ struct swap_info_struct { signed char type; /* strange name for an index */ unsigned int max; /* extent of the swap_map */ unsigned char *swap_map; /* vmalloc'ed array of usage counts */ + unsigned long *zeromap; /* kvmalloc'ed bitmap to track zero pages */ struct swap_cluster_info *cluster_info; /* cluster info. Only for SSD */ struct swap_cluster_list free_clusters; /* free clusters list */ unsigned int lowest_bit; /* index of first free in swap_map */ diff --git a/mm/page_io.c b/mm/page_io.c index 6c1c1828bb88..6cbfb277d020 100644 --- a/mm/page_io.c +++ b/mm/page_io.c @@ -172,6 +172,80 @@ int generic_swapfile_activate(struct swap_info_struct *sis, goto out; } +static bool is_folio_zero_filled(struct folio *folio) +{ + unsigned int pos, last_pos; + unsigned long *data; + unsigned int i; + + last_pos = PAGE_SIZE / sizeof(*data) - 1; + for (i = 0; i < folio_nr_pages(folio); i++) { + data = kmap_local_folio(folio, i * PAGE_SIZE); + /* + * Check last word first, incase the page is zero-filled at + * the start and has non-zero data at the end, which is common + * in real-world workloads. + */ + if (data[last_pos]) { + kunmap_local(data); + return false; + } + for (pos = 0; pos < last_pos; pos++) { + if (data[pos]) { + kunmap_local(data); + return false; + } + } + kunmap_local(data); + } + + return true; +} + +static void swap_zeromap_folio_set(struct folio *folio) +{ + struct swap_info_struct *sis = swp_swap_info(folio->swap); + swp_entry_t entry; + unsigned int i; + + for (i = 0; i < folio_nr_pages(folio); i++) { + entry = page_swap_entry(folio_page(folio, i)); + set_bit(swp_offset(entry), sis->zeromap); + } +} + +static void swap_zeromap_folio_clear(struct folio *folio) +{ + struct swap_info_struct *sis = swp_swap_info(folio->swap); + swp_entry_t entry; + unsigned int i; + + for (i = 0; i < folio_nr_pages(folio); i++) { + entry = page_swap_entry(folio_page(folio, i)); + clear_bit(swp_offset(entry), sis->zeromap); + } +} + +/* + * Return the index of the first subpage which is not zero-filled + * according to swap_info_struct->zeromap. + * If all pages are zero-filled according to zeromap, it will return + * folio_nr_pages(folio). + */ +static unsigned int swap_zeromap_folio_test(struct folio *folio) +{ + struct swap_info_struct *sis = swp_swap_info(folio->swap); + swp_entry_t entry; + unsigned int i; + + for (i = 0; i < folio_nr_pages(folio); i++) { + entry = page_swap_entry(folio_page(folio, i)); + if (!test_bit(swp_offset(entry), sis->zeromap)) + return i; + } + return i; +} + /* * We may have stale swap cache pages in memory: notice * them here and get rid of the unnecessary final write. @@ -195,6 +269,25 @@ int swap_writepage(struct page *page, struct writeback_control *wbc) folio_unlock(folio); return ret; } + + /* + * Use a bitmap (zeromap) to avoid doing IO for zero-filled pages. + * The bits in zeromap are protected by the locked swapcache folio + * and atomic updates are used to protect against read-modify-write + * corruption due to other zero swap entries seeing concurrent updates. + */ + if (is_folio_zero_filled(folio)) { + swap_zeromap_folio_set(folio); + folio_unlock(folio); + return 0; + } else { + /* + * Clear bits this folio occupies in the zeromap to prevent + * zero data being read in from any previous zero writes that + * occupied the same swap entries. + */ + swap_zeromap_folio_clear(folio); + } if (zswap_store(folio)) { folio_unlock(folio); return 0; @@ -424,6 +517,26 @@ static void sio_read_complete(struct kiocb *iocb, long ret) mempool_free(sio, sio_pool); } +static bool swap_read_folio_zeromap(struct folio *folio) +{ + unsigned int idx = swap_zeromap_folio_test(folio); + + if (idx == 0) + return false; + + /* + * Swapping in a large folio that is partially in the zeromap is not + * currently handled. Return true without marking the folio uptodate so + * that an IO error is emitted (e.g. do_swap_page() will sigbus). + */ + if (WARN_ON_ONCE(idx < folio_nr_pages(folio))) + return true; + + folio_zero_range(folio, 0, folio_size(folio)); + folio_mark_uptodate(folio); + return true; +} + static void swap_read_folio_fs(struct folio *folio, struct swap_iocb **plug) { struct swap_info_struct *sis = swp_swap_info(folio->swap); @@ -514,7 +627,9 @@ void swap_read_folio(struct folio *folio, struct swap_iocb **plug) } delayacct_swapin_start(); - if (zswap_load(folio)) { + if (swap_read_folio_zeromap(folio)) { + folio_unlock(folio); + } else if (zswap_load(folio)) { folio_unlock(folio); } else if (data_race(sis->flags & SWP_FS_OPS)) { swap_read_folio_fs(folio, plug); diff --git a/mm/swapfile.c b/mm/swapfile.c index 9c6d8e557c0f..111ca98a5944 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -747,6 +747,14 @@ static void swap_range_free(struct swap_info_struct *si, unsigned long offset, unsigned long begin = offset; unsigned long end = offset + nr_entries - 1; void (*swap_slot_free_notify)(struct block_device *, unsigned long); + unsigned int i; + + /* + * Use atomic clear_bit operations only on zeromap instead of non-atomic + * bitmap_clear to prevent adjacent bits corruption due to simultaneous writes. + */ + for (i = 0; i < nr_entries; i++) + clear_bit(offset + i, si->zeromap); if (offset < si->lowest_bit) si->lowest_bit = offset; @@ -2635,6 +2643,7 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) free_percpu(p->cluster_next_cpu); p->cluster_next_cpu = NULL; vfree(swap_map); + kvfree(p->zeromap); kvfree(cluster_info); /* Destroy swap account information */ swap_cgroup_swapoff(p->type); @@ -3161,6 +3170,17 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) goto bad_swap_unlock_inode; } + /* + * Use kvmalloc_array instead of bitmap_zalloc as the allocation order might + * be above MAX_PAGE_ORDER incase of a large swap file. + */ + p->zeromap = kvmalloc_array(BITS_TO_LONGS(maxpages), sizeof(long), + GFP_KERNEL | __GFP_ZERO); + if (!p->zeromap) { + error = -ENOMEM; + goto bad_swap_unlock_inode; + } + if (p->bdev && bdev_stable_writes(p->bdev)) p->flags |= SWP_STABLE_WRITES; @@ -3315,6 +3335,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) p->flags = 0; spin_unlock(&swap_lock); vfree(swap_map); + kvfree(p->zeromap); kvfree(cluster_info); if (inced_nr_rotate_swap) atomic_dec(&nr_rotate_swap); -- 2.43.0 ^ permalink raw reply related [flat|nested] 11+ messages in thread
* Re: [PATCH v7 1/2] mm: store zero pages to be swapped out in a bitmap 2024-07-01 17:15 ` Usama Arif @ 2024-07-04 23:22 ` Andrew Morton 2024-07-05 9:41 ` Usama Arif 0 siblings, 1 reply; 11+ messages in thread From: Andrew Morton @ 2024-07-04 23:22 UTC (permalink / raw) To: Usama Arif Cc: Johannes Weiner, shakeel.butt, david, ying.huang, hughd, willy, yosryahmed, nphamcs, chengming.zhou, linux-mm, linux-kernel, kernel-team, Andi Kleen On Mon, 1 Jul 2024 20:15:46 +0300 Usama Arif <usamaarif642@gmail.com> wrote: > Changes from v7 (Johannes): > - Give overview and explain how locking works in zeromap in comments > - Add comment for why last word is checked first when checking if > folio is zero-filled > - Merge is_folio_zero_filled and is_folio_page_zero_filled into > 1 function. > - Use folio_zero_range to fill a folio with zero at readtime. > - Put swap_zeromap_folio_clear in an else branch (even if checkpatch > gives warning) and add comment to make it explicitly clear that it > needs to happen if folio is not zero filled. > - add missing kvfree for zeromap incase swapon fails. I queued the below as a delta against what was in mm-unstable. Can we please get this nailed down? It has been nearly three weeks and this patch is getting in the way of the "mm/zsmalloc: change back to per-size_class lock" series, at least. include/linux/swap.h | 2 - mm/page_io.c | 64 +++++++++++++++++++++-------------------- mm/swapfile.c | 9 +++-- 3 files changed, 40 insertions(+), 35 deletions(-) --- a/include/linux/swap.h~mm-store-zero-pages-to-be-swapped-out-in-a-bitmap-v8 +++ a/include/linux/swap.h @@ -299,7 +299,7 @@ struct swap_info_struct { signed char type; /* strange name for an index */ unsigned int max; /* extent of the swap_map */ unsigned char *swap_map; /* vmalloc'ed array of usage counts */ - unsigned long *zeromap; /* vmalloc'ed bitmap to track zero pages */ + unsigned long *zeromap; /* kvmalloc'ed bitmap to track zero pages */ struct swap_cluster_info *cluster_info; /* cluster info. Only for SSD */ struct swap_cluster_list free_clusters; /* free clusters list */ unsigned int lowest_bit; /* index of first free in swap_map */ --- a/mm/page_io.c~mm-store-zero-pages-to-be-swapped-out-in-a-bitmap-v8 +++ a/mm/page_io.c @@ -172,42 +172,34 @@ bad_bmap: goto out; } -static bool is_folio_page_zero_filled(struct folio *folio, int i) -{ - unsigned long *data; - unsigned int pos, last_pos = PAGE_SIZE / sizeof(*data) - 1; - bool ret = false; - - data = kmap_local_folio(folio, i * PAGE_SIZE); - if (data[last_pos]) - goto out; - for (pos = 0; pos < PAGE_SIZE / sizeof(*data); pos++) { - if (data[pos]) - goto out; - } - ret = true; -out: - kunmap_local(data); - return ret; -} - static bool is_folio_zero_filled(struct folio *folio) { + unsigned int pos, last_pos; + unsigned long *data; unsigned int i; + last_pos = PAGE_SIZE / sizeof(*data) - 1; for (i = 0; i < folio_nr_pages(folio); i++) { - if (!is_folio_page_zero_filled(folio, i)) + data = kmap_local_folio(folio, i * PAGE_SIZE); + /* + * Check last word first, incase the page is zero-filled at + * the start and has non-zero data at the end, which is common + * in real-world workloads. + */ + if (data[last_pos]) { + kunmap_local(data); return false; + } + for (pos = 0; pos < last_pos; pos++) { + if (data[pos]) { + kunmap_local(data); + return false; + } + } + kunmap_local(data); } - return true; -} -static void folio_zero_fill(struct folio *folio) -{ - unsigned int i; - - for (i = 0; i < folio_nr_pages(folio); i++) - clear_highpage(folio_page(folio, i)); + return true; } static void swap_zeromap_folio_set(struct folio *folio) @@ -278,12 +270,24 @@ int swap_writepage(struct page *page, st return ret; } + /* + * Use a bitmap (zeromap) to avoid doing IO for zero-filled pages. + * The bits in zeromap are protected by the locked swapcache folio + * and atomic updates are used to protect against read-modify-write + * corruption due to other zero swap entries seeing concurrent updates. + */ if (is_folio_zero_filled(folio)) { swap_zeromap_folio_set(folio); folio_unlock(folio); return 0; + } else { + /* + * Clear bits this folio occupies in the zeromap to prevent + * zero data being read in from any previous zero writes that + * occupied the same swap entries. + */ + swap_zeromap_folio_clear(folio); } - swap_zeromap_folio_clear(folio); if (zswap_store(folio)) { folio_unlock(folio); return 0; @@ -528,7 +532,7 @@ static bool swap_read_folio_zeromap(stru if (WARN_ON_ONCE(idx < folio_nr_pages(folio))) return true; - folio_zero_fill(folio); + folio_zero_range(folio, 0, folio_size(folio)); folio_mark_uptodate(folio); return true; } --- a/mm/swapfile.c~mm-store-zero-pages-to-be-swapped-out-in-a-bitmap-v8 +++ a/mm/swapfile.c @@ -3181,11 +3181,11 @@ SYSCALL_DEFINE2(swapon, const char __use } /* - * Use kvmalloc_array instead of bitmap_zalloc as the allocation order - * might be above MAX_PAGE_ORDER incase of a large swap file. + * Use kvmalloc_array instead of bitmap_zalloc as the allocation order might + * be above MAX_PAGE_ORDER incase of a large swap file. */ - p->zeromap = kvmalloc_array(BITS_TO_LONGS(maxpages), - sizeof(unsigned long), GFP_KERNEL | __GFP_ZERO); + p->zeromap = kvmalloc_array(BITS_TO_LONGS(maxpages), sizeof(long), + GFP_KERNEL | __GFP_ZERO); if (!p->zeromap) { error = -ENOMEM; goto bad_swap_unlock_inode; @@ -3345,6 +3345,7 @@ bad_swap: p->flags = 0; spin_unlock(&swap_lock); vfree(swap_map); + kvfree(p->zeromap); kvfree(cluster_info); if (inced_nr_rotate_swap) atomic_dec(&nr_rotate_swap); _ ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v7 1/2] mm: store zero pages to be swapped out in a bitmap 2024-07-04 23:22 ` Andrew Morton @ 2024-07-05 9:41 ` Usama Arif 0 siblings, 0 replies; 11+ messages in thread From: Usama Arif @ 2024-07-05 9:41 UTC (permalink / raw) To: Andrew Morton Cc: Johannes Weiner, shakeel.butt, david, ying.huang, hughd, willy, yosryahmed, nphamcs, chengming.zhou, linux-mm, linux-kernel, kernel-team, Andi Kleen On 05/07/2024 02:22, Andrew Morton wrote: > On Mon, 1 Jul 2024 20:15:46 +0300 Usama Arif <usamaarif642@gmail.com> wrote: > >> Changes from v7 (Johannes): >> - Give overview and explain how locking works in zeromap in comments >> - Add comment for why last word is checked first when checking if >> folio is zero-filled >> - Merge is_folio_zero_filled and is_folio_page_zero_filled into >> 1 function. >> - Use folio_zero_range to fill a folio with zero at readtime. >> - Put swap_zeromap_folio_clear in an else branch (even if checkpatch >> gives warning) and add comment to make it explicitly clear that it >> needs to happen if folio is not zero filled. >> - add missing kvfree for zeromap incase swapon fails. > > I queued the below as a delta against what was in mm-unstable. > > Can we please get this nailed down? Thanks, yes its finished. There won't be anymore changes on this anymore. ^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH v7 2/2] mm: remove code to handle same filled pages 2024-06-27 10:55 [PATCH v7 0/2] mm: store zero pages to be swapped out in a bitmap Usama Arif 2024-06-27 10:55 ` [PATCH v7 1/2] " Usama Arif @ 2024-06-27 10:55 ` Usama Arif 2024-06-27 16:19 ` Johannes Weiner 1 sibling, 1 reply; 11+ messages in thread From: Usama Arif @ 2024-06-27 10:55 UTC (permalink / raw) To: akpm Cc: hannes, shakeel.butt, david, ying.huang, hughd, willy, yosryahmed, nphamcs, chengming.zhou, linux-mm, linux-kernel, kernel-team, Usama Arif, Andi Kleen With an earlier commit to handle zero-filled pages in swap directly, and with only 1% of the same-filled pages being non-zero, zswap no longer needs to handle same-filled pages and can just work on compressed pages. Signed-off-by: Usama Arif <usamaarif642@gmail.com> Reviewed-by: Chengming Zhou <chengming.zhou@linux.dev> Acked-by: Yosry Ahmed <yosryahmed@google.com> Reviewed-by: Nhat Pham <nphamcs@gmail.com> Cc: Andi Kleen <ak@linux.intel.com> Cc: David Hildenbrand <david@redhat.com> Cc: "Huang, Ying" <ying.huang@intel.com> Cc: Hugh Dickins <hughd@google.com> Cc: Johannes Weiner <hannes@cmpxchg.org> Cc: Matthew Wilcox (Oracle) <willy@infradead.org> Cc: Shakeel Butt <shakeel.butt@linux.dev> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> --- mm/zswap.c | 86 +++++------------------------------------------------- 1 file changed, 8 insertions(+), 78 deletions(-) diff --git a/mm/zswap.c b/mm/zswap.c index a546c01602aa..e25a6808c2ed 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -44,8 +44,6 @@ **********************************/ /* The number of compressed pages currently stored in zswap */ atomic_t zswap_stored_pages = ATOMIC_INIT(0); -/* The number of same-value filled pages currently stored in zswap */ -static atomic_t zswap_same_filled_pages = ATOMIC_INIT(0); /* * The statistics below are not protected from concurrent access for @@ -188,11 +186,9 @@ static struct shrinker *zswap_shrinker; * * swpentry - associated swap entry, the offset indexes into the red-black tree * length - the length in bytes of the compressed page data. Needed during - * decompression. For a same value filled page length is 0, and both - * pool and lru are invalid and must be ignored. + * decompression. * pool - the zswap_pool the entry's data is in * handle - zpool allocation handle that stores the compressed page data - * value - value of the same-value filled pages which have same content * objcg - the obj_cgroup that the compressed memory is charged to * lru - handle to the pool's lru used to evict pages. */ @@ -200,10 +196,7 @@ struct zswap_entry { swp_entry_t swpentry; unsigned int length; struct zswap_pool *pool; - union { - unsigned long handle; - unsigned long value; - }; + unsigned long handle; struct obj_cgroup *objcg; struct list_head lru; }; @@ -820,13 +813,9 @@ static struct zpool *zswap_find_zpool(struct zswap_entry *entry) */ static void zswap_entry_free(struct zswap_entry *entry) { - if (!entry->length) - atomic_dec(&zswap_same_filled_pages); - else { - zswap_lru_del(&zswap_list_lru, entry); - zpool_free(zswap_find_zpool(entry), entry->handle); - zswap_pool_put(entry->pool); - } + zswap_lru_del(&zswap_list_lru, entry); + zpool_free(zswap_find_zpool(entry), entry->handle); + zswap_pool_put(entry->pool); if (entry->objcg) { obj_cgroup_uncharge_zswap(entry->objcg, entry->length); obj_cgroup_put(entry->objcg); @@ -1268,11 +1257,6 @@ static unsigned long zswap_shrinker_count(struct shrinker *shrinker, * This ensures that the better zswap compresses memory, the fewer * pages we will evict to swap (as it will otherwise incur IO for * relatively small memory saving). - * - * The memory saving factor calculated here takes same-filled pages into - * account, but those are not freeable since they almost occupy no - * space. Hence, we may scale nr_freeable down a little bit more than we - * should if we have a lot of same-filled pages. */ return mult_frac(nr_freeable, nr_backing, nr_stored); } @@ -1376,42 +1360,6 @@ static void shrink_worker(struct work_struct *w) } while (zswap_total_pages() > thr); } -/********************************* -* same-filled functions -**********************************/ -static bool zswap_is_folio_same_filled(struct folio *folio, unsigned long *value) -{ - unsigned long *data; - unsigned long val; - unsigned int pos, last_pos = PAGE_SIZE / sizeof(*data) - 1; - bool ret = false; - - data = kmap_local_folio(folio, 0); - val = data[0]; - - if (val != data[last_pos]) - goto out; - - for (pos = 1; pos < last_pos; pos++) { - if (val != data[pos]) - goto out; - } - - *value = val; - ret = true; -out: - kunmap_local(data); - return ret; -} - -static void zswap_fill_folio(struct folio *folio, unsigned long value) -{ - unsigned long *data = kmap_local_folio(folio, 0); - - memset_l(data, value, PAGE_SIZE / sizeof(unsigned long)); - kunmap_local(data); -} - /********************************* * main API **********************************/ @@ -1423,7 +1371,6 @@ bool zswap_store(struct folio *folio) struct zswap_entry *entry, *old; struct obj_cgroup *objcg = NULL; struct mem_cgroup *memcg = NULL; - unsigned long value; VM_WARN_ON_ONCE(!folio_test_locked(folio)); VM_WARN_ON_ONCE(!folio_test_swapcache(folio)); @@ -1456,13 +1403,6 @@ bool zswap_store(struct folio *folio) goto reject; } - if (zswap_is_folio_same_filled(folio, &value)) { - entry->length = 0; - entry->value = value; - atomic_inc(&zswap_same_filled_pages); - goto store_entry; - } - /* if entry is successfully added, it keeps the reference */ entry->pool = zswap_pool_current_get(); if (!entry->pool) @@ -1480,7 +1420,6 @@ bool zswap_store(struct folio *folio) if (!zswap_compress(folio, entry)) goto put_pool; -store_entry: entry->swpentry = swp; entry->objcg = objcg; @@ -1528,13 +1467,9 @@ bool zswap_store(struct folio *folio) return true; store_failed: - if (!entry->length) - atomic_dec(&zswap_same_filled_pages); - else { - zpool_free(zswap_find_zpool(entry), entry->handle); + zpool_free(zswap_find_zpool(entry), entry->handle); put_pool: - zswap_pool_put(entry->pool); - } + zswap_pool_put(entry->pool); freepage: zswap_entry_cache_free(entry); reject: @@ -1597,10 +1532,7 @@ bool zswap_load(struct folio *folio) if (!entry) return false; - if (entry->length) - zswap_decompress(entry, folio); - else - zswap_fill_folio(folio, entry->value); + zswap_decompress(entry, folio); count_vm_event(ZSWPIN); if (entry->objcg) @@ -1703,8 +1635,6 @@ static int zswap_debugfs_init(void) zswap_debugfs_root, NULL, &total_size_fops); debugfs_create_atomic_t("stored_pages", 0444, zswap_debugfs_root, &zswap_stored_pages); - debugfs_create_atomic_t("same_filled_pages", 0444, - zswap_debugfs_root, &zswap_same_filled_pages); return 0; } -- 2.43.0 ^ permalink raw reply related [flat|nested] 11+ messages in thread
* Re: [PATCH v7 2/2] mm: remove code to handle same filled pages 2024-06-27 10:55 ` [PATCH v7 2/2] mm: remove code to handle same filled pages Usama Arif @ 2024-06-27 16:19 ` Johannes Weiner 0 siblings, 0 replies; 11+ messages in thread From: Johannes Weiner @ 2024-06-27 16:19 UTC (permalink / raw) To: Usama Arif Cc: akpm, shakeel.butt, david, ying.huang, hughd, willy, yosryahmed, nphamcs, chengming.zhou, linux-mm, linux-kernel, kernel-team, Andi Kleen On Thu, Jun 27, 2024 at 11:55:30AM +0100, Usama Arif wrote: > With an earlier commit to handle zero-filled pages in swap directly, > and with only 1% of the same-filled pages being non-zero, zswap no > longer needs to handle same-filled pages and can just work on compressed > pages. > > Signed-off-by: Usama Arif <usamaarif642@gmail.com> > Reviewed-by: Chengming Zhou <chengming.zhou@linux.dev> > Acked-by: Yosry Ahmed <yosryahmed@google.com> > Reviewed-by: Nhat Pham <nphamcs@gmail.com> > Cc: Andi Kleen <ak@linux.intel.com> > Cc: David Hildenbrand <david@redhat.com> > Cc: "Huang, Ying" <ying.huang@intel.com> > Cc: Hugh Dickins <hughd@google.com> > Cc: Johannes Weiner <hannes@cmpxchg.org> > Cc: Matthew Wilcox (Oracle) <willy@infradead.org> > Cc: Shakeel Butt <shakeel.butt@linux.dev> > Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Excellent! Acked-by: Johannes Weiner <hannes@cmpxchg.org> ^ permalink raw reply [flat|nested] 11+ messages in thread
end of thread, other threads:[~2024-07-05 9:41 UTC | newest] Thread overview: 11+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2024-06-27 10:55 [PATCH v7 0/2] mm: store zero pages to be swapped out in a bitmap Usama Arif 2024-06-27 10:55 ` [PATCH v7 1/2] " Usama Arif 2024-06-27 16:18 ` Johannes Weiner 2024-06-28 15:30 ` Usama Arif 2024-07-01 15:01 ` Usama Arif 2024-07-01 15:37 ` Usama Arif 2024-07-01 17:15 ` Usama Arif 2024-07-04 23:22 ` Andrew Morton 2024-07-05 9:41 ` Usama Arif 2024-06-27 10:55 ` [PATCH v7 2/2] mm: remove code to handle same filled pages Usama Arif 2024-06-27 16:19 ` Johannes Weiner
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).