All of lore.kernel.org
 help / color / mirror / Atom feed
From: Usama Arif <usamaarif642@gmail.com>
To: Nhat Pham <nphamcs@gmail.com>
Cc: akpm@linux-foundation.org, hannes@cmpxchg.org, david@redhat.com,
	ying.huang@intel.com, hughd@google.com, willy@infradead.org,
	yosryahmed@google.com, chengming.zhou@linux.dev,
	linux-mm@kvack.org, linux-kernel@vger.kernel.org,
	kernel-team@meta.com, Shakeel Butt <shakeel.butt@linux.dev>
Subject: Re: [PATCH v3 1/2] mm: store zero pages to be swapped out in a bitmap
Date: Tue, 11 Jun 2024 19:50:11 +0100	[thread overview]
Message-ID: <d6088fb2-58d8-4ed1-8d3b-83ea34657db7@gmail.com> (raw)
In-Reply-To: <CAKEwX=PnwjmZKPLX2=ubD6+-+ZAqpXnczkHe4=1QY1hizOE8WQ@mail.gmail.com>


On 11/06/2024 19:39, Nhat Pham wrote:
> On Mon, Jun 10, 2024 at 5:18 AM Usama Arif <usamaarif642@gmail.com> 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>
>> ---
>>   include/linux/swap.h |  1 +
>>   mm/page_io.c         | 92 +++++++++++++++++++++++++++++++++++++++++++-
>>   mm/swapfile.c        | 21 +++++++++-
>>   3 files changed, 111 insertions(+), 3 deletions(-)
>>
>> diff --git a/include/linux/swap.h b/include/linux/swap.h
>> index a11c75e897ec..e88563978441 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 a360857cf75d..2cac1e11fb85 100644
>> --- a/mm/page_io.c
>> +++ b/mm/page_io.c
>> @@ -172,6 +172,82 @@ 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);
>> +       }
>> +}
>> +
>> +static bool 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 false;
>> +       }
>> +       return true;
>> +}
>> +
>>   /*
>>    * We may have stale swap cache pages in memory: notice
>>    * them here and get rid of the unnecessary final write.
>> @@ -195,6 +271,15 @@ 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_start_writeback(folio);
>> +               folio_unlock(folio);
>> +               folio_end_writeback(folio);
>> +               return 0;
>> +       }
>> +       swap_zeromap_folio_clear(folio);
>>          if (zswap_store(folio)) {
>>                  folio_start_writeback(folio);
>>                  folio_unlock(folio);
>> @@ -515,8 +600,11 @@ void swap_read_folio(struct folio *folio, bool synchronous,
>>                  psi_memstall_enter(&pflags);
>>          }
>>          delayacct_swapin_start();
>> -
>> -       if (zswap_load(folio)) {
>> +       if (swap_zeromap_folio_test(folio)) {
>> +               folio_zero_fill(folio);
>> +               folio_mark_uptodate(folio);
>> +               folio_unlock(folio);
>> +       } else if (zswap_load(folio)) {
>>                  folio_mark_uptodate(folio);
>>                  folio_unlock(folio);
>>          } else if (data_race(sis->flags & SWP_FS_OPS)) {
>> diff --git a/mm/swapfile.c b/mm/swapfile.c
>> index f1e559e216bd..90451174fe34 100644
>> --- a/mm/swapfile.c
>> +++ b/mm/swapfile.c
>> @@ -453,6 +453,8 @@ static unsigned int cluster_list_del_first(struct swap_cluster_list *list,
>>   static void swap_cluster_schedule_discard(struct swap_info_struct *si,
>>                  unsigned int idx)
>>   {
>> +       unsigned int i;
>> +
>>          /*
>>           * If scan_swap_map_slots() can't find a free cluster, it will check
>>           * si->swap_map directly. To make sure the discarding cluster isn't
>> @@ -461,6 +463,13 @@ static void swap_cluster_schedule_discard(struct swap_info_struct *si,
>>           */
>>          memset(si->swap_map + idx * SWAPFILE_CLUSTER,
>>                          SWAP_MAP_BAD, SWAPFILE_CLUSTER);
>> +       /*
>> +        * zeromap can see updates from concurrent swap_writepage() and swap_read_folio()
>> +        * call on other slots, hence use atomic clear_bit for zeromap instead of the
>> +        * non-atomic bitmap_clear.
>> +        */
>> +       for (i = 0; i < SWAPFILE_CLUSTER; i++)
>> +               clear_bit(idx * SWAPFILE_CLUSTER + i, si->zeromap);
>>
>>          cluster_list_add_tail(&si->discard_clusters, si->cluster_info, idx);
>>
>> @@ -482,7 +491,7 @@ static void __free_cluster(struct swap_info_struct *si, unsigned long idx)
>>   static void swap_do_scheduled_discard(struct swap_info_struct *si)
>>   {
>>          struct swap_cluster_info *info, *ci;
>> -       unsigned int idx;
>> +       unsigned int idx, i;
>>
>>          info = si->cluster_info;
>>
>> @@ -498,6 +507,8 @@ static void swap_do_scheduled_discard(struct swap_info_struct *si)
>>                  __free_cluster(si, idx);
>>                  memset(si->swap_map + idx * SWAPFILE_CLUSTER,
>>                                  0, SWAPFILE_CLUSTER);
>> +               for (i = 0; i < SWAPFILE_CLUSTER; i++)
>> +                       clear_bit(idx * SWAPFILE_CLUSTER + i, si->zeromap);
>>                  unlock_cluster(ci);
>>          }
>>   }
>> @@ -1336,6 +1347,7 @@ static void swap_entry_free(struct swap_info_struct *p, swp_entry_t entry)
>>          count = p->swap_map[offset];
>>          VM_BUG_ON(count != SWAP_HAS_CACHE);
>>          p->swap_map[offset] = 0;
>> +       clear_bit(offset, p->zeromap);
> Hmm so clear_bit() is done at the swap_entry_free() point. I wonder if
> we can have a problem, where:
>
> 1. The swap entry has its zeromap bit set, and is freed to the swap
> slot cache (free_swap_slot() in mm/swap_slots.c). For instance, it is
> reclaimed from the swap cache, and all the processes referring to it
> are terminated, which decrements the swap count to 0 (swap_free() ->
> __swap_entry_free() -> free_swap_slots())
>
> 2. The swap slot is then re-used in swap space allocation
> (add_to_swap()) - its zeromap bit is never cleared.
>
> 3. swap_writepage() writes that non-zero page to swap

