mm: store zero pages to be swapped out in a bitmap
Subsystems (3)
v6 cover mm: store zero pages to be swapped out in a bitmap
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/[email protected]/
---
v5 -> v6 (kernel test robot <[email protected]>):
- 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 | 15 ++++++ mm/zswap.c | 86 +++----------------------------- 4 files changed, 136 insertions(+), 79 deletions(-)
Signature · 1 line
2.43.0
PATCH v6 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/
--- include/linux/swap.h | 1 + mm/page_io.c | 113 ++++++++++++++++++++++++++++++++++++++++++- mm/swapfile.c | 15 ++++++ 3 files changed, 128 insertions(+), 1 deletion(-)
include/linux/swap.h +1 -0
@@ -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 */
mm/page_io.c +112 -1
137 lines not on this page · raw message
mm/swapfile.c +15 -0
@@ -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,12 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) goto bad_swap_unlock_inode; } + p->zeromap =kvzalloc (DIV_ROUND_UP (maxpages,8 ),GFP_KERNEL ); +if (!p->zeromap) { + error = -ENOMEM ; + goto bad_swap_unlock_inode; + } +if (p->bdev &&bdev_stable_writes (p->bdev)) p->flags |=SWP_STABLE_WRITES ;
Signature · 1 line
2.43.0
PATCH v6 2/2 mm: remove code to handle same filled pages
In reply to Usama ArifWith 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.
--- mm/zswap.c | 86 +++++------------------------------------------------- 1 file changed, 8 insertions(+), 78 deletions(-)
mm/zswap.c +8 -78
@@ -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 ; }
Signature · 1 line
2.43.0