mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
synced 2024-12-28 16:56:26 +00:00
mm: store zero pages to be swapped out in a bitmap
Patch series "mm: store zero pages to be swapped out in a bitmap", v8. As shown in the patch series 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. [1] https://lore.kernel.org/all/20171018104832epcms5p1b2232e2236258de3d03d1344dde9fce0@epcms5p1/ [2] https://lore.kernel.org/lkml/20240325235018.2028408-1-yosryahmed@google.com/ This patch (of 2): 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/ Link: https://lkml.kernel.org/r/20240823190545.979059-1-usamaarif642@gmail.com Link: https://lkml.kernel.org/r/20240823190545.979059-2-usamaarif642@gmail.com 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> Acked-by: 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: Usama Arif <usamaarif642@gmail.com> Cc: Andi Kleen <ak@linux.intel.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
This commit is contained in:
parent
435b3894e7
commit
0ca0c24e32
@ -296,6 +296,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 list_head free_clusters; /* free clusters list */
|
||||
struct list_head full_clusters; /* full clusters list */
|
||||
|
118
mm/page_io.c
118
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;
|
||||
@ -427,6 +520,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);
|
||||
@ -517,7 +630,10 @@ 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);
|
||||
goto finish;
|
||||
} else if (zswap_load(folio)) {
|
||||
folio_unlock(folio);
|
||||
goto finish;
|
||||
}
|
||||
|
@ -940,6 +940,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;
|
||||
@ -2609,7 +2617,8 @@ static int swap_node(struct swap_info_struct *si)
|
||||
|
||||
static void setup_swap_info(struct swap_info_struct *si, int prio,
|
||||
unsigned char *swap_map,
|
||||
struct swap_cluster_info *cluster_info)
|
||||
struct swap_cluster_info *cluster_info,
|
||||
unsigned long *zeromap)
|
||||
{
|
||||
int i;
|
||||
|
||||
@ -2634,6 +2643,7 @@ static void setup_swap_info(struct swap_info_struct *si, int prio,
|
||||
}
|
||||
si->swap_map = swap_map;
|
||||
si->cluster_info = cluster_info;
|
||||
si->zeromap = zeromap;
|
||||
}
|
||||
|
||||
static void _enable_swap_info(struct swap_info_struct *si)
|
||||
@ -2662,11 +2672,12 @@ static void _enable_swap_info(struct swap_info_struct *si)
|
||||
|
||||
static void enable_swap_info(struct swap_info_struct *si, int prio,
|
||||
unsigned char *swap_map,
|
||||
struct swap_cluster_info *cluster_info)
|
||||
struct swap_cluster_info *cluster_info,
|
||||
unsigned long *zeromap)
|
||||
{
|
||||
spin_lock(&swap_lock);
|
||||
spin_lock(&si->lock);
|
||||
setup_swap_info(si, prio, swap_map, cluster_info);
|
||||
setup_swap_info(si, prio, swap_map, cluster_info, zeromap);
|
||||
spin_unlock(&si->lock);
|
||||
spin_unlock(&swap_lock);
|
||||
/*
|
||||
@ -2684,7 +2695,7 @@ static void reinsert_swap_info(struct swap_info_struct *si)
|
||||
{
|
||||
spin_lock(&swap_lock);
|
||||
spin_lock(&si->lock);
|
||||
setup_swap_info(si, si->prio, si->swap_map, si->cluster_info);
|
||||
setup_swap_info(si, si->prio, si->swap_map, si->cluster_info, si->zeromap);
|
||||
_enable_swap_info(si);
|
||||
spin_unlock(&si->lock);
|
||||
spin_unlock(&swap_lock);
|
||||
@ -2709,6 +2720,7 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
|
||||
{
|
||||
struct swap_info_struct *p = NULL;
|
||||
unsigned char *swap_map;
|
||||
unsigned long *zeromap;
|
||||
struct swap_cluster_info *cluster_info;
|
||||
struct file *swap_file, *victim;
|
||||
struct address_space *mapping;
|
||||
@ -2831,6 +2843,8 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
|
||||
p->max = 0;
|
||||
swap_map = p->swap_map;
|
||||
p->swap_map = NULL;
|
||||
zeromap = p->zeromap;
|
||||
p->zeromap = NULL;
|
||||
cluster_info = p->cluster_info;
|
||||
p->cluster_info = NULL;
|
||||
spin_unlock(&p->lock);
|
||||
@ -2843,6 +2857,7 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
|
||||
free_percpu(p->cluster_next_cpu);
|
||||
p->cluster_next_cpu = NULL;
|
||||
vfree(swap_map);
|
||||
kvfree(zeromap);
|
||||
kvfree(cluster_info);
|
||||
/* Destroy swap account information */
|
||||
swap_cgroup_swapoff(p->type);
|
||||
@ -3340,6 +3355,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
|
||||
sector_t span;
|
||||
unsigned long maxpages;
|
||||
unsigned char *swap_map = NULL;
|
||||
unsigned long *zeromap = NULL;
|
||||
struct swap_cluster_info *cluster_info = NULL;
|
||||
struct page *page = NULL;
|
||||
struct inode *inode = NULL;
|
||||
@ -3430,6 +3446,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.
|
||||
*/
|
||||
zeromap = kvmalloc_array(BITS_TO_LONGS(maxpages), sizeof(long),
|
||||
GFP_KERNEL | __GFP_ZERO);
|
||||
if (!zeromap) {
|
||||
error = -ENOMEM;
|
||||
goto bad_swap_unlock_inode;
|
||||
}
|
||||
|
||||
if (si->bdev && bdev_stable_writes(si->bdev))
|
||||
si->flags |= SWP_STABLE_WRITES;
|
||||
|
||||
@ -3505,7 +3532,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
|
||||
if (swap_flags & SWAP_FLAG_PREFER)
|
||||
prio =
|
||||
(swap_flags & SWAP_FLAG_PRIO_MASK) >> SWAP_FLAG_PRIO_SHIFT;
|
||||
enable_swap_info(si, prio, swap_map, cluster_info);
|
||||
enable_swap_info(si, prio, swap_map, cluster_info, zeromap);
|
||||
|
||||
pr_info("Adding %uk swap on %s. Priority:%d extents:%d across:%lluk %s%s%s%s\n",
|
||||
K(si->pages), name->name, si->prio, nr_extents,
|
||||
@ -3540,6 +3567,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
|
||||
si->flags = 0;
|
||||
spin_unlock(&swap_lock);
|
||||
vfree(swap_map);
|
||||
kvfree(zeromap);
|
||||
kvfree(cluster_info);
|
||||
if (inced_nr_rotate_swap)
|
||||
atomic_dec(&nr_rotate_swap);
|
||||
|
Loading…
Reference in New Issue
Block a user