In swap_writepage, with this patch you have:

     if (is_folio_zero_filled(folio)) {
         swap_zeromap_folio_set(folio);
         folio_unlock(folio);
         return 0;
     }
     swap_zeromap_folio_clear(folio);

i.e. if folio is not zero filled, swap_zeromap_folio_clear will be 
called and the bit is cleared, so I think it would take care of this 
scenario? swap_read_folio will see the bit cleared in step 4.

> 4. swap_read_folio() checks the bitmap, sees that the zeromap bit for
> the entry is set, so populates a zero page for it.
>
> zswap in the past has to carefully invalidate these leftover entries
> quite carefully. Chengming then move the invalidation point to
> free_swap_slot(), massively simplifying the logic.
>
> I wonder if we need to do the same here?


  parent reply	other threads:[~2024-06-11 18:50 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-10 12:15 [PATCH v3 0/2] mm: store zero pages to be swapped out in a bitmap Usama Arif
2024-06-10 12:15 ` [PATCH v3 1/2] " Usama Arif
2024-06-10 13:07   ` Matthew Wilcox
2024-06-10 13:56     ` Usama Arif
2024-06-10 14:06       ` Matthew Wilcox
2024-06-10 14:14         ` Usama Arif
2024-06-10 14:33           ` Usama Arif
2024-06-10 17:57   ` Yosry Ahmed
2024-06-10 18:36     ` Usama Arif
2024-06-10 18:47       ` Yosry Ahmed
2024-06-11 11:49         ` Usama Arif
2024-06-11 15:42           ` Yosry Ahmed
2024-06-11 16:52             ` Usama Arif
2024-06-11 17:51               ` Yosry Ahmed
2024-06-11 18:43                 ` Usama Arif
2024-06-11 18:39   ` Nhat Pham
2024-06-11 18:46     ` Yosry Ahmed
2024-06-11 18:53       ` Nhat Pham
2024-06-11 18:50     ` Usama Arif [this message]
2024-06-11 19:33       ` Nhat Pham
2024-06-12 10:42         ` Usama Arif
2024-06-10 12:16 ` [PATCH v3 2/2] mm: remove code to handle same filled pages Usama Arif
2024-06-13 21:21 ` [PATCH v3 0/2] mm: store zero pages to be swapped out in a bitmap Yosry Ahmed
2024-06-14  9:22   ` Usama Arif
2024-06-14  9:28     ` Yosry Ahmed
2024-06-13 21:50 ` Yosry Ahmed
2024-06-13 22:41   ` Shakeel Butt
2024-06-13 22:59     ` Yosry Ahmed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=d6088fb2-58d8-4ed1-8d3b-83ea34657db7@gmail.com \
    --to=usamaarif642@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=chengming.zhou@linux.dev \
    --cc=david@redhat.com \
    --cc=hannes@cmpxchg.org \
    --cc=hughd@google.com \
    --cc=kernel-team@meta.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=nphamcs@gmail.com \
    --cc=shakeel.butt@linux.dev \
    --cc=willy@infradead.org \
    --cc=ying.huang@intel.com \
    --cc=yosryahmed@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.